Created
April 4, 2014 20:03
-
-
Save lambdaknight/9982115 to your computer and use it in GitHub Desktop.
Lens-like Access to Dictionary-like Object
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 AList where | |
import Control.Lens | |
type AList t = [(String, t)] | |
alistGet :: String -> AList t -> Maybe t | |
alistGet key ((k,v):xs) | |
| k == key = Just v | |
| otherwise = alistGet key xs | |
alistGet _ [] = Nothing | |
alistSet :: String -> AList t -> t -> AList t | |
alistSet key ((k,v):xs) value | |
| k == key = (key, value):xs | |
| otherwise = (k,v):(alistSet key xs value) | |
alistSet key [] value = [(key, value)] | |
-- Bad Lens. Violates Lens rules 1 and 2. | |
alistLens :: String -> Lens (AList t) (AList t) (Maybe t) t | |
alistLens key = lens (alistGet key) (alistSet key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment