0

I have never been really good at C# and I am trying to better myself by learing new things. I am now trying lambda's.

this is my code so far:

public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper)
    {
        Func<float[], bool[]> fun = x => new bool[] { true };
        return fun;
    }

    public static void Main()
    {
        Func<float, bool> success = x => x == 5.5f;
        var result = CategoricalMap(success)(new float[] { 4f, 5.5f, 3.5f, -5.5f, 10.2f });
        Console.ReadKey();
    }

What I am trying to do is, to check what number is equal to 5.5f, but I don't know how to get the floats to show up in the CategoricalMap function. I know a bit of the puzzle, I have to do this:

mapper(float);

but I don't know how to get the floats from result to the function. Please help.

Edit 1

I think I should make things more clear, A few weeks ago asked a someone to give me lambda challenges, sadly I have no way of contacting him. (kinda stupid of me) this is what he gave me:

Create the lambda success which returns true if the given value is above or equal to 5.5f

Create the following function: CategoricalMap Creates a mapper function Input: mapper a function of type Func<float, bool> Output: a function of type Func<float[], bool[]> that takes in an array of booleans and applies the function mapper to each of the elements and stores the results in an integer array

6
  • 1
    This seems very overly complicated for something you could do with linq. var matching = { 4f, 5.5f, 3.5f, ... }.Where(x => x == 5.5f); Commented Mar 23, 2020 at 18:59
  • @gunr2171 yeah I know I just wanted to try it without using linq, I learn more this way. Commented Mar 23, 2020 at 19:02
  • You seem to be using lambdas for the sake of using lambdas. In other words, I don't see why lambdas are needed here. bool[] CategoricalMap(float someFloat, float[] floatArray) would suffice, wouldn't it? Commented Mar 23, 2020 at 19:05
  • Oh wait, are you trying to reinvent Select? Commented Mar 23, 2020 at 19:07
  • 1
    So I'm guessing you expect result to be false, true, false, false, false? Hint: you need a loop. Commented Mar 23, 2020 at 19:11

1 Answer 1

1
Func<float[], bool[]> fun = x => new bool[] { true };

There x is your argument and it has type float[]. So in fact it is an array which you can iterate:

Func<float[], bool[]> fun = x => { 
   var result = new bool[x.Length];
   for(int i = 0; i < x.Length; ++i) {
      result[i] = mapper(x[i]);
   } 
   return result;
};

Now you can use it as

public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper)
{
   Func<float[], bool[]> fun = x => { 
      var result = new bool[x.Length];
      for(int i = 0; i < x.Length; ++i) {
         result[i] = mapper(x[i]);
      }
      return result;
   };
   return fun;
}

public static void Main()
{
    Func<float, bool> success = x => x == 5.5f;
    var result = CategoricalMap(success)(new float[] { 4f, 5.5f, 3.5f, -5.5f, 10.2f });
    Console.ReadKey();
}

But don't do that. This approach is terrible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.