Created
March 21, 2013 14:51
-
-
Save tokiwoousaka/5213644 to your computer and use it in GitHub Desktop.
Control.Lensの再発明
This file contains 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
module Main where | |
import Data.Functor.Identity | |
infixr 4 .~ | |
infixl 1 & | |
instance Show a => Show (Identity a) where | |
show x = "Identity " ++ (show $ runIdentity x) | |
type Setter s t a b = (a -> Identity b) -> s -> Identity t | |
over :: Setter s t a b -> (a -> b) -> s -> t | |
over l f = runIdentity . l (Identity . f) | |
_1 :: Setter (a, v) (b, v) a b | |
_1 f (x, y) = Identity (runIdentity . f $ x, y) | |
_2 :: Setter (v, a) (v, b) a b | |
_2 f (x, y) = Identity (x, runIdentity . f $ y) | |
(&) = flip ($) | |
a .~ v = (over a) (const v) | |
main = let | |
v = (("Hello", ()), "World")&_1._2.~"Lens" | |
in print v -- => (("Hello","Lens"),"World") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あと、Getterも再実装しなくては。
fmapDefaultからこの実装にたどり着くのがスゲーよekmett