Module

Data.Variant

Package
purescript-variant
Repository
natefaubion/purescript-variant

#Variant Source

data Variant (a :: Row Type)

#inj Source

inj :: forall r2 r1 a sym. RowCons sym a r1 r2 => IsSymbol sym => SProxy sym -> a -> Variant r2

Inject into the variant at a given label.

intAtFoo :: forall r. Variant (foo :: Int | r)
intAtFoo = inj (SProxy :: SProxy "foo") 42

#prj Source

prj :: forall r2 r1 a sym. RowCons sym a r1 r2 => IsSymbol sym => SProxy sym -> Variant r2 -> Maybe a

Attempt to read a variant at a given label.

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

#on Source

on :: forall r2 r1 b a sym. RowCons sym a r1 r2 => IsSymbol sym => SProxy sym -> (a -> b) -> (Variant r1 -> b) -> Variant r2 -> 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 a. Variant () -> a

Combinator for exhaustive pattern matching.

caseFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
caseFn = case_
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)
 # on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> show baz)

#default Source

default :: forall r a. a -> Variant r -> a

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

caseFn :: forall r. Variant (foo :: Int, bar :: String | r) -> String
caseFn = default "No match"
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)

Re-exports from Data.Symbol

#SProxy Source

data SProxy (sym :: Symbol)

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

Constructors