0

I have this method:

public static IQueryable<char> Test(this string text, Func<Func<char, bool>, int> func)

Here char is the character to find in text, bool determines whether the character is found and int returns the index of the character in the text.

How should I write this code? Thanks.

Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
Beta
  • 87
  • 7
  • `func` is a function, that accepts a function and returns an `int`. That input function accepts a `char` and returns a `bool` – mihai Feb 10 '15 at 08:56
  • Thanks. I understand this. But I don't understand how to properly code this. I need an example. – Beta Feb 10 '15 at 08:57
  • 1
    Since there is not a character source in your function, it's hard to detect an index of char. It seems the type of `func` must be `Func, Func, int>` or `Func, Func, int>`. – Mark Shevchenko Feb 10 '15 at 09:06
  • I understood that I needed more methods than I wanted to have. I tried to put a lot in one extension method and there should be a few. – Beta Feb 10 '15 at 21:50

1 Answers1

2

Here's an example that will make you understand what this does.
I have no idea why you would need this but there probably is a reason :).

    // These Func's take a char and return a boolean value.
    Func<char, bool> f1 = (ch) => ch == 'a';
    Func<char, bool> f2 = (ch) => ch == 'b';

    char chr = 'a';

    // This Func takes a function (of the type we saw above) and returns an integer.
    Func<Func<char, bool>, int> func = (foo) => foo(chr) ? 1 : 0;

    // Run the complex Func by passing a function as an input param and receiving an integer as a response.
    int res1 = func(f1); // 1
    int res2 = func(f2); // 0

By your request, here is another example (I still can't find a good usage but whatever):

    string text = "TesTinG";
    Func<char, bool> IsCapital = ch => ch == char.ToUpper(ch);
    int counter = 0;
    foreach (char chr in text.ToCharArray())
    {
        Func<Func<char, bool>, int> func = fn => fn(chr) ? 1 : 0;
        counter += func(IsCapital);
    }
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Hello. Very thanks for this code and explanation. Could you give a more elaborate example how to use this code? – Beta Feb 10 '15 at 09:32
  • @DeividasGrigas - I've added another example of counting capital letters in a string. I still can't find a good use with the Func,int> but whatever.. – Amir Popovich Feb 10 '15 at 09:39
  • Could you help me some more,,, I have this code: Func f1 = (found, p) => new StringParameters(p.Character, p.Text.IndexOf(p.Character, p.Position) != -1); Action comparisonType = (found, p) => f1(found, p).Position != -1 ? true : false;//new StringParameters(p., d)).Position; Func, StringParameters> f2 = (c, index) => text.IndexOf(c, comparisonType); What is wrong with it? – Beta Feb 10 '15 at 17:19
  • @DeividasGrigas - You need to add more code. What are you trying to do here? – Amir Popovich Feb 10 '15 at 21:02
  • I'll open another question then. I'll write the linq in the next comment. Thank you for your help. Where did you learn linq extensions? What material did you read or watch? Thanks. – Beta Feb 10 '15 at 21:28
  • I understood the code already by writing the post so there will be no link. – Beta Feb 10 '15 at 21:43