Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

correct examples and include as separate file

  • Loading branch information...
commit 1b2305e432fc967b5422a00cc86ac92d6bb5d431 1 parent 51291bb
@dag authored
Showing with 49 additions and 9 deletions.
  1. +17 −8 Data/Lens/IxSet.hs
  2. +1 −1  data-lens-ixset.cabal
  3. +31 −0 example.hs
View
25 Data/Lens/IxSet.hs
@@ -20,14 +20,23 @@ We can now work with indices using lenses and fix Simon's last name:
people\' = 'ixLens' (FirstName \"Simon\") '^%=' 'fmap' (lastName '^=' LastName \"Peyton-Jones\") '$' people
@
+>>> people'
+fromList [Person {_firstName = FirstName "Edward A.", _lastName = LastName "Kmett"}
+ ,Person {_firstName = FirstName "Simon", _lastName = LastName "Peyton-Jones"}]
+>>> (firstName ^$) <$> people' ^. ixLens (LastName "Peyton-Jones")
+Just (FirstName "Simon")
+>>> ixLens (LastName "Peyton-Jones") ^$ people
+Nothing
+
Perhaps more commonly you're working with an 'IxSet' from inside a state
-monad such as with acid-state. In that case usage is even easier:
+monad such as @Update@ from the @acid-state@ package. In that case usage
+is even easier:
@
changeLastName = 'ixLens' (FirstName \"Simon\") '%=' 'fmap' (lastName '^=' LastName \"Peyton-Jones\")
@
-Here's the missing boilerplate:
+Here's the missing boilerplate, which also needs the packages @data-lens-fd@ and @data-lens-template@:
@
\{\-\# LANGUAGE DeriveDataTypeable \#\-\}
@@ -39,18 +48,18 @@ import Data.Lens
import Data.Lens.IxSet
import Data.Lens.Template
-data Person = Person { _firstName :: FirstName
- , _lastName :: LastName
- } deriving ('Show', 'Eq', 'Ord', 'Data', 'Typeable')
-
-mkLens ''Person
-
newtype FirstName = FirstName 'String'
deriving ('Show', 'Eq', 'Ord', 'Data', 'Typeable')
newtype LastName = LastName 'String'
deriving ('Show', 'Eq', 'Ord', 'Data', 'Typeable')
+data Person = Person { _firstName :: FirstName
+ , _lastName :: LastName
+ } deriving ('Show', 'Eq', 'Ord', 'Data', 'Typeable')
+
+makeLens ''Person
+
instance 'Indexable' Person where
empty = 'ixSet' [ 'ixGen' ('Proxy' :: 'Proxy' FirstName)
, 'ixGen' ('Proxy' :: 'Proxy' LastName)
View
2  data-lens-ixset.cabal
@@ -1,5 +1,5 @@
Name : data-lens-ixset
-Version : 0.1.3
+Version : 0.1.4
Synopsis : A Lens for IxSet
Description : Integrates Data.IxSet with Data.Lens.
Homepage : https://github.com/dag/data-lens-ixset
View
31 example.hs
@@ -0,0 +1,31 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE TemplateHaskell #-}
+
+import Data.Data
+import Data.IxSet
+import Data.Lens
+import Data.Lens.IxSet
+import Data.Lens.Template
+
+newtype FirstName = FirstName String
+ deriving (Show, Eq, Ord, Data, Typeable)
+
+newtype LastName = LastName String
+ deriving (Show, Eq, Ord, Data, Typeable)
+
+data Person = Person { _firstName :: FirstName
+ , _lastName :: LastName
+ } deriving (Show, Eq, Ord, Data, Typeable)
+
+makeLens ''Person
+
+instance Indexable Person where
+ empty = ixSet [ ixGen (Proxy :: Proxy FirstName)
+ , ixGen (Proxy :: Proxy LastName)
+ ]
+
+people = fromList [ Person (FirstName "Edward A.") (LastName "Kmett")
+ , Person (FirstName "Simon") (LastName "P. Jones")
+ ]
+
+people' = ixLens (FirstName "Simon") ^%= fmap (lastName ^= LastName "Peyton-Jones") $ people
Please sign in to comment.
Something went wrong with that request. Please try again.