map -package:containers -package:bytestring -package:aeson package:base

Map a function over a NonEmpty stream.
Given a means of mapping the elements of a structure to lists, computes the concatenation of all such lists in order.

Examples

Basic usage:
>>> biconcatMap (take 3) (fmap digitToInt) ([1..], "89")
[1,2,3,8,9]
>>> biconcatMap (take 3) (fmap digitToInt) (Left [1..])
[1,2,3]
>>> biconcatMap (take 3) (fmap digitToInt) (Right "89")
[8,9]
Combines the elements of a structure, given ways of mapping them to a common monoid.
bifoldMap f g ≡ bifoldr (mappend . f) (mappend . g) mempty

Examples

Basic usage:
>>> bifoldMap (take 3) (fmap digitToInt) ([1..], "89")
[1,2,3,8,9]
>>> bifoldMap (take 3) (fmap digitToInt) (Left [1..])
[1,2,3]
>>> bifoldMap (take 3) (fmap digitToInt) (Right "89")
[8,9]
Alias for bitraverse_.
Map over both arguments at the same time.
bimap f g ≡ first f . second g

Examples

>>> bimap toUpper (+1) ('j', 3)
('J',4)
>>> bimap toUpper (+1) (Left 'j')
Left 'J'
>>> bimap toUpper (+1) (Right 3)
Right 4
A default definition of bifoldMap in terms of the Bitraversable operations.
bifoldMapDefault f g ≡
getConst . bitraverse (Const . f) (Const . g)
The bimapAccumL function behaves like a combination of bimap and bifoldl; it traverses a structure from left to right, threading a state of type a and using the given actions to compute new elements for the structure.

Examples

Basic usage:
>>> bimapAccumL (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")
(8,("True","oof"))
The bimapAccumR function behaves like a combination of bimap and bifoldr; it traverses a structure from right to left, threading a state of type a and using the given actions to compute new elements for the structure.

Examples

Basic usage:
>>> bimapAccumR (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")
(7,("True","oof"))
A default definition of bimap in terms of the Bitraversable operations.
bimapDefault f g ≡
runIdentity . bitraverse (Identity . f) (Identity . g)
Alias for bitraverse.
Map each element of the structure to a semigroup, and combine the results with (<>). This fold is right-associative and lazy in the accumulator. For strict left-associative folds consider foldMap1' instead.
>>> foldMap1 (:[]) (1 :| [2, 3, 4])
[1,2,3,4]
A left-associative variant of foldMap1 that is strict in the accumulator. Use this for strict reduction when partial results are merged via (<>).
>>> foldMap1' Sum (1 :| [2, 3, 4])
Sum {getSum = 10}
Left-associative fold of a structure, lazy in the accumulator. This is rarely what you want, but can work well for structures with efficient right-to-left sequencing and an operator that is lazy in its left argument. In case of NonEmpty lists, foldlMap1, when given a function f, a binary operator g, and a list, reduces the list using g from left to right applying f to the leftmost element:
foldlMap1 f g (x1 :| [x2, ..., xn]) == (...(((f x1) `g` x2) `g`...) `g` xn
Note that to produce the outermost application of the operator the entire input list must be traversed. This means that foldlMap1 will diverge if given an infinite list. If you want an efficient strict left-fold, you probably want to use foldlMap1' instead of foldlMap1. The reason for this is that the latter does not force the inner results (e.g. (f x1) `g` x2 in the above example) before applying them to the operator (e.g. to (`g` x3)). This results in a thunk chain <math> elements long, which then must be evaluated from the outside-in. For a general Foldable1 structure this should be semantically identical to:
foldlMap1 f g = foldlMap1 f g . toNonEmpty
Left-associative fold of a structure but with strict application of the operator. This ensures that each step of the fold is forced to Weak Head Normal Form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite structure to a single strict result. For a general Foldable1 structure this should be semantically identical to:
foldlMap1' f z = foldlMap1' f z . toNonEmpty
Map variant of foldlM1.
Right-associative fold of a structure, lazy in the accumulator. In case of NonEmpty lists, foldrMap1, when given a function f, a binary operator g, and a list, reduces the list using g from right to left applying f to the rightmost element:
foldrMap1 f g (x1 :| [x2, ..., xn1, xn]) == x1 `g` (x2 `g` ... (xn1 `g` (f xn))...)
Note that since the head of the resulting expression is produced by an application of g to the first element of the list, if g is lazy in its right argument, foldrMap1 can produce a terminating expression from an unbounded list. For a general Foldable1 structure this should be semantically identical to:
foldrMap1 f g = foldrMap1 f g . toNonEmpty
foldrMap1' is a variant of foldrMap1 that performs strict reduction from right to left, i.e. starting with the right-most element. The input structure must be finite, otherwise foldrMap1' runs out of space (diverges). If you want a strict right fold in constant space, you need a structure that supports faster than <math> access to the right-most element. This method does not run in constant space for structures such as NonEmpty lists that don't support efficient right-to-left iteration and so require <math> space to perform right-to-left reduction. Use of this method with such a structure is a hint that the chosen structure may be a poor fit for the task at hand. If the order in which the elements are combined is not important, use foldlMap1' instead.
Map variant of foldrM1.