Created
August 12, 2018 19:41
-
-
Save paldepind/6ec923b94c6aab6915536580d6f0c466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Experiment | |
( mapHeterogenousRecord | |
, mapRecordBuilder | |
, class MapRecord | |
) where | |
import Prelude | |
import Prim.Row as Row | |
import Prim.RowList as RL | |
import Record as R | |
import Record.Builder (Builder) | |
import Record.Builder as Builder | |
import Data.Maybe (Maybe(..)) | |
import Data.Symbol (class IsSymbol, SProxy(SProxy)) | |
import Type.Data.RowList (RLProxy(RLProxy)) | |
mapHeterogenousRecord :: forall row xs f row' | |
. RL.RowToList row xs | |
=> MapRecord xs row f () row' | |
=> (forall a. a -> f a) | |
-> Record row | |
-> Record row' | |
mapHeterogenousRecord f r = Builder.build builder {} | |
where | |
builder = mapRecordBuilder (RLProxy :: RLProxy xs) f r | |
class MapRecord (xs :: RL.RowList) (row :: # Type) f (from :: # Type) (to :: # Type) | |
| xs -> row f from to where | |
mapRecordBuilder :: RLProxy xs -> (forall a. a -> f a) -> Record row -> Builder { | from } { | to } | |
instance mapRecordCons :: | |
( IsSymbol name -- name is a symbol | |
, Row.Cons name a trash row -- `name` and `a` is the head of `row` | |
, MapRecord tail row f from from' -- recursive invocation | |
, Row.Lacks name from' | |
, Row.Cons name (f a) from' to | |
) => MapRecord (RL.Cons name a tail) row f from to where | |
mapRecordBuilder _ f r = | |
first <<< rest | |
where | |
nameP = SProxy :: SProxy name | |
val = f $ R.get nameP r | |
rest = mapRecordBuilder (RLProxy :: RLProxy tail) f r | |
first = Builder.insert nameP val | |
instance mapRecordNil :: MapRecord RL.Nil row f () () where | |
mapRecordBuilder _ _ _ = identity | |
object = { foo: 1, bar: "baz" } | |
mapped = mapHeterogenousRecord Just object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> mapHeterogenousRecord pure { foo: 1, bar: "baz" } :: { foo :: Maybe Int, bar :: Maybe String } | |
{ foo: (Just 1), bar: (Just "baz") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment