Module

Data.Functor.Variant

Package
purescript-variant
Repository
natefaubion/purescript-variant

#VariantF Source

data VariantF (f :: Row Type) a

Instances

#FProxy Source

data FProxy (a :: Type -> Type)

Constructors

#inj Source

inj :: forall r2 r1 a f sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => Functor f => SProxy sym -> f a -> VariantF r2 a

Inject into the variant at a given label.

maybeAtFoo :: forall r. VariantF (foo :: FProxy Maybe | r) Int
maybeAtFoo = inj (SProxy :: SProxy "foo") (Just 42)

#prj Source

prj :: forall r2 r1 a f sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => SProxy sym -> VariantF r2 a -> Maybe (f a)

Attempt to read a variant at a given label.

case prj (SProxy :: SProxy "foo") maybeAtFoo of
  Just (Just i) -> i + 1
  _ -> 0

#on Source

on :: forall r2 r1 b a f sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => SProxy sym -> (f a -> b) -> (VariantF r1 a -> b) -> VariantF r2 a -> b

Attempt to read a variant at a given label by providing branches. The failure branch receives the provided variant, but with the label removed.

#case_ Source

case_ :: forall b a. VariantF () a -> b

Combinator for exhaustive pattern matching.

caseFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
caseFn = case_
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))
 # on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> either id show baz)

#default Source

default :: forall r b a. a -> VariantF r b -> a

Combinator for partial matching with a default value in case of failure.

caseFn :: forall r. VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String) | r) Int -> String
caseFn = default "No match"
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))

Re-exports from Data.Symbol

#SProxy Source

data SProxy (sym :: Symbol)

A value-level proxy for a type-level symbol.

Constructors