1- -- | This module defines functions for working with traversals.
1+ -- | `Traversal` is an optic that focuses on zero or more functor values. An
2+ -- | `Array` would be a typical example:
3+ -- |
4+ -- | ```purescript
5+ -- | over traversed negate [1, 2, 3] == [-1, -2, -3]
6+ -- | preview traversed [1, 2, 3] == Just 1
7+ -- | firstOf traversed [1, 2, 3] == Just 1 -- same as `preview`
8+ -- | lastOf traversed [1, 2, 3] == Just 3
9+ -- | ```
10+ -- |
11+ -- | `view` might surprise you. It assumes that the wrapped values
12+ -- | are a monoid, and `append`s them together:
13+ -- |
14+ -- | ```purescript
15+ -- | view traversed ["D", "a", "w", "n"] == "Dawn"
16+ -- | ```
17+ -- |
18+ -- | Many of the functions you'll use are documented in `Data.Lens.Fold`.
19+
220module Data.Lens.Traversal
321 ( traversed
22+ , element
423 , traverseOf
524 , sequenceOf
625 , failover
726 , elementsOf
827 , itraverseOf
9- , element
1028 , module ExportTypes
1129 ) where
1230
@@ -24,7 +42,12 @@ import Data.Traversable (class Traversable, traverse)
2442import Data.Tuple (Tuple (..), uncurry )
2543import Data.Newtype (under , unwrap )
2644
27- -- | Create a `Traversal` which traverses the elements of a `Traversable` functor.
45+ -- | A `Traversal` for the elements of a `Traversable` functor.
46+ -- |
47+ -- | ```purescript
48+ -- | over traversed negate [1, 2, 3] == [-1,-2,-3]
49+ -- | over traversed negate (Just 3) == Just -3
50+ -- | ```
2851traversed :: forall t a b . Traversable t => Traversal (t a ) (t b ) a b
2952traversed = wander traverse
3053
@@ -37,6 +60,29 @@ traverseOf = under Star
3760
3861-- | Sequence the foci of a `Traversal`, pulling out an `Applicative` effect.
3962-- | If you do not need the result, see `sequenceOf_` for `Fold`s.
63+ -- |
64+ -- | `sequenceOf traversed` has the same result as `Data.Traversable.sequence`:
65+ -- |
66+ -- | ```purescript
67+ -- | sequenceOf traversed (Just [1, 2]) == [Just 1, Just 2]
68+ -- | sequence (Just [1, 2]) == [Just 1, Just 2]
69+ -- | ```
70+ -- |
71+ -- | An example with effects:
72+ -- | ```purescript
73+ -- | > array = [random, random]
74+ -- | > :t array
75+ -- | Array (Eff ... Number)
76+ -- |
77+ -- | > effect = sequenceOf traversed array
78+ -- | > :t effect
79+ -- | Eff ... (Array Number)
80+ -- |
81+ -- | > effect >>= logShow
82+ -- | [0.15556037108154985,0.28500369615270515]
83+ -- | unit
84+ -- | ```
85+
4086sequenceOf
4187 :: forall f s t a
4288 . Applicative f
@@ -56,7 +102,14 @@ failover t f s = case unwrap (t $ Star $ Tuple (Disj true) <<< f) s of
56102 Tuple (Disj true ) x -> pure x
57103 Tuple (Disj false ) _ -> empty
58104
59- -- | Affine traversal the `n`-th focus of a `Traversal`.
105+ -- | Combine an index and a traversal to narrow the focus to a single
106+ -- | element. This is called an "affine traversal". Compare to `Data.Lens.Index`.
107+ -- |
108+ -- | ```purescript
109+ -- | set (element 2 traversed) 8888 [0, 0, 3] == [0, 0, 8888]
110+ -- | preview (element 2 traversed) [0, 0, 3] == Just 3
111+ -- | ```
112+
60113element
61114 :: forall p s t a
62115 . Wander p
0 commit comments