Permalink
Browse files

Switched from `Simple` in various places. IndexPreserving setters for…

… set types.
  • Loading branch information...
1 parent d6fa424 commit bf06716744197c3b1b641b32b39968c6f9cf7ce6 @ekmett committed Jan 1, 2013
Showing with 41 additions and 34 deletions.
  1. +2 −2 lens.cabal
  2. +1 −1 src/Control/Lens.hs
  3. +1 −1 src/Control/Lens/Setter.hs
  4. +12 −12 src/Data/HashSet/Lens.hs
  5. +10 −7 src/Data/IntSet/Lens.hs
  6. +13 −9 src/Data/Set/Lens.hs
  7. +2 −2 src/Data/Typeable/Lens.hs
View
@@ -61,7 +61,7 @@ description:
.
You can define lenses such as
.
- > -- bar :: Simple Lens (Foo a) Int
+ > -- bar :: Lens' (Foo a) Int
> bar :: Functor f => (Int -> f Int) -> Foo a -> f (Foo a)
> bar f (Foo a b c) = fmap (\a' -> Foo a' b c) (f a)
.
@@ -73,7 +73,7 @@ description:
.
And you can define a traversal of multiple fields with 'Control.Applicative.Applicative':
.
- > -- traverseBarAndBaz :: Simple Traversal (Foo a) Int
+ > -- traverseBarAndBaz :: Traversal' (Foo a) Int
> traverseBarAndBaz :: Applicative f => (Int -> f Int) -> Foo a -> f (Foo a)
> traverseBarAndBaz f (Foo a b c) = Foo <$> f a <*> f b <*> pure c
.
View
@@ -21,7 +21,7 @@
-- This defines the following lenses:
--
-- @
--- fooArgs :: 'Simple' 'Lens' (Foo a) ['String']
+-- fooArgs :: 'Lens'' (Foo a) ['String']
-- fooValue :: 'Lens' (Foo a) (Foo b) a b
-- @
--
@@ -35,7 +35,7 @@ module Control.Lens.Setter
, ASetter, ASetter'
, AnIndexedSetter, AnIndexedSetter'
-- * Building Setters
- , sets
+ , sets, setting
, cloneSetter
, cloneIndexPreservingSetter
, cloneIndexedSetter
View
@@ -18,8 +18,9 @@ module Data.HashSet.Lens
, setOf
) where
-import Control.Lens.Getter
-import Control.Lens.Setter
+import Control.Lens.Getter (Getting, views)
+import Control.Lens.Setter (setting)
+import Control.Lens.Type
import Data.HashSet as HashSet
import Data.Hashable
@@ -29,21 +30,20 @@ import Data.Hashable
-- | This 'Setter' can be used to change the type of a 'HashSet' by mapping
-- the elements to new values.
--
--- Sadly, you can't create a valid 'Control.Lens.Traversal.Traversal' for a 'Set', but you can
--- manipulate it by reading using 'folded' and reindexing it via 'setmapped'.
---
-setmapped :: (Eq i, Hashable i, Eq j, Hashable j) => Setter (HashSet i) (HashSet j) i j
-setmapped = sets HashSet.map
+-- Sadly, you can't create a valid 'Traversal' for a 'Set', but you can
+-- manipulate it by reading using 'Control.Lens.Fold.folded' and reindexing it via 'setmapped'.
+setmapped :: (Eq i, Hashable i, Eq j, Hashable j) => IndexPreservingSetter (HashSet i) (HashSet j) i j
+setmapped = setting HashSet.map
{-# INLINE setmapped #-}
-- | Construct a set from a 'Getter', 'Control.Lens.Fold.Fold', 'Control.Lens.Traversal.Traversal', 'Control.Lens.Lens.Lens' or 'Control.Lens.Iso.Iso'.
--
-- @
--- 'setOf' :: 'Hashable' a => 'Getter' s a -> s -> 'HashSet' a
--- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Control.Lens.Fold.Fold' s a -> s -> 'HashSet' a
--- 'setOf' :: 'Hashable' a => 'Control.Lens.Lens.Simple' 'Control.Lens.Iso.Iso' s a -> s -> 'HashSet' a
--- 'setOf' :: 'Hashable' a => 'Control.Lens.Lens.Simple' 'Control.Lens.Lens.Lens' s a -> s -> 'HashSet' a
--- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Control.Lens.Lens.Simple' 'Control.Lens.Traversal.Traversal' s a -> s -> 'HashSet' a
+-- 'setOf' :: 'Hashable' a => 'Getter' s a -> s -> 'HashSet' a
+-- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Fold' s a -> s -> 'HashSet' a
+-- 'setOf' :: 'Hashable' a => 'Iso'' s a -> s -> 'HashSet' a
+-- 'setOf' :: 'Hashable' a => 'Lens'' s a -> s -> 'HashSet' a
+-- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Traversal'' s a -> s -> 'HashSet' a
-- @
setOf :: Hashable a => Getting (HashSet a) s t a b -> s -> HashSet a
setOf l = views l HashSet.singleton
View
@@ -38,21 +38,24 @@ members = folding IntSet.toAscList
--
-- >>> over setmapped (+1) (fromList [1,2,3,4])
-- fromList [2,3,4,5]
-setmapped :: Setter' IntSet Int
-setmapped = sets IntSet.map
+setmapped :: IndexPreservingSetter' IntSet Int
+setmapped = setting IntSet.map
{-# INLINE setmapped #-}
-- | Construct an 'IntSet' from a 'Getter', 'Fold', 'Traversal', 'Lens' or 'Iso'.
--
+-- >>> setOf folded [1,2,3,4]
+-- fromList [1,2,3,4]
+--
-- >>> setOf (folded._2) [("hello",1),("world",2),("!!!",3)]
-- fromList [1,2,3]
--
-- @
--- 'setOf' :: 'Getter' s 'Int' -> s -> 'IntSet'
--- 'setOf' :: 'Fold' s 'Int' -> s -> 'IntSet'
--- 'setOf' :: 'Simple' 'Iso' s 'Int' -> s -> 'IntSet'
--- 'setOf' :: 'Simple' 'Lens' s 'Int' -> s -> 'IntSet'
--- 'setOf' :: 'Simple' 'Traversal' s 'Int' -> s -> 'IntSet'
+-- 'setOf' :: 'Getter' s 'Int' -> s -> 'IntSet'
+-- 'setOf' :: 'Fold' s 'Int' -> s -> 'IntSet'
+-- 'setOf' :: 'Iso'' s 'Int' -> s -> 'IntSet'
+-- 'setOf' :: 'Lens'' s 'Int' -> s -> 'IntSet'
+-- 'setOf' :: 'Traversal'' s 'Int' -> s -> 'IntSet'
-- @
setOf :: Getting IntSet s t Int b -> s -> IntSet
setOf l = views l IntSet.singleton
View
@@ -15,7 +15,8 @@ module Data.Set.Lens
) where
import Control.Lens.Getter ( Getting, views )
-import Control.Lens.Setter ( Setter, sets )
+import Control.Lens.Setter ( setting )
+import Control.Lens.Type
import Data.Set as Set
-- $setup
@@ -24,26 +25,29 @@ import Data.Set as Set
-- | This 'Setter' can be used to change the type of a 'Set' by mapping
-- the elements to new values.
--
--- Sadly, you can't create a valid 'Control.Lens.Traversal.Traversal' for a 'Set', but you can
+-- Sadly, you can't create a valid 'Traversal' for a 'Set', but you can
-- manipulate it by reading using 'Control.Lens.Fold.folded' and reindexing it via 'setmapped'.
--
-- >>> over setmapped (+1) (fromList [1,2,3,4])
-- fromList [2,3,4,5]
-setmapped :: (Ord i, Ord j) => Setter (Set i) (Set j) i j
-setmapped = sets Set.map
+setmapped :: (Ord i, Ord j) => IndexPreservingSetter (Set i) (Set j) i j
+setmapped = setting Set.map
{-# INLINE setmapped #-}
-- | Construct a set from a 'Getter', 'Control.Lens.Fold.Fold', 'Control.Lens.Traversal.Traversal', 'Control.Lens.Lens.Lens' or 'Control.Lens.Iso.Iso'.
--
+-- >>> setOf folded ["hello","world"]
+-- fromList ["hello","world"]
+--
-- >>> setOf (folded._2) [("hello",1),("world",2),("!!!",3)]
-- fromList [1,2,3]
--
-- @
--- 'setOf' :: 'Getter' s a -> s -> 'Set' a
--- 'setOf' :: 'Ord' a => 'Control.Lens.Fold.Fold' s a -> s -> 'Set' a
--- 'setOf' :: 'Control.Lens.Lens.Simple' 'Control.Lens.Iso.Iso' s a -> s -> 'Set' a
--- 'setOf' :: 'Control.Lens.Lens.Simple' 'Control.Lens.Lens.Lens' s a -> s -> 'Set' a
--- 'setOf' :: 'Ord' a => 'Control.Lens.Lens.Simple' 'Control.Lens.Traversal.Traversal' s a -> s -> 'Set' a
+-- 'setOf' :: 'Getter' s a -> s -> 'Set' a
+-- 'setOf' :: 'Ord' a => 'Fold' s a -> s -> 'Set' a
+-- 'setOf' :: 'Iso'' s a -> s -> 'Set' a
+-- 'setOf' :: 'Lens'' s a -> s -> 'Set' a
+-- 'setOf' :: 'Ord' a => 'Traversal'' s a -> s -> 'Set' a
-- @
setOf :: Getting (Set a) s t a b -> s -> Set a
setOf l = views l Set.singleton
@@ -23,14 +23,14 @@ import Control.Lens
import Data.Typeable
import Data.Maybe
--- | A 'Simple' 'Traversal' for working with a 'cast' of a 'Typeable' value.
+-- | A 'Traversal'' for working with a 'cast' of a 'Typeable' value.
_cast :: (Typeable s, Typeable a) => Traversal' s a
_cast f s = case cast s of
Just a -> fromMaybe (error "_cast: recast failed") . cast <$> f a
Nothing -> pure s
{-# INLINE _cast #-}
--- | A 'Simple' 'Traversal' for working with a 'gcast' of a 'Typeable' value.
+-- | A 'Traversal'' for working with a 'gcast' of a 'Typeable' value.
_gcast :: (Typeable s, Typeable a) => Traversal' (c s) (c a)
_gcast f s = case gcast s of
Just a -> fromMaybe (error "_gcast: recast failed") . gcast <$> f a

0 comments on commit bf06716

Please sign in to comment.