Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(pointfree) chain: Chain f => (a -> f a) -> f a -> f a #178

Closed
yotamDvir opened this issue Apr 13, 2016 · 3 comments · Fixed by #216
Closed

(pointfree) chain: Chain f => (a -> f a) -> f a -> f a #178

yotamDvir opened this issue Apr 13, 2016 · 3 comments · Fixed by #216

Comments

@yotamDvir
Copy link

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.

@yotamDvir yotamDvir changed the title Add pointfree chain chain: Functor f => (a -> f a) -> f a -> f a Apr 13, 2016
@yotamDvir yotamDvir changed the title chain: Functor f => (a -> f a) -> f a -> f a (pointfree) chain: Functor f => (a -> f a) -> f a -> f a Apr 13, 2016
@davidchambers
Copy link
Member

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.

@yotamDvir
Copy link
Author

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.

@davidchambers
Copy link
Member

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.

@yotamDvir yotamDvir changed the title (pointfree) chain: Functor f => (a -> f a) -> f a -> f a (pointfree) chain: Chain f => (a -> f a) -> f a -> f a Apr 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants