-1

I'm trying to understand a code but I can't understand what 'p' var is.

public Prediction(Game kkk,bool checkit, params State[] checkStates)
    : base(game, p => Manager.method(kkk, p))
{
    this.checkit = checkit;
    this.checkStates = checkStates;
}

The second class:

public PiratePrediction(Game game, System.Func<Pirate, T> valueExtractor)
{
    this.game = game;

    this.valueExtractor = valueExtractor;

    this.predictedValues = new Dictionary<Pirate, T>();

    this.totalPredictions = 0;
    this.correctPredictions = 0;
}
19
  • 1
    It's the parameter that is passed into the anonymous method. Commented Feb 4, 2017 at 21:32
  • 1
    See SO Whats the point of a lambda expression Commented Feb 4, 2017 at 21:33
  • 1
    Nor do we since you did not post the constructor of the base class that is called. Commented Feb 4, 2017 at 21:33
  • @Abion47 how can I detect the anonymous method? Commented Feb 4, 2017 at 21:33
  • 1
    @Sagi Define "detect". Commented Feb 4, 2017 at 21:34

3 Answers 3

1

found the class you're using on https://github.com/YoavKa/Skillz2016/blob/f23d25eed4baa9786cf517583ee867075a2f0505/API/Prediction/PiratePrediction.cs

the valueExtractor lambda is used from Update, and p comes from the keys of predictedValues dictionary.

 public virtual void Update()
 {
       foreach (var pair in this.predictedValues)
       {
          if (pair.Key.State != PirateState.Lost && !EqualityComparer<T>.Default.Equals(pair.Value, default(T)))
           {
                this.totalPredictions++;
                if (this.valueExtractor(pair.Key).Equals(pair.Value))
                    this.correctPredictions++;
            }
        }
        this.predictedValues.Clear();
    }

the p comes from the call to Predict method of PiratePrediction class. Because it's added to the predictedValues array.

    public T Predict(Pirate p)
    {
        if (this.predictedValues.ContainsKey(p))
            return this.predictedValues[p];
        T predictedValue = this.predict(p);
        if (EqualityComparer<T>.Default.Equals(predictedValue, default(T)))
            return default(T);
        this.predictedValues.Add(p, predictedValue);
        return predictedValue;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I love you. Thanks you mate!
0

p is a parameter being passed into your method. A Func<T, TOut> is a delegate, meaning a method signature.

Consider the following:

private class DisplayClass
{ 
    public readonly Game kkk;
    public DisplayyClass(Game kkk) { this.kkk = kkk; }
    public T handler(Pirate p) { return Manager.method(kkk, p); }
}

public Prediction(Game kkk,bool checkit, params State[] checkStates)
    : base(game, new DisplayClass(kkk).handler)
{
    this.checkit= checkit;
    this.checkStates = checkStates;
}

This is what the compiler does to your code when interpreting lambdas - it may be a good idea to pass your code through a decompiler to see the exact phrasing.

The p variable, as you can see in the expanded code, is a parameter into the method, and lambdas are just a shorthand way to pass methods which can then be invoked in other code.

// Somewhere in the base class...
void ExtractValue(Pirate p)
{ 
    // ...
    T value = this.valueExtractor(p);
    // ...
}

When thus invoked, p will be the value passed in by that other code, and by definition of Func<Pirate, T>, will be of type Pirate.

Keep in mind that the code you're passing a lambda to can invoke the code inside your lambda multiple times, such as is the case with .Select. I suggest not only reading up on lambdas, but also their extensive use in the Linq namespace

Comments

0

A lambda expression is a declaration of an anonymous method. So imagine that this:

p => Manager.method(kkk, p)

is equal to this:

private T SomeMethod<T>(Pirate p)
{
    return Manager.method(kkk, p);
}

You wouldn't be able to do the second snippet in your situation, however, because kkk is a local variable from the scope of where the lambda expression was declared, which means that while the lambda can use kkk, an explicit method declaration can't (See Closure). This is just one benefit of lambdas over declared methods.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.