Sanctuary: (pointfree) chain: Chain f => (a -> f a) -> f a -> f a

Created on 13 Apr 2016  路  3Comments  路  Source: sanctuary-js/sanctuary

I am missing the pointfree chain function in this library.

I have several functions of signature Functor f => a -> f a, which I would like to compose using pipe.
I have written the following conversion to pointfree of chain to allow this.

// Functor f => (a -> f a) -> f a -> f a
const chain = func => x => x.chain(func);

Now I can write

// Functor f => a -> f a
const composed = pipe([func1, chain(func2), chain(func3)]);

Maybe there is a different intended way of doing this with this library? If not, I think the simple chain function would be a good addition.

Most helpful comment

To clarify the relationship between Ramda and Sanctuary, Sanctuary is currently complementary to Ramda. At some point, though, Sanctuary will be an alternative to Ramda, providing much of the same functionality but with a different approach to handling type errors.

All 3 comments

I agree that this is a great idea! Ramda provides R.chain, and I'd love Sanctuary to provide S.chain.

I don't think the implementation is quite as simple as func => x => x.chain(func), since it would be nice to provide an implementation for Array (as Ramda does).

@svozza has been working on a map implementation in #142. We've encountered a few problems, but once we've resolved them all it should be fairly easy to add chain.

I believe the correct signature is:

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

Every Chain is a Functor, but the reverse is not true.

Oh yes you're right of course about the signature.

The implementation I suggested works for Chains in Sanctuary; and as for Arrays, the Ramda function is available. However, I read in #142 that you aim to lose that dependency in the future, in which case I agree chain should address Arrays as well.

To clarify the relationship between Ramda and Sanctuary, Sanctuary is currently complementary to Ramda. At some point, though, Sanctuary will be an alternative to Ramda, providing much of the same functionality but with a different approach to handling type errors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t1ger-0527 picture t1ger-0527  路  3Comments

svozza picture svozza  路  7Comments

davidchambers picture davidchambers  路  5Comments

blackxored picture blackxored  路  10Comments

blackxored picture blackxored  路  7Comments