Skip to content

Instantly share code, notes, and snippets.

@JordanMartinez
Created September 7, 2022 15:54
Show Gist options
  • Save JordanMartinez/3d4bd8a8ab632715b9c316f45ddf6165 to your computer and use it in GitHub Desktop.
Save JordanMartinez/3d4bd8a8ab632715b9c316f45ddf6165 to your computer and use it in GitHub Desktop.
Folding over record of records - compiler bug?
module HeterogeneousLog.Main where
import Prelude
import Data.Symbol (class IsSymbol)
import Effect (Effect)
import Effect.Console (log)
import Heterogeneous.Folding (class FoldingWithIndex, class HFoldlWithIndex, hfoldlWithIndex)
import Prim.Row as Row
import Record as Record
import Type.Proxy (Proxy)
main :: Effect Unit
main = do
demoFoldingWithIndex
printSection :: String -> Effect Unit -> Effect Unit
printSection header runSection = do
log "======================"
log header
log "======================"
runSection
log ""
--------------------------------------------------------------------
demoFoldingWithIndex :: Effect Unit
demoFoldingWithIndex = do
printSection "FoldingWithIndex" do
log $ show $ isDirtyRec { foo: { initialValue: 1, value: 2 }, bar: { initialValue: "x", value: "y" } }
isDirty :: forall a row. Eq a => Boolean -> { value :: a, initialValue :: a | row } -> Boolean
isDirty b { value, initialValue } = b || value /= initialValue
data DirtyProps = DirtyProps
instance
( Eq a
, Row.Cons sym { value :: a, initialValue :: a | tail } other row
, IsSymbol sym
) =>
FoldingWithIndex DirtyProps (Proxy sym) Boolean (Record row) Boolean where
foldingWithIndex DirtyProps _prop b a =
isDirty b (Record.get _prop a)
isDirtyRec
:: forall r
. HFoldlWithIndex DirtyProps Boolean { | r } Boolean
=> { | r }
-> Boolean
isDirtyRec r = hfoldlWithIndex DirtyProps false r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment