Skip to content

Instantly share code, notes, and snippets.

@nsaunders
Created July 13, 2023 03:24
Show Gist options
  • Save nsaunders/9014de19386409c373668ced58968ecd to your computer and use it in GitHub Desktop.
Save nsaunders/9014de19386409c373668ced58968ecd to your computer and use it in GitHub Desktop.
applyRecord in PureScript
module Record.Apply where
import Prelude
import Data.Symbol (class IsSymbol)
import Prim.Row as Row
import Prim.RowList (class RowToList, RowList)
import Prim.RowList as RL
import Record as Record
import Record.Builder as RB
import Type.Proxy (Proxy(..))
applyRecord
:: forall fl xl f x o
. RowToList f fl
=> RowToList x xl
=> ApplyRecord fl xl f x () o
=> { | f }
-> { | x }
-> { | o }
applyRecord f x =
RB.buildFromScratch $
applyRecordImpl (Proxy :: _ fl) (Proxy :: _ xl) f x
class ApplyRecord (fl :: RowList Type) (xl :: RowList Type) (f :: Row Type) (x :: Row Type) (i :: Row Type) (o :: Row Type) | fl xl -> o where
applyRecordImpl :: Proxy fl -> Proxy xl -> { | f } -> { | x } -> RB.Builder { | i } { | o }
instance
( IsSymbol label
, Row.Cons label (xv -> ov) ft f
, Row.Cons label xv xt x
, Row.Cons label ov ot o
, Row.Lacks label ot
, ApplyRecord flt xlt f x i ot
) =>
ApplyRecord (RL.Cons label (xv -> ov) flt) (RL.Cons label xv xlt) f x i o where
applyRecordImpl _ _ fs xs =
let
key = Proxy :: _ label
f = Record.get key fs
x = Record.get key xs
tail = applyRecordImpl (Proxy :: _ flt) (Proxy :: _ xlt) fs xs
in
RB.insert key (f x) <<< tail
instance ApplyRecord RL.Nil RL.Nil f x o o where
applyRecordImpl _ _ _ _ = RB.union {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment