Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module shows you how to fold a Record
.
These utilities are based on recordToFieldList
, which converts
a Record
into a SomeFieldWithValue
. Then, foldRecord
unpacks that
GADT for you, which allows you to know the type of the field and combine
them.
Since: 0.0.3.0
Synopsis
- data SomeFieldWithValue rec where
- SomeFieldWithValue :: Field rec a -> a -> SomeFieldWithValue rec
- recordToFieldList :: forall rec. Record rec => rec -> [SomeFieldWithValue rec]
- foldRecord :: forall rec r. Record rec => (forall ty. ty -> r -> Field rec ty -> r) -> r -> rec -> r
- foldMRecord :: forall rec m r. (Record rec, Monad m) => (forall ty. ty -> r -> Field rec ty -> m r) -> r -> rec -> m r
- foldMapRecord :: forall rec m. (Record rec, Monoid m) => (forall ty. ty -> Field rec ty -> m) -> rec -> m
Documentation
data SomeFieldWithValue rec where Source #
A datatype containing a Field
along with a value for that field.
Since: 0.0.3.0
SomeFieldWithValue :: Field rec a -> a -> SomeFieldWithValue rec |
recordToFieldList :: forall rec. Record rec => rec -> [SomeFieldWithValue rec] Source #
Convert a Record
into a list of the records fields paired with the
value for that record.
Since: 0.0.3.0
foldRecord :: forall rec r. Record rec => (forall ty. ty -> r -> Field rec ty -> r) -> r -> rec -> r Source #
Fold over the fields of a record to produce a final result.
The function parameter accepts the
, a value in the
record, and the accumulator. Example:Field
rec ty
foldRecord
(\ val acc field ->
case field of
UserName ->
length val + acc
UserAge ->
val + acc
)
0
User { userName = "Matt", userAge = 35 }
The paramater list is given to enable LambdaCase
nicety:
foldRecord
(\val acc -> \case
UserName ->
length val + acc
UserAge ->
val + acc
)
Since: 0.0.3.0