profunctor-optics-0.0.1: An optics library compatible with the typeclasses in 'profunctors'.

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.Fold

Contents

Synopsis

Fold & Ixfold

type Fold s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => Optic' p s a Source #

A Fold combines 0 or more elements, with Monoid interactions.

type Ixfold i s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => IndexedOptic' p i s a Source #

fold_ :: Foldable f => (s -> f a) -> Fold s a Source #

Obtain a Fold directly.

fold_ (lists o) ≡ o
fold_ f ≡ to f . foldVl traverse_
fold_ f ≡ coercer . lmap f . lift traverse_

See Property.

This can be useful to lift operations from Data.List and elsewhere into a Fold.

>>> [1,2,3,4] ^.. fold_ tail
[2,3,4]

folding :: Traversable f => (s -> a) -> Fold (f s) a Source #

Obtain a Fold from a Traversable functor.

folding f ≡ traversed . to f
folding f ≡ foldVl traverse . to f

foldVl :: (forall f. Applicative f => (a -> f b) -> s -> f t) -> Fold s a Source #

Obtain a Fold from a Van Laarhoven Fold.

ifoldVl :: (forall f. Applicative f => (i -> a -> f b) -> s -> f t) -> Ixfold i s a Source #

Obtain a Ixfold from a Van Laarhoven Fold.

afold :: Monoid r => ((a -> r) -> s -> r) -> APrimView r s t a b Source #

TODO: Document

afold :: Monoid r => ((a -> r) -> s -> r) -> AFold r s a

aifold :: Monoid r => ((i -> a -> r) -> s -> r) -> AIxfold r i s a Source #

TODO: Document

Fold1 & Ixfold1

type Fold1 s a = forall p. (Strong p, Representable p, Apply (Rep p), forall x. Contravariant (p x)) => Optic' p s a Source #

A Fold1 combines 1 or more elements, with Semigroup interactions.

type Ixfold1 i s a = forall p. (Strong p, Representable p, Apply (Rep p), forall x. Contravariant (p x)) => IndexedOptic' p i s a Source #

fold1_ :: Foldable1 f => (s -> f a) -> Fold1 s a Source #

Obtain a Fold1 directly.

fold1_ (nelists o) ≡ o
fold1_ f ≡ to f . fold1Vl traverse1_
fold1_ f ≡ coercer . lmap f . lift traverse1_

See Property.

This can be useful to repn operations from Data.List.NonEmpty and elsewhere into a Fold1.

folding1 :: Traversable1 f => (s -> a) -> Fold1 (f s) a Source #

Obtain a Fold1 from a Traversable1 functor.

folding1 f ≡ traversed1 . to f
folding1 f ≡ fold1Vl traverse1 . to f

fold1Vl :: (forall f. Apply f => (a -> f b) -> s -> f t) -> Fold1 s a Source #

Obtain a Fold1 from a Van Laarhoven Fold1.

See Property.

ifold1Vl :: (forall f. Apply f => (i -> a -> f b) -> s -> f t) -> Ixfold1 i s a Source #

Obtain a Ixfold1 from a Van Laarhoven Fold1.

afold1 :: ((a -> r) -> s -> r) -> APrimView r s t a b Source #

TODO: Document

afold1 :: ((a -> r) -> s -> r) -> AFold1 r s a

aifold1 :: ((i -> a -> r) -> s -> r) -> AIxfold1 r i s a Source #

TODO: Document

Optics

folded :: Traversable f => Fold (f a) a Source #

Obtain a Fold from a Traversable functor.

folded_ :: Foldable f => Fold (f a) a Source #

The canonical Fold.

foldMapwithFold folded_'

folded1 :: Traversable1 f => Fold1 (f a) a Source #

Obtain a Fold1 from a Traversable1 functor.

folded1_ :: Foldable1 f => Fold1 (f a) a Source #

The canonical Fold1.

foldMap1withFold1 folded1_'

Indexed optics

ifolded :: FoldableWithKey f => Ixfold (Key f) (f a) a Source #

Obtain an AIxfold from a FoldableWithKey.

f ^%% ifoldedtoKeyedList f

ifoldedRep :: Representable f => Traversable f => Ixfold (Rep f) (f a) a Source #

Obtain an Ixfold from a Representable functor.

ifolded1 :: FoldableWithKey1 f => Ixfold1 (Key f) (f a) a Source #

Obtain an Ixfold1 from a FoldableWithKey1.

aifolded :: FoldableWithKey f => Monoid r => AIxfold r (Key f) (f a) a Source #

Obtain an AIxfold from a FoldableWithKey.

