EitherProjections-0.1.0.0: Either Projections, inspired by Scala.

Safe HaskellSafe
LanguageHaskell2010
Extensions
  • TypeSynonymInstances
  • FlexibleInstances
  • MultiParamTypeClasses
  • FunctionalDependencies

Data.Either.Projections

Contents

Description

Either Projections, inspired by Scala's Either.

Example:

>>> let process = (+) 100 . (4 *)
>>> let foo = fmap (10 *) . rightProjection
>>> let ok = Right 10 :: Either String Int
>>> let fail = Left "wrong input" :: Either String Int
>>> foo ok
RightProjection 140
>>> foo fail
RightNothing "wrong input"
>>> toMaybe $ foo fail
Nothing
>>> toMaybe $ foo ok
Just 140
>>> toEither $ foo fail
Left "wrong input"
>>> mergeEither . toEither . fmap show $ rightProjection ok
"10"

Synopsis

Projections.

class EitherProjection proj left right side | proj -> left, proj -> right, proj -> side where Source

A projection of Either.

Methods

toEither :: proj -> Either left right Source

toMaybe :: proj -> Maybe side Source

type LeftProjection l r = LeftProjection' r l Source

Left projection.

data LeftProjection' r l Source

Left projection with type arguments flipped. | Allows to define instances of Functor, etc.

data RightProjection l r Source

Right projection.

leftProjection :: Either l r -> LeftProjection l r Source

Get left projection.

rightProjection :: Either l r -> RightProjection l r Source

Get right projection.

misc

mergeEither :: Either a a -> a Source

Merge Left and Right. Inspired by Scala.