Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Currying #37

Open
jgrund opened this issue Oct 11, 2016 · 11 comments
Open

Currying #37

jgrund opened this issue Oct 11, 2016 · 11 comments

Comments

@jgrund
Copy link

jgrund commented Oct 11, 2016

Thoughts on currying by default a-la Haskell, PureScript, Elm?

@gcanti
Copy link
Owner

gcanti commented Oct 11, 2016

Hi,

a relevant discussion in the static-land repo fantasyland/static-land#6

/cc @rpominov

@rpominov
Copy link

@jgrund Just curious what kind of currying you have in mind? There are two ways to approach it:

  1. We can make this work fn(a)(b), but not this fn(a, b).
  2. We can make both fn(a)(b) and fn(a, b) work.

There are different sets of trade-offs for this two options in regard of typing code with Flow.

@jgrund
Copy link
Author

jgrund commented Oct 11, 2016

@rpominov I am thinking of the second option.

Both can be represented in flow, though the second in a more complex fashion via overloads.

@jgrund
Copy link
Author

jgrund commented Oct 11, 2016

I think option 1 is more of a surfacing to user code of what happens in languages that support currying.

@ivenmarquardt
Copy link

Is the difference between fn(a)(b) and fn(a, b) really that big that it justifies the second version, which would require a programmatic solution and thus an additional layer of complexity?

@jgrund
Copy link
Author

jgrund commented Oct 11, 2016

I think the advantage of the second version is that it can be transparent: i.e. fn(a, b).

The first version takes two invocations regardless if the user plans to fully satisfy the arguments.

That said, I suppose it could be said the first is more explicit.

@jgrund
Copy link
Author

jgrund commented Oct 11, 2016

Ultimately, It would be great if currying was not a user-land construct in JS and was part of the language.

@davidchambers
Copy link

Is the difference between fn(a)(b) and fn(a, b) really that big that it justifies the second version, which would require a programmatic solution and thus an additional layer of complexity?

Another question is whether requiring fn(a)(b) would prevent some potential users from adopting the project. The answer is undoubtedly yes, since programmers are just as irrational (i.e. guided by emotion) as the rest of the population. If wide adoption is a goal, requiring )( is inadvisable.

@ivenmarquardt
Copy link

@davidchambers OK - evolution is better than revolution. I can live with the second version.

@rjmk
Copy link

rjmk commented Oct 12, 2016

I'm for 2 over 1 and uncurried over 2.

I'm for 2 over 1 because of the complexity @ivenmarquardt has mentioned. I've had a lot of headaches with functions curried Ramda-style (maybe this problem is solvable, maybe not).

I'm for uncurried over 2 because of what @davidchambers said.

@jgrund
Copy link
Author

jgrund commented Oct 14, 2016

Something interesting about currying at the library level. Using the implementation in Fun.js, consider:

const result = maybe.map(
  (a):number => a + 1,
  maybe.inj(3)
);

const result2 = maybe.map(
  (a):string => a + 'bar',
  maybe.inj('foo')
);

const curryMap = fun.curry(maybe.map);

const result3 = curryMap(
  (a):number => a + 1,
  maybe.inj(3)
);

const result4 = curryMap(
  (a):string => a + 'bar',
  maybe.inj('foo')
);

If we run flow suggest:

-const result = maybe.map(
+const result: Maybe<number> = maybe.map(
   (a):number => a + 1,
   maybe.inj(3)
 );

-const result2 = maybe.map(
+const result2: Maybe<string> = maybe.map(
   (a):string => a + 'bar',
   maybe.inj('foo')
 );

 const curryMap = fun.curry(maybe.map);

-const result3 = curryMap(
+const result3: Maybe<number | string> = curryMap(
   (a):number => a + 1,
   maybe.inj(3)
 );

-const result4 = curryMap(
+const result4: Maybe<number | string> = curryMap(
   (a):string => a + 'bar',
   maybe.inj('foo')
 );

When currying, flow is taking a union of the possibilities, which it doesn't in the non-curried version.

This seems like a large issue, as the buildup of types from usage is problematic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants