Skip to content

Instantly share code, notes, and snippets.

@ChrisPenner
Created October 1, 2019 04:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ChrisPenner/ecc365d1cde2efaac0cfb164eed03f49 to your computer and use it in GitHub Desktop.
Cofree animation
module Lib where
import Control.Comonad.Cofree
import Control.Monad.Reader
import Control.Concurrent
drawBar :: Float -> IO ()
drawBar n = putStrLn (replicate (ceiling n) '#')
type Animation = Cofree (Reader Float) Float
animation :: Cofree (Reader Float) Float
animation = unfold go 0
where
go :: Float -> (Float, Reader Float Float)
go n = ((1 + sin n) * 10, asks (+ n))
loop :: Animation -> IO ()
loop (a :< next) = do
drawBar a
threadDelay 10000
loop (runReader next 0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment