Data.Lens
- Package
- purescript-profunctor-lenses
- Repository
- purescript-contrib/purescript-profunctor-lenses
This module re-exports types and functions from other modules:
module Data.Lens.Isomodule Data.Lens.Lensmodule Data.Lens.Prismmodule Data.Lens.Traversalmodule Data.Lens.Typesmodule Data.Lens.Settermodule Data.Lens.Gettermodule Data.Lens.Foldmodule Data.Lens.Common
Re-exports from Data.Lens.Common
Re-exports from Data.Lens.Fold
#sequenceOf_ Source
sequenceOf_ :: forall f s t a b. Applicative f => Fold (Endo Function (f Unit)) s t (f a) b -> s -> f UnitSequence the foci of a Fold, pulling out an Applicative, and ignore
the result. If you need the result, see sequenceOf for Traversals.
#replicated Source
replicated :: forall a b t r. Monoid r => Int -> Fold r a b a tReplicates the elements of a fold.
#productOf Source
productOf :: forall s t a b. Semiring a => Fold (Multiplicative a) s t a b -> s -> aThe product of all foci of a Fold.
#orOf Source
orOf :: forall s t a b. HeytingAlgebra a => Fold (Disj a) s t a b -> s -> aThe disjunction of all foci of a Fold.
#itraverseOf_ Source
itraverseOf_ :: forall i f s t a b r. Applicative f => IndexedFold (Endo Function (f Unit)) i s t a b -> (i -> a -> f r) -> s -> f UnitTraverse the foci of an IndexedFold, discarding the results.
#ifoldrOf Source
ifoldrOf :: forall i s t a b r. IndexedFold (Endo Function r) i s t a b -> (i -> a -> r -> r) -> r -> s -> rRight fold over an IndexedFold.
#ifoldlOf Source
ifoldlOf :: forall i s t a b r. IndexedFold (Dual (Endo Function r)) i s t a b -> (i -> r -> a -> r) -> r -> s -> rLeft fold over an IndexedFold.
#ifoldMapOf Source
ifoldMapOf :: forall r i s t a b. IndexedFold r i s t a b -> (i -> a -> r) -> s -> rFold map over an IndexedFold.
#ianyOf Source
ianyOf :: forall i s t a b r. HeytingAlgebra r => IndexedFold (Disj r) i s t a b -> (i -> a -> r) -> s -> rWhether any focus of an IndexedFold satisfies a predicate.
#iallOf Source
iallOf :: forall i s t a b r. HeytingAlgebra r => IndexedFold (Conj r) i s t a b -> (i -> a -> r) -> s -> rWhether all foci of an IndexedFold satisfy a predicate.
#hasn't Source
hasn't :: forall s t a b r. HeytingAlgebra r => Fold (Conj r) s t a b -> s -> rDetermines whether a Fold does not have a focus.
#has Source
has :: forall s t a b r. HeytingAlgebra r => Fold (Disj r) s t a b -> s -> rDetermines whether a Fold has at least one focus.
#anyOf Source
anyOf :: forall s t a b r. HeytingAlgebra r => Fold (Disj r) s t a b -> (a -> r) -> s -> rWhether any focus of a Fold satisfies a predicate.
#andOf Source
andOf :: forall s t a b. HeytingAlgebra a => Fold (Conj a) s t a b -> s -> aThe conjunction of all foci of a Fold.
#allOf Source
allOf :: forall s t a b r. HeytingAlgebra r => Fold (Conj r) s t a b -> (a -> r) -> s -> rWhether all foci of a Fold satisfy a predicate.
Re-exports from Data.Lens.Getter
#use Source
use :: forall s t a b m. MonadState s m => Getter s t a b -> m aView the focus of a Getter in the state of a monad.
#iview Source
iview :: forall i s t a b. IndexedFold (Tuple i a) i s t a b -> s -> Tuple i aView the focus of a Getter and its index.
#iuse Source
iuse :: forall i s t a b m. MonadState s m => IndexedFold (Tuple i a) i s t a b -> m (Tuple i a)View the focus of a Getter and its index in the state of a monad.
#cloneGetter Source
cloneGetter :: forall s t a b. AGetter s t a b -> Getter s t a bRe-exports from Data.Lens.Grate
#zipFWithOf Source
zipFWithOf :: forall f s t a b. Optic (Costar f) s t a b -> (f a -> b) -> (f s -> t)Re-exports from Data.Lens.Iso
#auf Source
auf :: forall s t a b e r p. Profunctor p => AnIso s t a b -> (p r a -> e -> b) -> p r s -> e -> tRe-exports from Data.Lens.Lens
#lensStore Source
lensStore :: forall s t a b. ALens s t a b -> s -> Tuple a (b -> t)Converts a lens into the form that lens' accepts.
Can be useful when defining a lens where the focus appears under multiple constructors of an algebraic data type. This function would be called for each case of the data type.
For example:
data LensStoreExample = LensStoreA Int | LensStoreB (Tuple Boolean Int)
lensStoreExampleInt :: Lens' LensStoreExample Int
lensStoreExampleInt = lens' case _ of
LensStoreA i -> map LensStoreA <$> lensStore identity i
LensStoreB i -> map LensStoreB <$> lensStore _2 i
#lens Source
lens :: forall s t a b. (s -> a) -> (s -> b -> t) -> Lens s t a bCreate a Lens from a getter/setter pair.
> species = lens _.species $ _ {species = _}
> view species {species : "bovine"}
"bovine"
> _2 = lens Tuple.snd $ \(Tuple keep _) new -> Tuple keep new
Note: _2 is predefined in Data.Lens.Tuple.
Re-exports from Data.Lens.Prism
#prism Source
prism :: forall s t a b. (b -> t) -> (s -> Either t a) -> Prism s t a bCreate a Prism from a constructor and a matcher function that
produces an Either:
solidFocus :: Prism' Fill Color
solidFocus = prism Solid case _ of
Solid color -> Right color
anotherCase -> Left anotherCase
Note: The matcher function returns a result wrapped in Either t
to allow for type-changing prisms in the case where the input does
not match.
#only Source
only :: forall a. Eq a => a -> Prism a a Unit Unitonly focuses not just on a case, but a specific value of that case.
solidWhiteFocus :: Prism' Fill Unit
solidWhiteFocus = only $ Solid Color.white
is solidWhiteFocus (Solid Color.white) == true
preview solidWhiteFocus (Solid Color.white) == Just unit
review solidWhiteFocus unit == Solid Color.white
Note: only depends on Eq. Strange definitions of (==)
(for example, that it counts any Fill as being equal to Solid Color.white)
will create a prism that violates the preview-review law.
#nearly Source
nearly :: forall a. a -> (a -> Boolean) -> Prism' a Unitnearly is a variant of only. Like only, nearly produces
a prism that matches
a single value. Unlike only, it uses a predicate you supply
instead of depending on class Eq:
solidWhiteFocus :: Prism' Fill Unit
solidWhiteFocus = nearly (Solid Color.white) predicate
where
predicate candidate =
color.toHexString == Color.white.toHexString
#isn't Source
isn't :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> rAsk if preview prism would produce a Nothing.
#is Source
is :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> rAsk if preview prism would produce a Just.
#clonePrism Source
clonePrism :: forall s t a b. APrism s t a b -> Prism s t a bRe-exports from Data.Lens.Setter
#subModifying Source
subModifying :: forall s a m. MonadState s m => Ring a => Setter' s a -> a -> m Unit#mulModifying Source
mulModifying :: forall s a m. MonadState s m => Semiring a => Setter' s a -> a -> m Unit#modifying Source
modifying :: forall s a b m. MonadState s m => Setter s s a b -> (a -> b) -> m UnitModify the foci of a Setter in a monadic state.
#iover Source
iover :: forall i s t a b. IndexedSetter i s t a b -> (i -> a -> b) -> s -> tApply a function to the foci of a Setter that may vary with the index.
#divOver Source
divOver :: forall s t a. EuclideanRing a => Setter s t a a -> a -> s -> t#divModifying Source
divModifying :: forall s a m. MonadState s m => EuclideanRing a => Setter' s a -> a -> m Unit#disjOver Source
disjOver :: forall s t a. HeytingAlgebra a => Setter s t a a -> a -> s -> t#disjModifying Source
disjModifying :: forall s a m. MonadState s m => HeytingAlgebra a => Setter' s a -> a -> m Unit#conjOver Source
conjOver :: forall s t a. HeytingAlgebra a => Setter s t a a -> a -> s -> t#conjModifying Source
conjModifying :: forall s a m. MonadState s m => HeytingAlgebra a => Setter' s a -> a -> m Unit#assignJust Source
assignJust :: forall s a b m. MonadState s m => Setter s s a (Maybe b) -> b -> m Unit#assign Source
assign :: forall s a b m. MonadState s m => Setter s s a b -> b -> m UnitSet the foci of a Setter in a monadic state to a constant value.
#appendOver Source
appendOver :: forall s t a. Semigroup a => Setter s t a a -> a -> s -> t#appendModifying Source
appendModifying :: forall s a m. MonadState s m => Semigroup a => Setter' s a -> a -> m Unit#addModifying Source
addModifying :: forall s a m. MonadState s m => Semiring a => Setter' s a -> a -> m UnitRe-exports from Data.Lens.Traversal
#traversed Source
traversed :: forall t a b. Traversable t => Traversal (t a) (t b) a bA Traversal for the elements of a Traversable functor.
over traversed negate [1, 2, 3] == [-1,-2,-3]
over traversed negate (Just 3) == Just -3
#traverseOf Source
traverseOf :: forall f s t a b. Optic (Star f) s t a b -> (a -> f b) -> s -> f tTurn a pure profunctor Traversal into a lens-like Traversal.
#sequenceOf Source
sequenceOf :: forall f s t a. Optic (Star f) s t (f a) a -> s -> f tSequence the foci of an optic, pulling out an "effect".
If you do not need the result, see sequenceOf_ for Folds.
sequenceOf traversed has the same result as Data.Traversable.sequence:
sequenceOf traversed (Just [1, 2]) == [Just 1, Just 2]
sequence (Just [1, 2]) == [Just 1, Just 2]
An example with effects:
> array = [random, random]
> :t array
Array (Eff ... Number)
> effect = sequenceOf traversed array
> :t effect
Eff ... (Array Number)
> effect >>= logShow
[0.15556037108154985,0.28500369615270515]
unit
#itraverseOf Source
itraverseOf :: forall f i s t a b. IndexedOptic (Star f) i s t a b -> (i -> a -> f b) -> s -> f tTurn a pure profunctor IndexedTraversal into a lens-like IndexedTraversal.
#elementsOf Source
elementsOf :: forall p i s t a. Wander p => IndexedTraversal i s t a a -> (i -> Boolean) -> IndexedOptic p i s t a aTraverse elements of an IndexedTraversal whose index satisfy a predicate.
#element Source
element :: forall p s t a. Wander p => Int -> Traversal s t a a -> Optic p s t a aCombine an index and a traversal to narrow the focus to a single
element. Compare to Data.Lens.Index.
set (element 2 traversed) 8888 [0, 0, 3] == [0, 0, 8888]
preview (element 2 traversed) [0, 0, 3] == Just 3
The resulting traversal is called an affine traversal, which means that the traversal focuses on one or zero (if the index is out of range) results.
Re-exports from Data.Lens.Types
#Traversal' Source
type Traversal' s a = Traversal s s a a#Traversal Source
type Traversal s t a b = forall p. Wander p => Optic p s t a bThe specialized type Traversal s a means that the type s contains zero, one, or many values of type a.
They're especially useful when working with collections like arrays, maps, and any other member of the Traversable type class.
They are also a more general form of lenses and prisms.
Traversals which focus on at most one element (like lenses, prisms, and their composition) are called affine traversals.
#Tagged Source
#Re Source
#Lens' Source
type Lens' s a = Lens s s a aLens' is a specialization of Lens. An optic of type Lens'
can change only the value of its focus,
not its type. As an example, consider the Lens _2, which has this type:
_2 :: forall s t a b. Lens (Tuple s a) (Tuple t b) a b
_2 can produce a Tuple Int String from a Tuple Int Int:
set _2 "NEW" (Tuple 1 2) == (Tuple 1 "NEW")
If we specialize _2's type with Lens', the following will not
type check:
set (_2 :: Lens' (Tuple Int Int) Int) "NEW" (Tuple 1 2)
^^^^^^^^^^^^^^^^^^^^^^^^^
See Data.Lens.Getter and Data.Lens.Setter for functions and operators
frequently used with lenses.
#Lens Source
type Lens s t a b = forall p. Strong p => Optic p s t a bGiven a type whose "focus element" always exists, a lens provides a convenient way to view, set, and transform that element.
For example, _2 is a tuple-specific Lens available from Data.Lens, so:
over _2 String.length $ Tuple "ignore" "four" == Tuple "ignore" 4
Note the result has a different type than the original tuple.
That is, the four Lens type variables have been narrowed to:
sisTuple String StringtisTuple String IntaisStringbisInt
See Data.Lens.Getter and Data.Lens.Setter for functions and operators
frequently used with lenses.
#Iso Source
type Iso s t a b = forall p. Profunctor p => Optic p s t a bA generalized isomorphism.
Isos are the most constrained optic, simply enabling you to transform back and forth between two types without losing information.
The specialized type Iso' s a means that s and a are isomorphic – the two types represent the same information.
They’re especially useful when you need to unwrap a newtype or transform an array to a list or otherwise convert between types.
This optic is also useful for backward compatibility without boilerplate.
#IndexedTraversal' Source
type IndexedTraversal' i s a = IndexedTraversal i s s a a#IndexedTraversal Source
type IndexedTraversal i s t a b = forall p. Wander p => IndexedOptic p i s t a b#IndexedSetter' Source
type IndexedSetter' i s a = IndexedSetter i s s a a#IndexedSetter Source
type IndexedSetter i s t a b = IndexedOptic Function i s t a b#IndexedOptic' Source
type IndexedOptic' :: (Type -> Type -> Type) -> Type -> Type -> Type -> Typetype IndexedOptic' p i s a = IndexedOptic p i s s a a
#IndexedGetter' Source
type IndexedGetter' i s a = IndexedGetter i s s a a#IndexedGetter Source
type IndexedGetter i s t a b = IndexedFold a i s t a b#IndexedFold' Source
type IndexedFold' r i s a = IndexedFold r i s s a a#IndexedFold Source
type IndexedFold r i s t a b = IndexedOptic (Forget r) i s t a b#Indexed Source
#Forget Source
#ATraversal' Source
type ATraversal' s a = ATraversal s s a a#ATraversal Source
type ATraversal s t a b = Optic (Bazaar Function a b) s t a bA traversal defined in terms of Bazaar, which can be used
to avoid issues with impredicativity.
#Wander Source
class Wander :: (Type -> Type -> Type) -> Constraintclass (Strong p, Choice p) <= Wander p where
Class for profunctors that support polymorphic traversals.
Members
wander :: forall s t a b. (forall f. Applicative f => (a -> f b) -> s -> f t) -> p a b -> p s t
Instances
Wander Function(Applicative f) => Wander (Star f)
- Modules
- Data.
Lens - Data.
Lens. AffineTraversal - Data.
Lens. At - Data.
Lens. Common - Data.
Lens. Fold - Data.
Lens. Fold. Partial - Data.
Lens. Getter - Data.
Lens. Grate - Data.
Lens. Index - Data.
Lens. Indexed - Data.
Lens. Internal. Bazaar - Data.
Lens. Internal. Exchange - Data.
Lens. Internal. Focusing - Data.
Lens. Internal. Forget - Data.
Lens. Internal. Grating - Data.
Lens. Internal. Indexed - Data.
Lens. Internal. Market - Data.
Lens. Internal. Re - Data.
Lens. Internal. Shop - Data.
Lens. Internal. Stall - Data.
Lens. Internal. Tagged - Data.
Lens. Internal. Wander - Data.
Lens. Internal. Zipping - Data.
Lens. Iso - Data.
Lens. Iso. Newtype - Data.
Lens. Lens - Data.
Lens. Lens. Product - Data.
Lens. Lens. Tuple - Data.
Lens. Lens. Unit - Data.
Lens. Lens. Void - Data.
Lens. Prism - Data.
Lens. Prism. Coproduct - Data.
Lens. Prism. Either - Data.
Lens. Prism. Maybe - Data.
Lens. Record - Data.
Lens. Setter - Data.
Lens. Traversal - Data.
Lens. Types - Data.
Lens. Zoom