Skip to content

Instantly share code, notes, and snippets.

@sarkologist
Created September 2, 2018 08:35
Show Gist options
  • Save sarkologist/416c70268dea986c103cdcab2bb36a47 to your computer and use it in GitHub Desktop.
Save sarkologist/416c70268dea986c103cdcab2bb36a47 to your computer and use it in GitHub Desktop.
λ: let a = stagger [] (arr (1:)) (arr (0:)) in mapM_ print $ scan a (Prelude.replicate 5 ())
([1],[0,1])
([1,0,1],[0,1,0,1])
([1,0,1,0,1],[0,1,0,1,0,1])
([1,0,1,0,1,0,1],[0,1,0,1,0,1,0,1])
([1,0,1,0,1,0,1,0,1],[0,1,0,1,0,1,0,1,0,1])
stagger :: b -> (b -> a) -> (a -> b) -> Scan () (a, b)
stagger start consume determine = Scan (state . step) initial
where
initial = start
step () b =
let a' = consume b
b' = determine a'
in ((a', b'), b')
staggerScan :: b -> (Scan (x, b) a) -> (Scan (y, a) b) -> Scan (x, y) (a, b)
staggerScan start (Scan s sInitial) (Scan t tIinitial) =
Scan (state . step) initial
where
initial = (start, sInitial, tIinitial)
step (x, y) (b, i, j) = let
(a', i') = runState (s (x, b)) i
(b', j') = runState (t (y, a')) j
in ((a', b'), (b', i', j'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment