Skip to content

Instantly share code, notes, and snippets.

@techtangents
Created March 9, 2013 13:44
Show Gist options
  • Save techtangents/5124206 to your computer and use it in GitHub Desktop.
Save techtangents/5124206 to your computer and use it in GitHub Desktop.
A cycling Automaton in Elm. This Automaton produces values off a non-empty list. When the list is exhausted, it starts again. The input signal is just used as a source of events - its values are ignored.
-- non-empty list
data NonEmpty x = NonEmpty x [x]
-- neFromList :: [x] -> NonEmpty x
neFromList list = case list of [] -> Maybe.Nothing
(x:xs) -> Maybe.Just (NonEmpty x xs)
-- neFromListOr :: NonEmpty x -> [x] -> NonEmpty x
neFromListOr other qs = maybeOr other (neFromList xs)
-- maybeOr :: a -> Maybe a -> a
maybeOr other = Maybe.maybe other id
-- init'' :: s -> (s -> (o,s)) -> Automaton i o
init'' i f = Automaton.init' i (\_ x -> f x)
-- cycler :: NonEmpty a -> Automaton z a
cycler list = init'' list (\(NonEmpty x xs) -> (x, maybeOr list (neFromList xs)))
list = NonEmpty 1 [4, 9, 12]
main = asText <~ (Automaton.run (cycler list) (every second))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment