Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created February 26, 2023 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natefaubion/365396bb83b582cd857e413cc2f963f5 to your computer and use it in GitHub Desktop.
Save natefaubion/365396bb83b582cd857e413cc2f963f5 to your computer and use it in GitHub Desktop.
Record projection
-- @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