profunctor-optics-0.0.0.4: 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 Fold from a Van Laarhoven Fold.

toFold :: AView s a -> Fold s a Source #

Obtain a Fold from a View or AFold.

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

TODO: Document

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

aifold :: ((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.

toFold1 :: AView s a -> Fold1 s a Source #

Obtain a Fold1 from a View or AFold1.

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

TODO: Document

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

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_'

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

Obtain an Ixfold from a Representable functor.

unital :: Foldable f => Foldable g => Monoid r => Semiring r => AFold r (f (g a)) a Source #

Expression in a unital semiring

unitalsummed . multiplied
>>> folds unital [[1,2], [3,4 :: Int]]
14

For semirings without a multiplicative unit this is equivalent to const mempty:

>>> folds unital $ (fmap . fmap) Just [[1,2], [3,4 :: Int]]
Just 0

In this situation you most likely want to use nonunital.

nonunital :: Foldable f => Foldable1 g => Monoid r => Semiring r => AFold r (f (g a)) a Source #

Expression in a semiring expression with no multiplicative unit.

nonunitalsummed . multiplied1
>>> folds1 nonunital $ (fmap . fmap) Just [1 :| [2], 3 :| [4 :: Int]]
Just 14

presemiring :: Foldable1 f => Foldable1 g => Semiring r => AFold1 r (f (g a)) a Source #

Expression in a semiring with no additive or multiplicative unit.

presemiringsummed1 . multiplied1

summed :: Foldable f => Monoid r => AFold r (f a) a Source #

Monoidal sum of a foldable collection.

>>> 1 <> 2 <> 3 <> 4 :: Int
10
>>> folds summed [1,2,3,4 :: Int]
10

summed and multiplied compose just as they do in arithmetic:

>>> 1 >< 2 <> 3 >< 4 :: Int
14
>>> folds (summed . multiplied) [[1,2], [3,4 :: Int]]
14
>>> (1 <> 2) >< (3 <> 4) :: Int
21
>>> folds (multiplied . summed) [[1,2], [3,4 :: Int]]
21

summed1 :: Foldable1 f => Semigroup r => AFold1 r (f a) a Source #

Semigroup sum of a non-empty foldable collection.

>>> 1 <> 2 <> 3 <> 4 :: Int
10
>>> folds1 summed1 $ 1 :| [2,3,4 :: Int]
10

multiplied :: Foldable f => Monoid r => Semiring r => AFold r (f a) a Source #

Semiring product of a foldable collection.

>>> 1 >< 2 >< 3 >< 4 :: Int
24
>>> folds multiplied [1,2,3,4 :: Int]
24

For semirings without a multiplicative unit this is equivalent to const mempty:

>>> folds multiplied $ fmap Just [1..(5 :: Int)]
Just 0

In this situation you most likely want to use multiplied1.

multiplied1 :: Foldable1 f => Semiring r => AFold1 r (f a) a Source #

Semiring product of a non-empty foldable collection.

>>> folds1 multiplied1 $ fmap Just (1 :| [2..(5 :: Int)])
Just 120 

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:

>>> withIxfold itraversed const 100 [1..5]
10
>>> withIxfold itraversed const 100 []
0

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 :: Int]] ^.. id
[[[1,2],[3]]]
>>> [[1,2], [3 :: Int]] ^.. traversed
[[1,2],[3]]
>>> [[1,2], [3 :: Int]] ^.. 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 :: 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

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.

(^%%) :: 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 :: Monoid i => Monoid a => AIxfold (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

foldsp :: Monoid r => Semiring r => AFold (Prod r) s a -> (a -> r) -> s -> r Source #

Compute the semiring product of the foci of an optic.

For semirings without a multiplicative unit this is equivalent to const mempty:

>>> foldsp folded Just [1..(5 :: Int)]
Just 0

In this situation you most likely want to use folds1p.

folds1p :: Semiring r => AFold (Prod r) s a -> (a -> r) -> s -> r Source #

Compute the semiring product of the foci of an optic.

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

Right fold over an optic.

>>> foldsr folded (<>) 0 [1..5::Int]
15

ifoldsr :: 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
>>> 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 (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r Source #

Left fold over an optic.

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

Left fold over an indexed optic.

ifoldslFrom :: AIxfold (Dual (Endo 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 (Dual (Endo (Endo r))) s a -> (a -> r -> r) -> r -> s -> r Source #

Strict right fold over an optic.

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

Strict right fold over an indexed optic.

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' :: Monoid i => AIxfold (Endo (r -> r)) i s a -> (i -> r -> a -> r) -> r -> s -> r Source #

Strict left fold over an indexed optic.

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

Monadic right fold over an optic.

ifoldsrM :: Monoid i => Monad m => AIxfold (Dual (Endo (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 :: 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_ :: 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

type Any = Bool Source #

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 #