The selections package

[ Tags: bsd3, data-structures, functors, library ] [ Propose Tags ]

See the README on github for tutorials!

[Skip to Readme]


Dependencies base (>=4.7 && <5), bifunctors, comonad, profunctors [details]
License BSD3
Copyright 2017 Chris Penner
Author Chris Penner
Category Data Structures, Functors
Home page
Source repository head: git clone
Uploaded Sat Aug 12 17:58:09 UTC 2017 by ChrisPenner
Distributions NixOS:
Downloads 185 total (25 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-08-12 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for selections-

[back to package description]



See it on Hackage

selections is a haskell package for transforming subsets of values within a functor using an intuitive selection-based interface.

Ever wished you could select just a few values within a functor, perform some operations on them, then flatten them back into the plain old functor again? Now you can!

Selection is a newtype wrapper around Functors which adds several combinators and interesting instances. Wrapping a functor in Selection allows you to:

  • Select specific values within your functor according to a predicate
  • Expand/Contract selections based on additional predicates using include and exclude
  • Select values based on their context if your functor is also a Comonad
  • Map over unselected and/or selected values using Bifunctor
  • Traverse over unselected and/or selected values using Bitraversable
  • Fold over unselected and/or selected values using Bifoldable
  • Perform monad computations over selected values if your functor is a Monad
  • Extract all unselected or selected elements to a list
  • Deselect and return to your original functor using unify
  • Traverse or fold over selections using Control.Lens

Here's how it looks, tutorials are available here.

xs = [1..6]
λ> newSelection xs & select even & mapSelected (+100) & bimap (("Odd: " ++) . show) (("Even: " ++) . show) & forgetSelection
["Odd: 1","Even: 102","Odd: 3","Even: 104","Odd: 5","Even: 106"]

Technically you could use Selection as a monad-transformer, but it's a bit clunky and you'd probably be better off with EitherT.

Fun fact, Selection is isomorphic to EitherT, but the semantics are quite different and they're suited to different purposes.

When Should/Shouldn't I Use Selections?

You can use selections whenever you've got a bunch of things and you want to operate over just a few of them at a time. You can do everything that selections provides by combining a bunch of predicates with fmap, but it gets messy really quick; selections provides a clean interface for this sort of operation.

You shouldn't use selections when you're looking for a monadic interface for this sort of thing, selections works at the value level and typically you want to chain selection commands using (.) or (&), it technically can be used as a monad transformer if your underlying functor is also a monad, but at that point you may wish to check out EitherT instead.


Check out the Accounts tutorial first to get your bearings. After that continue to the Lenses tutorial.