Last active
August 29, 2015 14:16
-
-
Save timjb/4958411d2a7a306fad95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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