Data.Functor.Variant
- Package
- purescript-variant
- Repository
- natefaubion/purescript-variant
#VariantF Source
data VariantF (f :: Row Type) a
Instances
Functor (VariantF r)
(RowToList row rl, FoldableVFRL rl row) => Foldable (VariantF row)
(RowToList row rl, TraversableVFRL rl row) => Traversable (VariantF row)
(RowToList r rl, VariantTags rl, VariantFShows rl a, Show a) => Show (VariantF r a)
#onMatch Source
onMatch :: forall b a r3 r2 r1 r rl. RowToList r rl => VariantFMatchCases rl r1 a b => Union r1 r2 r3 => Record r -> (VariantF r2 a -> b) -> VariantF r3 a -> b
Match a VariantF
with a Record
containing functions for handling cases.
This is similar to on
, except instead of providing a single label and
handler, you can provide a record where each field maps to a particular
VariantF
case.
onMatch
{ foo: \foo -> "Foo: " <> maybe "nothing" id foo
, bar: \bar -> "Bar: " <> snd bar
}
Polymorphic functions in records (such as show
or id
) can lead
to inference issues if not all polymorphic variables are specified
in usage. When in doubt, label methods with specific types, such as
show :: Int -> String
, or give the whole record an appropriate type.
#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)
#match Source
match :: forall b a r2 r1 r rl. RowToList r rl => VariantFMatchCases rl r1 a b => Union r1 () r2 => Record r -> VariantF r2 a -> b
Combinator for exhaustive pattern matching using an onMatch
case record.
matchFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
matchFn = match
{ foo: \foo -> "Foo: " <> maybe "nothing" show foo
, bar: \bar -> "Bar: " <> show (snd bar)
, 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))
#contract Source
contract :: forall a f gt lt. Alternative f => Contractable gt lt => VariantF gt a -> f (VariantF lt a)
A VariantF gt a
can be cast to some VariantF lt a
, where lt
is is a subset
of gt
, as long as there is proof that the VariantF
's runtime tag is
within the subset of lt
.
#unvariantF Source
unvariantF :: forall a r. VariantF r a -> UnvariantF r a
A low-level eliminator which reifies the IsSymbol
, Cons
and
Functor
constraints require to reconstruct the Variant. This
lets you work generically with some VariantF at runtime.
#revariantF Source
revariantF :: forall a r. UnvariantF r a -> VariantF r a
Reconstructs a VariantF given an UnvariantF eliminator.
#VariantFShows Source
class VariantFShows (rl :: RowList) x where
Members
variantFShows :: RLProxy rl -> Proxy x -> List (VariantCase -> String)
Instances
VariantFShows Nil x
(VariantFShows rs x, TypeEquals a (FProxy f), Show (f x), Show x) => VariantFShows (Cons sym a rs) x
#TraversableVFRL Source
class (FoldableVFRL rl row) <= TraversableVFRL (rl :: RowList) (row :: Row Type) | rl -> row where
Members
traverseVFRL :: forall b a f. Applicative f => RLProxy rl -> (a -> f b) -> VariantF row a -> f (VariantF row b)
Instances
TraversableVFRL Nil ()
(IsSymbol k, Traversable f, TraversableVFRL rl r, Cons k (FProxy f) r r', Union r rx r') => TraversableVFRL (Cons k (FProxy f) rl) r'
#FoldableVFRL Source
class FoldableVFRL (rl :: RowList) (row :: Row Type) | rl -> row where
Members
foldrVFRL :: forall b a. RLProxy rl -> (a -> b -> b) -> b -> VariantF row a -> b
foldlVFRL :: forall b a. RLProxy rl -> (b -> a -> b) -> b -> VariantF row a -> b
foldMapVFRL :: forall m a. Monoid m => RLProxy rl -> (a -> m) -> VariantF row a -> m
Instances
FoldableVFRL Nil ()
(IsSymbol k, Foldable f, FoldableVFRL rl r, Cons k (FProxy f) r r') => FoldableVFRL (Cons k (FProxy f) rl) r'
Re-exports from Data.Symbol
Re-exports from Data.Variant.Internal
#Contractable Source
class Contractable gt lt
Instances
(RowToList lt ltl, Union lt a gt, VariantTags ltl) => Contractable gt lt
#VariantFMatchCases Source
class VariantFMatchCases (rl :: RowList) (vo :: Row Type) a b | rl -> vo a b
Instances
(VariantFMatchCases rl vo' a b, Cons sym (FProxy f) vo' vo, TypeEquals k (f a -> b)) => VariantFMatchCases (Cons sym k rl) vo a b
VariantFMatchCases Nil () a b