purescript-either icon indicating copy to clipboard operation
purescript-either copied to clipboard

Add `lefts` and `rights` functions

Open l-monnier opened this issue 1 year ago • 1 comments

Description of the change

Add a lefts function similar to the one existing in Haskell Data.Either. Add also its "partner" rights function.

Motivation

I find lefts useful to collect the error messages of multiple validations of a same value, when such validations do not alter the value. For example, if I want to test that a value is no longer than n, has no forbidden character, contains at least 1 number, etc.

Notes

As in PureScript there are Array and List, I've made the type signature more general than the Haskell one (which is only for native Haskell lists). I've used Monad (and Monoid) to avoid introducing new dependencies. However, it seems to me that Foldable would be a better fit than Monad conceptually. But I don't think it matters much in practice.

The above makes the documentation a bit tricky as those functions are pretty abstract. I hope it's still understandable while remaining accurate enough.

I let you decide if you want to include those 2 functions in the library.

Checklist

  • [x] Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • [x] Linked any existing issues or proposals that this pull request should close [none]
  • [x] Updated or added relevant documentation
  • [x] Added a test for the contribution (if applicable)

l-monnier avatar Aug 29 '24 21:08 l-monnier

FWIW, functionality like this exists in purescript-filterable.

natefaubion avatar Aug 29 '24 21:08 natefaubion