Skip to content

Instantly share code, notes, and snippets.

@effe-megna
Last active May 8, 2020 15:25
Show Gist options
  • Select an option

  • Save effe-megna/d49c77cd79b4b4fea239f429abaaf66a to your computer and use it in GitHub Desktop.

Select an option

Save effe-megna/d49c77cd79b4b4fea239f429abaaf66a to your computer and use it in GitHub Desktop.
These monad implemented in Elm
type These e a
= Both ( e, a )
| Left e
| Right a
map : (a -> b) -> These e a -> These e b
map f t =
bimap ( identity, f ) t
mapLeft : (e -> g) -> These e a -> These g a
mapLeft f t =
bimap ( f, identity ) t
bimap : ( e -> g, a -> b ) -> These e a -> These g b
bimap ( g, f ) t =
case t of
Left e ->
Left (g e)
Right a ->
Right (f a)
Both ( e, a ) ->
Both ( g e, f a )
andThen : (a -> These e b) -> These e a -> These e b
andThen f t =
case t of
Left e ->
Left e
Right a ->
f a
Both ( e, a ) ->
case f a of
Left _ ->
Left e
Right aa ->
Right aa
Both ( ee, aa ) ->
Both ( ee, aa )
swap : These e a -> These a e
swap t =
case t of
Left e ->
Right e
Right a ->
Left a
Both ( e, a ) ->
Both ( a, e )
leftOnly : These e a -> Maybe e
leftOnly t =
unwrapLeft Nothing Just <| t
rightOnly : These e a -> Maybe a
rightOnly t =
unwrap Nothing Just <| t
unwrap : b -> (a -> b) -> These e a -> b
unwrap default f t =
case t of
Left _ ->
default
Right a ->
f a
Both ( _, a ) ->
f a
unwrapLeft : g -> (e -> g) -> These e a -> g
unwrapLeft default f t =
case t of
Left e ->
f e
Right _ ->
default
Both ( e, _ ) ->
f e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment