Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Last active October 8, 2018 17:29
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 throughnothing/1b9fff2e254e4d6df1e19b04c11f980f to your computer and use it in GitHub Desktop.
Save throughnothing/1b9fff2e254e4d6df1e19b04c11f980f to your computer and use it in GitHub Desktop.
Loeb in Purescript
module Loeb where
import Prelude
import Data.Lazy (Lazy, defer, force)
import Data.Array ((!!))
import Data.Maybe (fromMaybe)
loeb :: ∀ f a. Functor f => f (f (Lazy a) -> a) -> f (Lazy a)
loeb x = go where go = map (\y -> defer (\_ -> y go)) x
arr :: Array (Array (Lazy Int) -> Int)
arr = [ const 1, f 0, f 1, f 2 ]
where f = \n -> (\x -> fromMaybe 0 (map ((add 1) <<< force) (x !! n)))
-- | Output:
-- | [(defer \_ -> 1),(defer \_ -> 2),(defer \_ -> 3),(defer \_ -> 4)]
run :: Array (Lazy Int)
run = loeb arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment