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

either: add S.lefts and S.rights #144

Merged
merged 1 commit into from
Feb 4, 2016
Merged

either: add S.lefts and S.rights #144

merged 1 commit into from
Feb 4, 2016

Conversation

davidchambers
Copy link
Member

This pull request adds Haskell's lefts and rights:

lefts  :: [Either a b] -> [a]
rights :: [Either a b] -> [b]

I recently reviewed some code @Bpless wrote which could be nicely expressed in terms of rights.

These functions are similar to S.catMaybes, which should arguably be renamed S.justs for consistency.

def('lefts',
{},
[$.Array($Either(a, b)), $.Array(a)],
R.chain(function(either) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both of these cases, we could alternatively filter -> map to achieve the same ends.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We certainly could!

   def('lefts',
       {},
       [$.Array($Either(a, b)), $.Array(a)],
-      R.chain(function(either) {
-        return either.isLeft ? [either.value] : [];
-      }));
+      compose(R.pluck('value'), R.filter(isLeft)));

   //# rights :: [Either a b] -> [b]
   //.

I slightly prefer the chain version because it avoids an intermediate collection. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer chain too.

@svozza
Copy link
Member

svozza commented Feb 3, 2016

These a nice functions, I could see them being quite useful.

davidchambers added a commit that referenced this pull request Feb 4, 2016
either: add S.lefts and S.rights
@davidchambers davidchambers merged commit f35a3df into master Feb 4, 2016
@davidchambers davidchambers deleted the dc-lefts-rights branch February 4, 2016 05:12
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 this pull request may close these issues.

3 participants