MyNixOS website logo
Description

Combinators for operating with selections over an underlying functor.

See the README on github for tutorials!

Selections

Hackage

See it on Hackage

See also this library's Scala port by Christopher Davenport

You'll probably want to start by reading the tutorial.

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  -- wrap `[Int]` into `Selection [] Int Int`, you can wrap any functor
     & select even  -- Focus on only even integers
     & mapSelected (+100)  -- Increment selected ints by 100
     & bimap (("Odd: " ++) . show) (("Even: " ++) . show)  -- map over unselected and selected values respectively
     & forgetSelection -- Collapse back down to the underlying functor, in this case a list
["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.

Examples

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

Metadata

Version

0.3.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows