-
-
Save natefaubion/365396bb83b582cd857e413cc2f963f5 to your computer and use it in GitHub Desktop.
Record projection
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
-- @inline Heterogeneous.Folding.hfoldlWithIndexRowListCons always | |
-- @inline Heterogeneous.Folding.hfoldlWithIndex always | |
-- @inline export foldingWithIndexPrj always | |
module Main where | |
import Prelude | |
import Data.Symbol (class IsSymbol) | |
import Heterogeneous.Folding (class FoldingWithIndex, class HFoldlWithIndex, hfoldlWithIndex) | |
import Prim.Row as Row | |
import Prim.RowList as RowList | |
import Record as Record | |
import Type.Proxy (Proxy(..)) | |
data Prj r = Prj r | |
instance foldingWithIndexPrj :: | |
( IsSymbol sym | |
, Row.Lacks sym acc1 | |
, Row.Cons sym ty acc1 acc2 | |
, Row.Cons sym ty inp' inp | |
) => | |
FoldingWithIndex (Prj { | inp }) (Proxy sym) { | acc1 } (Proxy ty) { | acc2 } where | |
foldingWithIndex (Prj inp) sym acc _ = | |
Record.insert sym (Record.get sym inp) acc | |
class Project a b where | |
prj :: a -> b | |
instance projectRecord :: | |
( RowList.RowToList r2 rl | |
, HFoldlWithIndex (Prj { | r1 }) {} (Proxy rl) { | r2 } | |
) => | |
Project { | r1 } { | r2 } where | |
prj inp = hfoldlWithIndex (Prj inp) {} (Proxy :: Proxy rl) | |
test | |
:: { foo :: Int, bar :: String, baz :: Boolean } | |
-> _ | |
test wat = prj wat :: { foo :: _, baz :: _ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment