This repository was archived by the owner on Oct 4, 2020. It is now read-only.
Description I thought it would be nice if we could use record-syntax to create Maps. Using the new RowToList mechanism, we can now turn this:
numbers ∷ Map String Number
numbers = fromFoldable
[ Tuple " pi" 3.14159
, Tuple " e" 2.71828
, Tuple " ultimate_answer" 42.0
, Tuple " alpha" 0.00729735
]
into this:
numbers ∷ Map String Number
numbers = fromRecord
{ pi: 3.14159
, e: 2.71828
, ultimate_answer: 42.0
, alpha: 0.00729735
}
The implementation could look like this:
class HomogeneousRecord (row ∷ # Type ) (list ∷ RowList ) a | list → row
where
toMap ∷ RLProxy list → Record row → Map String a
instance homogeneousRecordNil ∷ HomogeneousRecord () Nil a where
toMap _ _ = empty
instance homogeneousRecordCons ∷
( RowToList row list
, IsSymbol l
, RowLacks l row'
, RowCons l a row' row
, RowToList row' list'
, HomogeneousRecord row' list' a
)
⇒ HomogeneousRecord row (Cons l a list' ) a where
toMap _ record = insert key value (toMap (RLProxy ∷ RLProxy list' ) record')
where
keyS = SProxy ∷ SProxy l
key = reflectSymbol keyS
value = R .get keyS record
record' :: Record row'
record' = R .delete keyS record
fromRecord ∷ ∀ row list a
. RowToList row list
⇒ HomogeneousRecord row list a
⇒ Record row
→ Map String a
fromRecord = toMap (RLProxy :: RLProxy list )
Calling fromRecord with a heterogeneous record leads to a compile-time error, as expected.