Guest User

Untitled

a guest
Dec 10th, 2018
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Prelude
  4.  
  5. import Data.Array.NonEmpty (NonEmptyArray, (..))
  6. import Data.Traversable (class Traversable, sequence)
  7. import Effect (Effect)
  8. import Effect.Console (log)
  9. import Prelude as P
  10.  
  11. type Callbag a err = A a err -> Effect Unit
  12.  
  13. data A a err = Subscribe (C a err -> Effect Unit)
  14. data B
  15. = Pull
  16. | Unsubscribe
  17. data C a err
  18. = Talkback (B -> Effect Unit)
  19. | Push a
  20. | End
  21. | Error err
  22.  
  23.  
  24. fromTraversable :: forall t a. Traversable t => t a -> Callbag a Unit
  25. fromTraversable t = \(Subscribe sink) -> do
  26. _ <- sink (Talkback (const $ pure unit))
  27. _ <- sequence $ P.map (sink <<< Push) t
  28. sink End
  29.  
  30. map :: forall a b err. (a -> b) -> Callbag a err -> Callbag b err
  31. map f source = \(Subscribe sink) -> let
  32. talkback sourceTb Pull = sourceTb Pull
  33. talkback sourceTb Unsubscribe = sourceTb Unsubscribe
  34. handler (Talkback tb) = sink (Talkback $ talkback tb)
  35. handler (Push v) = sink (Push $ f v)
  36. handler End = sink End
  37. handler (Error err) = sink (Error err)
  38. in source (Subscribe handler)
  39.  
  40. -- incomplete, pulling requires using mutable refs?
  41. forEach :: forall a err. (a -> Effect Unit) -> Callbag a err -> Effect Unit
  42. forEach f cb = cb $ Subscribe handler
  43. where
  44. handler (Talkback tb) = pure unit
  45. handler (Push v) = f v
  46. handler _ = pure unit
RAW Paste Data