Skip to content

Instantly share code, notes, and snippets.

@timjb
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save timjb/4958411d2a7a306fad95 to your computer and use it in GitHub Desktop.

Select an option

Save timjb/4958411d2a7a306fad95 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
module TraversalIso where
import Control.Lens.Traversal
import Control.Monad.State.Lazy
import Data.Traversable (Traversable (..))
{-
type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t
type Traversal' s a = Traversal s s a a
-}
type SimpleTraversal' s a =
forall r. r -> (a -> r -> (a,r)) -> s -> (s,r)
iso :: Traversal' s a -> SimpleTraversal' s a
iso trav init step s = runState (trav (state . step) s) init
osi :: SimpleTraversal' s a -> Traversal' s a
osi simpleTrav f s =
let (_, bs) = simpleTrav [] (\a xs -> (a, f a : xs)) s
in fmap (\bs' -> fst (simpleTrav bs' (\_ (b:bs) -> (b, bs)) s))
(sequenceA (reverse bs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment