Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created January 11, 2012 23:10
Show Gist options
  • Save sjoerdvisscher/1597375 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/1597375 to your computer and use it in GitHub Desktop.
A zipper for traversables.
import Data.Traversable
import Control.Monad.Free
import Control.Comonad
import Control.Comonad.Trans.Store
type Zipper t a = Free (Store a) (t a)
zipper :: Traversable t => t a -> Zipper t a
zipper = traverse (wrap . store return)
unzipper :: Zipper t a -> t a
unzipper = iter extract
step :: Maybe a -> Zipper t a -> Zipper t a
step _ (Pure t) = Pure t
step ma (Free s) = maybe extract peek ma s
test :: [Int]
test = unzipper . step (Just 10) . step Nothing . step (Just 20) $ zipper [1..10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment