purescript / purescript-foreign-object

Functions for working with homogeneous JavaScript objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide a builder

paluh opened this issue · comments

commented

Could you please tell me if you think that trivial builder like the below could be a good addition to the lib?

import Prelude

import Foreign.Object (Object)
import Record.Unsafe (unsafeSet) as Record.Unsafe
import Unsafe.Coerce (unsafeCoerce)

newtype Builder a = Builder (Object a  Object a)

instance semigroupBuilderSemigroup (Builder a) where
  append (Builder b1) (Builder b2) = Builder (b1 <<< b2)

instance monoidBuilderMonoid (Builder a) where
  mempty = Builder identity

insert   a. String  a  Builder a
insert k v = unsafeCoerce (Record.Unsafe.unsafeSet k v)

build   a. Builder a  Object a
build (Builder b) = b (unsafeCoerce {})

Of course this kind of builder is not stack safe (as @garyb pointed out under nearly identical issue here: purescript/purescript-arrays#162 :-)).