aifolded1 :: FoldableWithKey1 f => Semigroup r => AIxfold1 r (Key f) (f a) a Source #

Obtain an AIxfold1 from a FoldableWithKey1.

Primitive operators

withFold :: Monoid r => APrimView r s t a b -> (a -> r) -> s -> r Source #

Map an optic to a monoid and combine the results.

foldMap = withFold folded_
>>> withFold both id (["foo"], ["bar", "baz"])
["foo","bar","baz"]
>>> :t withFold traversed
withFold traversed
  :: (Monoid r, Traversable f) => (a -> r) -> f a -> r
withFold :: Monoid r => AFold r s a -> (a -> r) -> s -> r

withIxfold :: Monoid r => AIxfold r i s a -> (i -> a -> r) -> i -> s -> r Source #

Map an indexed optic to a monoid and combine the results.

Note that most indexed optics do not use their output index:

withFold1 :: Semigroup r => APrimView r s t a b -> (a -> r) -> s -> r Source #

Map an optic to a semigroup and combine the results.

withFold1 :: Semigroup r => AFold1 r s a -> (a -> r) -> s -> r

withIxfold1 :: Semigroup r => AIxfold1 r i s a -> (i -> a -> r) -> i -> s -> r Source #

Map an indexed optic to a semigroup and combine the results.

>>> :t flip withIxfold1 Map.singleton
flip withIxfold1 Map.singleton
  :: Ord i => AIxfold1 (Map i a) i s a -> i -> s -> Map i a
withIxfold1 :: Semigroup r => AIxfold1 r s a -> (i -> a -> r) -> i -> s -> r

Operators

lists :: AFold (Endo [a]) s a -> s -> [a] Source #

Collect the foci of an optic into a list.

(^..) :: s -> AFold (Endo [a]) s a -> [a] infixl 8 Source #

Infix alias of lists.

toList xs ≡ xs ^.. folding
(^..) ≡ flip lists
>>> [[1,2], [3 :: Int64]] ^.. id
[[[1,2],[3]]]
>>> [[1,2], [3 :: Int64]] ^.. traversed
[[1,2],[3]]
>>> [[1,2], [3 :: Int64]] ^.. traversed . traversed
[1,2,3]
>>> (1,2) ^.. bitraversed
[1,2]
(^..) :: s -> View s a     -> a :: s -> Fold s a       -> a :: s -> Lens' s a      -> a :: s -> Iso' s a       -> a :: s -> Traversal' s a -> a :: s -> Prism' s a     -> a :: s -> Affine' s a    -> [a]

ilists :: (Additive - Monoid) i => AIxfold (Endo [(i, a)]) i s a -> s -> [(i, a)] Source #

Collect the foci of an indexed optic into a list of index-value pairs.

lists l ≡ map snd . ilists l
>>> ilists (itraversed . imapping swapped) [(40,'f'),(41,'o'),(42,'o')]
[(0,('f',40)),(1,('o',41)),(2,('o',42))]

ilistsFrom :: AIxfold (Endo [(i, a)]) i s a -> i -> s -> [(i, a)] Source #

Collect the foci of an indexed optic into a list of index-value pairs.

This is only for use with the few indexed optics that don't ignore their output index. You most likely want to use ilists.

(^%%) :: (Additive - Monoid) i => s -> AIxfold (Endo [(i, a)]) i s a -> [(i, a)] infixl 8 Source #

Infix version of ilists.

nelists :: AFold1 (Nedl a) s a -> s -> NonEmpty a Source #

Extract a NonEmpty of the foci of an optic.

>>> nelists bitraversed1 ('h' :| "ello", 'w' :| "orld")
('h' :| "ello") :| ['w' :| "orld"]

folds :: Monoid a => AFold a s a -> s -> a Source #

TODO: Document

ifolds :: (Additive - Monoid) i => Monoid a => AIxfold (Additive i, a) i s a -> s -> (i, a) Source #

TODO: Document

folds1 :: Semigroup a => AFold1 a s a -> s -> a Source #

TODO: Document

foldsa :: Applicative f => Monoid (f a) => AFold (f a) s a -> s -> f a Source #

TODO: Document

foldsa :: Fold s a -> s -> [a]
foldsa :: Applicative f => Setter s t a b -> s -> f a

foldsr :: AFold (Endo r) s a -> (a -> r -> r) -> r -> s -> r Source #

Right fold over an optic.

>>> foldsr folded (+) 0 [1..5::Int64]
15

ifoldsr :: (Additive - Monoid) i => AIxfold (Endo r) i s a -> (i -> a -> r -> r) -> r -> s -> r Source #

Indexed right fold over an indexed optic.

