Skip to content

Instantly share code, notes, and snippets.

@thomashoneyman
Last active December 3, 2021 03:20
Show Gist options
  • Save thomashoneyman/6c186128fd7038f2366f3f181491c105 to your computer and use it in GitHub Desktop.
Save thomashoneyman/6c186128fd7038f2366f3f181491c105 to your computer and use it in GitHub Desktop.
Emit
module Main where
import Prelude
import Halogen.Subscription (Emitter)
import Run (Run)
import Run as Run
import Type.Proxy (Proxy(..))
import Type.Row (type (+))
import Unsafe.Coerce (unsafeCoerce)
data Emit' s s' a
= Get (s -> a)
| Emit (s -> s') (s' -> s' -> Boolean) (Emitter s' -> a)
data Emit :: Type -> Type -> Type
data Emit s a
instance Functor (Emit s) where
map f =
unEmit \emitF -> mkEmit $ case emitF of
Get k -> Get (k >>> f)
Emit pick eq k -> Emit pick eq (k >>> f)
mkEmit :: forall s s' a. Emit' s s' a -> Emit s a
mkEmit = unsafeCoerce
unEmit :: forall s a r. (forall s'. Emit' s s' a -> r) -> Emit s a -> r
unEmit = unsafeCoerce
type EMIT s r = (emit :: Emit s | r)
_emit :: Proxy "emit"
_emit = Proxy
get :: forall s r. Run (EMIT s + r) s
get = Run.lift _emit (mkEmit (Get identity))
emit :: forall s s' r. (s -> s') -> (s' -> s' -> Boolean) -> Run (EMIT s + r) (Emitter s')
emit pick eq = Run.lift _emit (mkEmit (Emit pick eq identity))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment