louthy / language-ext

C# pure functional programming framework - come and get declarative!
MIT License
6.53k stars 421 forks source link

[Wiki error]- Partial application #837

Closed hjkl950217 closed 3 weeks ago

hjkl950217 commented 3 years ago

address :https://github.com/louthy/language-ext/wiki/Thinking-Functionally:-Partial-application

//using
using LanguageExt;
using static LanguageExt.Prelude;
using static LanguageExt.List;
using LanguageExt.ClassInstances;

//code
Func<int, int, int> add = (x, y) => x + y;  //<--- error in there
Func<int, int> add42 = par(add, 42);  

int x = add42(1);  // x == 43
int y = add42(3);  // y == 45

var res = lpar(map, add42)(List(1,2,3)); //<--- error in there

This code doesn't compile.

TysonMN commented 3 years ago

The first error is that x can't be used as an identifier in both places. Changing either identifier for both x and y fixes the problem to someth

The second issue seems to be that the C# compiler is unable to both select a single map among the 64 overloads and infer the type parameters of lpar as the same time.

hjkl950217 commented 3 years ago

The second error doesn't know how to modify the code,I just contacted this library, but I couldn't find the corresponding method

howardgod commented 1 year ago

var res = lpar<IEnumerable, Func<int,int>, IEnumerable>(map, add42)(List(1, 2, 3));

this shall fix second error.

The reason behind this is that since lpar is not define as Func<> so that C# can't interpret the generic type correctly.

In document. there's a note. And this shall apply to lpar as well. NOTE: When using a method as an argument to curry you will need to provide the generic arguments that represent the arguments and return type of the method being curried.