foldsr o ≡ ifoldsr o . const
foldrWithKey f ≡ ifoldsr ifolded f
>>> ifoldsr itraversed (\i a -> ((show i ++ ":" ++ show a ++ ", ") ++)) [] [1,3,5,7,9]
"0:1, 1:3, 2:5, 3:7, 4:9, "

ifoldsrFrom :: AIxfold (Endo r) i s a -> (i -> a -> r -> r) -> i -> r -> s -> r Source #

Indexed right fold over an indexed optic, using an initial index value.

This is only for use with the few indexed optics that don't ignore their output index. You most likely want to use ifoldsr.

foldsl :: AFold ((Endo - Dual) r) s a -> (r -> a -> r) -> r -> s -> r Source #

Left fold over an optic.

ifoldsl :: (Additive - Monoid) i => AIxfold ((Endo - Dual) r) i s a -> (i -> r -> a -> r) -> r -> s -> r Source #

Left fold over an indexed optic.

foldsl o ≡ ifoldsl o . const
foldlWithKey f ≡ ifoldsl ifolded f

ifoldslFrom :: AIxfold ((Endo - Dual) r) i s a -> (i -> r -> a -> r) -> i -> r -> s -> r Source #

Left fold over an indexed optic, using an initial index value.

This is only for use with the few indexed optics that don't ignore their output index. You most likely want to use ifoldsl.

foldsr' :: AFold ((Endo - Dual) (Endo r)) s a -> (a -> r -> r) -> r -> s -> r Source #

Strict right fold over an optic.

ifoldsr' :: (Additive - Monoid) i => AIxfold ((Endo - Dual) (r -> r)) i s a -> (i -> a -> r -> r) -> r -> s -> r Source #

Strict right fold over an indexed optic.

foldsr' o ≡ ifoldsr' o . const
foldrWithKey' f ≡ ifoldsr' ifolded f

foldsl' :: AFold ((Endo - Endo) r) s a -> (r -> a -> r) -> r -> s -> r Source #

Strict left fold over an optic.

foldl'foldsl' folding
foldsl' :: Iso' s a        -> (c -> a -> c) -> c -> s -> c
foldsl' :: Lens' s a       -> (c -> a -> c) -> c -> s -> c
foldsl' :: View s a        -> (c -> a -> c) -> c -> s -> c
foldsl' :: Fold s a        -> (c -> a -> c) -> c -> s -> c
foldsl' :: Traversal' s a  -> (c -> a -> c) -> c -> s -> c
foldsl' :: Affine' s a -> (c -> a -> c) -> c -> s -> c

ifoldsl' :: (Additive - Monoid) i => AIxfold (Endo (r -> r)) i s a -> (i -> r -> a -> r) -> r -> s -> r Source #

Strict left fold over an indexed optic.

foldsl' o ≡ ifoldsl' o . const
foldlWithKey' f ≡ ifoldsl' ifolded f

foldsrM :: Monad m => AFold ((Endo - Dual) (r -> m r)) s a -> (a -> r -> m r) -> r -> s -> m r Source #

Monadic right fold over an optic.

ifoldsrM :: (Additive - Monoid) i => Monad m => AIxfold ((Endo - Dual) (r -> m r)) i s a -> (i -> a -> r -> m r) -> r -> s -> m r Source #

Monadic right fold over an indexed optic.

foldsrMifoldrM . const

foldslM :: Monad m => AFold (Endo (r -> m r)) s a -> (r -> a -> m r) -> r -> s -> m r Source #

Monadic left fold over an optic.

ifoldslM :: (Additive - Monoid) i => Monad m => AIxfold (Endo (r -> m r)) i s a -> (i -> r -> a -> m r) -> r -> s -> m r Source #

Monadic left fold over an indexed optic.

foldslMifoldslM . const

traverses_ :: Applicative f => AFold (Endo (f ())) s a -> (a -> f r) -> s -> f () Source #

Applicative fold over an optic.

>>> traverses_ both putStrLn ("hello","world")
hello
world
traverse_traverses_ folded

itraverses_ :: (Additive - Monoid) i => Applicative f => AIxfold (Endo (f ())) i s a -> (i -> a -> f r) -> s -> f () Source #

Applicative fold over an indexed optic.

Auxilliary Types

newtype Nedl a Source #

Constructors

Nedl 

Fields

Instances
Semigroup (Nedl a) Source # 
Instance details

Defined in Data.Profunctor.Optic.Fold

Methods

(<>) :: Nedl a -> Nedl a -> Nedl a #

sconcat :: NonEmpty (Nedl a) -> Nedl a #

stimes :: Integral b => b -> Nedl a -> Nedl a #