fantasyland / static-land

Specification for common algebraic structures in JavaScript based on Fantasy Land
MIT License
772 stars 41 forks source link

chain signature #18

Closed gcanti closed 8 years ago

gcanti commented 8 years ago

Hello,

in PureScript and Haskell the signature is

bind :: Bind m => (m a, a → m b) → m b

while in static land is

chain :: Chain m => (a → m b, m a) → m b

Is there a particular reason for that? At first sight having m a near the chain call seems more handy

// current spec
monad.chain((a) => {

  ... long snippet of code ...

}, ma) // <= this is far away from the chain call
rpominov commented 8 years ago

Hey! Two reasons:

1) To match signature of map and ap.

(  a →   b , f a) → f b
(f(a →   b), f a) → f b
(  a → m b , m a) → m b

2) For currying.

const flow = (value, ...fns) => fns.reduce((x, fn) => fn(x), value)

flow(T.of(1),
  T.chain(x => T.of(x + 1)),
  T.chain(x => T.of(x + 2))
) // -> T.of(4)

T would need to implement Ramda-like curried chain for this though. And SL used to require that, but now it just allows currying and don't require it.

gcanti commented 8 years ago

Thanks, makes sense.

SL used to require that, but now it just allows currying and don't require it.

Not sure about that. So if I implement a curried chain I'm still SL-compliant? There's no mention in the docs

chain :: Chain m => (a → m b) → m a → m b

Seems weird for interop though, how could you call another library if you don't know if it's curried or not?

rpominov commented 8 years ago

So if I implement a curried chain I'm still SL-compliant?

Not just curried, but Ramda-like curried, when both T.chain(f)(a) and T.chain(f, a) work.

gcanti commented 8 years ago

I see, thanks for explaining.

So currently flow-static-land is not compliant (I wonder if there is a way to fix that). I guess I'll pick the more sensible choice, the curried one

Thanks again @rpominov

rpominov commented 8 years ago

I think flow-static-land is compliant. Spec only require that T.chain(f, a) works, and it doesn't say anything to disallow T.chain(f)(a) to work. But it's up to implementer whether to implement currying or not, spec only requires uncurried signature.

gcanti commented 8 years ago

Got it. I don't want to pollute this repo with flow-static-land specific issues, I'll think about this a little bit more and, if it's ok for you, I'll open an issue there mentioning you

rpominov commented 8 years ago

Yeah, sure.