-
Notifications
You must be signed in to change notification settings - Fork 272
/
Control-Lens-Traversal.html
242 lines (242 loc) · 59.2 KB
/
Control-Lens-Traversal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Control.Lens.Traversal</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Control-Lens-Traversal.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Control-Lens-Traversal.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">lens-3.5.1: Lenses, Folds and Traversals</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>Rank2Types</td></tr><tr><th>Stability</th><td>provisional</td></tr><tr><th>Maintainer</th><td>Edward Kmett <ekmett@gmail.com></td></tr><tr><th>Safe Haskell</th><td>Safe-Inferred</td></tr></table><p class="caption">Control.Lens.Traversal</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Lenses
</a><ul><li><a href="#g:2">Lensing Traversals
</a></li></ul></li><li><a href="#g:3">Traversing and Lensing
</a></li><li><a href="#g:4">Parts and Holes
</a></li><li><a href="#g:5">Common Traversals
</a></li><li><a href="#g:6">Cloning Traversals
</a></li><li><a href="#g:7">Simple
</a></li><li><a href="#g:8">Exposed Implementation Details
</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>A <code><code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b</code> is a generalization of <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> from
<code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code>. It allows you to traverse over a structure and change out
its contents with monadic or applicative side-effects. Starting from
</p><p><code><code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> :: (<code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> t, <code>Applicative</code> f) => (a -> f b) -> t a -> f (t b)</code>,
</p><p>we monomorphize the contents and result to obtain
</p><pre> type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t
</pre><p>While a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> isn't quite a <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code>, it _can_ be used for <code>Getting</code>
like a <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code>, because given a <code>Monoid</code> <code>m</code>, we have an <code>Applicative</code>
for <code>(<code>Const</code> m)</code>. Everything you know how to do with a <code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code>
container, you can with with a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, and here we provide
combinators that generalize the usual <code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> operations.
</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">type</span> <a href="#t:Traversal">Traversal</a> s t a b = <span class="keyword">forall</span> f. Applicative f => (a -> f b) -> s -> f t</li><li class="src short"><a href="#v:element">element</a> :: <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> t => Int -> <a href="Control-Lens-Type.html#t:Simple">Simple</a> <a href="Control-Lens-Type.html#t:Lens">Lens</a> (t a) a</li><li class="src short"><a href="#v:elementOf">elementOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:ElementOf">ElementOf</a> f) s t a a -> Int -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a a</li><li class="src short"><a href="#v:traverseOf">traverseOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> (a -> f b) -> s -> f t</li><li class="src short"><a href="#v:forOf">forOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> s -> (a -> f b) -> f t</li><li class="src short"><a href="#v:sequenceAOf">sequenceAOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t (f b) b -> s -> f t</li><li class="src short"><a href="#v:mapMOf">mapMOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t</li><li class="src short"><a href="#v:forMOf">forMOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t</li><li class="src short"><a href="#v:sequenceOf">sequenceOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t (m b) b -> s -> m t</li><li class="src short"><a href="#v:transposeOf">transposeOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> ZipList s t [a] a -> s -> [t]</li><li class="src short"><a href="#v:mapAccumLOf">mapAccumLOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (Backwards (State acc)) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)</li><li class="src short"><a href="#v:mapAccumROf">mapAccumROf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)</li><li class="src short"><a href="#v:scanr1Of">scanr1Of</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (State (Maybe a)) s t a a -> (a -> a -> a) -> s -> t</li><li class="src short"><a href="#v:scanl1Of">scanl1Of</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (Backwards (State (Maybe a))) s t a a -> (a -> a -> a) -> s -> t</li><li class="src short"><a href="#v:partsOf">partsOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a a f) s t a a -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t [a] [a]</li><li class="src short"><a href="#v:partsOf-39-">partsOf'</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a a) s t a a -> <a href="Control-Lens-Type.html#t:Lens">Lens</a> s t [a] [a]</li><li class="src short"><a href="#v:unsafePartsOf">unsafePartsOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a b f) s t a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t [a] [b]</li><li class="src short"><a href="#v:unsafePartsOf-39-">unsafePartsOf'</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b) s t a b -> <a href="Control-Lens-Type.html#t:Lens">Lens</a> s t [a] [b]</li><li class="src short"><a href="#v:holesOf">holesOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a a) s t a a -> s -> [<a href="Control-Lens-Internal.html#t:Context">Context</a> a a t]</li><li class="src short"><span class="keyword">class</span> (Functor t, Foldable t) => <a href="#t:Traversable">Traversable</a> t <span class="keyword">where</span><ul class="subs"><li><a href="#v:traverse">traverse</a> :: Applicative f => (a -> f b) -> t a -> f (t b)</li></ul></li><li class="src short"><a href="#v:traverseLeft">traverseLeft</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (Either a c) (Either b c) a b</li><li class="src short"><a href="#v:traverseRight">traverseRight</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (Either c a) (Either c b) a b</li><li class="src short"><a href="#v:both">both</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (a, a) (b, b) a b</li><li class="src short"><a href="#v:beside">beside</a> :: Applicative f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s' t' a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f (s, s') (t, t') a b</li><li class="src short"><a href="#v:taking">taking</a> :: Applicative f => Int -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a a f) s a -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> f s a</li><li class="src short"><a href="#v:dropping">dropping</a> :: Applicative f => Int -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> (<a href="Control-Lens-Internal.html#t:Indexing">Indexing</a> f) s a -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> f s a</li><li class="src short"><a href="#v:cloneTraversal">cloneTraversal</a> :: Applicative f => ((a -> <a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b b) -> s -> <a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b t) -> (a -> f b) -> s -> f t</li><li class="src short"><span class="keyword">data</span> <a href="#t:ReifiedTraversal">ReifiedTraversal</a> s t a b = <a href="#v:ReifyTraversal">ReifyTraversal</a> {<ul class="subs"><li><a href="#v:reflectTraversal">reflectTraversal</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> s t a b</li></ul>}</li><li class="src short"><span class="keyword">type</span> <a href="#t:SimpleTraversal">SimpleTraversal</a> s a = <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> s s a a</li><li class="src short"><span class="keyword">type</span> <a href="#t:SimpleReifiedTraversal">SimpleReifiedTraversal</a> s a = <a href="Control-Lens-Traversal.html#t:ReifiedTraversal">ReifiedTraversal</a> s s a a</li><li class="src short"><span class="keyword">newtype</span> <a href="#t:Bazaar">Bazaar</a> a b t = <a href="#v:Bazaar">Bazaar</a> {<ul class="subs"><li><a href="#v:runBazaar">runBazaar</a> :: <span class="keyword">forall</span> f. Applicative f => (a -> f b) -> f t</li></ul>}</li></ul></div><div id="interface"><h1 id="g:1">Lenses
</h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:Traversal" class="def">Traversal</a> s t a b = <span class="keyword">forall</span> f. Applicative f => (a -> f b) -> s -> f t<a href="src/Control-Lens-Traversal.html#Traversal" class="link">Source</a></p><div class="doc"><p>A <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> can be used directly as a <code><a href="Control-Lens-Setter.html#t:Setter">Setter</a></code> or a <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> (but not as a <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>) and provides
the ability to both read and update multiple fields, subject to some relatively weak <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> laws.
</p><p>These have also been known as multilenses, but they have the signature and spirit of
</p><pre><code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> f => <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> (f a) (f b) a b</pre><p>and the more evocative name suggests their application.
</p><p>Most of the time the <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> you will want to use is just <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code>, but you can also pass any
<code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> or <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> as a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, and composition of a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> (or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> or <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code>) with a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> (or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> or <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code>)
using (.) forms a valid <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p><p>The laws for a Traversal <code>t</code> follow from the laws for Traversable as stated in "The Essence of the Iterator Pattern".
</p><pre>
t <code>pure</code> ≡ <code>pure</code>
<code>fmap</code> (t f) <code>.</code> t g ≡ <code><a href="Data-Functor-Compose.html#t:getCompose">getCompose</a></code> <code>.</code> t (<code><a href="Data-Functor-Compose.html#t:Compose">Compose</a></code> <code>.</code> <code>fmap</code> f <code>.</code> g)
</pre><p>One consequence of this requirement is that a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> needs to leave the same number of elements as a
candidate for subsequent <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> that it started with. Another testament to the strength of these laws
is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exotic
<code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> instances that <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> the same entry multiple times was actually already ruled out by the
second law in that same paper!
</p></div></div><h2 id="g:2">Lensing Traversals
</h2><div class="top"><p class="src"><a name="v:element" class="def">element</a> :: <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> t => Int -> <a href="Control-Lens-Type.html#t:Simple">Simple</a> <a href="Control-Lens-Type.html#t:Lens">Lens</a> (t a) a<a href="src/Control-Lens-Traversal.html#element" class="link">Source</a></p><div class="doc"><p>Access the <em>nth</em> element of a <code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> container.
</p><p>Attempts to access beyond the range of the <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> will cause an error.
</p><pre><code><a href="Control-Lens-Traversal.html#v:element">element</a></code> ≡ <code><a href="Control-Lens-Traversal.html#v:elementOf">elementOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre></div></div><div class="top"><p class="src"><a name="v:elementOf" class="def">elementOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:ElementOf">ElementOf</a> f) s t a a -> Int -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a a<a href="src/Control-Lens-Traversal.html#elementOf" class="link">Source</a></p><div class="doc"><p>A <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> to 'Control.Lens.Getter.view'/'Control.Lens.Setter.set' the nth element <code><a href="Control-Lens-Traversal.html#v:elementOf">elementOf</a></code> a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> or <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code>.
</p><p>Attempts to access beyond the range of the <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> will cause an error. This also works transparently
with Folds, returning a getter.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[[1],[3,4]] & elementOf (traverse.traverse) 1 .~ 5
</code></strong>[[1],[5,4]]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[[1],[3,4]]^.elementOf (folded.folded) 1
</code></strong>3
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[0..]^.elementOf folded 5
</code></strong>5
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>take 10 $ (elementOf traverse 3 .~ 16) [0..]
</code></strong>[0,1,2,16,4,5,6,7,8,9]
</pre></div></div><h1 id="g:3">Traversing and Lensing
</h1><div class="top"><p class="src"><a name="v:traverseOf" class="def">traverseOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> (a -> f b) -> s -> f t<a href="src/Control-Lens-Traversal.html#traverseOf" class="link">Source</a></p><div class="doc"><p>Map each element of a structure targeted by a Lens or Traversal,
evaluate these actions from left to right, and collect the results.
</p><p>This function is only provided for consistency, <code>id</code> is strictly more general.
</p><pre><code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> ≡ <code>id</code></pre><p>This yields the obvious law:
</p><pre><code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> ≡ <code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><pre>
<code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> (a -> f b) -> s -> f t
<code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> (a -> f b) -> s -> f t
<code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> (a -> f b) -> s -> f t
</pre></div></div><div class="top"><p class="src"><a name="v:forOf" class="def">forOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> s -> (a -> f b) -> f t<a href="src/Control-Lens-Traversal.html#forOf" class="link">Source</a></p><div class="doc"><p>A version of <code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> with the arguments flipped, such that:
</p><pre><code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> l ≡ <code>flip</code> (<code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> l)</pre><pre>
<code>for</code> ≡ <code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code>
</pre><p>This function is only provided for consistency, <code>flip</code> is strictly more general.
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> ≡ <code>flip</code>
</pre><pre>
<code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> s -> (a -> f b) -> f t
<code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> s -> (a -> f b) -> f t
<code><a href="Control-Lens-Traversal.html#v:forOf">forOf</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> s -> (a -> f b) -> f t
</pre></div></div><div class="top"><p class="src"><a name="v:sequenceAOf" class="def">sequenceAOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t (f b) b -> s -> f t<a href="src/Control-Lens-Traversal.html#sequenceAOf" class="link">Source</a></p><div class="doc"><p>Evaluate each action in the structure from left to right, and collect
the results.
</p><pre>
<code>sequenceA</code> ≡ <code><a href="Control-Lens-Traversal.html#v:sequenceAOf">sequenceAOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> ≡ <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code> <code>id</code>
<code><a href="Control-Lens-Traversal.html#v:sequenceAOf">sequenceAOf</a></code> l ≡ <code><a href="Control-Lens-Traversal.html#v:traverseOf">traverseOf</a></code> l id ≡ l id
</pre><pre>
<code><a href="Control-Lens-Traversal.html#v:sequenceAOf">sequenceAOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t (f b) b -> s -> f t
<code><a href="Control-Lens-Traversal.html#v:sequenceAOf">sequenceAOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t (f b) b -> s -> f t
<code><a href="Control-Lens-Traversal.html#v:sequenceAOf">sequenceAOf</a></code> :: <code>Applicative</code> f => <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t (f b) b -> s -> f t
</pre></div></div><div class="top"><p class="src"><a name="v:mapMOf" class="def">mapMOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t<a href="src/Control-Lens-Traversal.html#mapMOf" class="link">Source</a></p><div class="doc"><p>Map each element of a structure targeted by a lens to a monadic action,
evaluate these actions from left to right, and collect the results.
</p><pre><code>mapM</code> ≡ <code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><pre>
<code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> (a -> m b) -> s -> m t
<code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> (a -> m b) -> s -> m t
<code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> :: <code>Monad</code> m => <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> (a -> m b) -> s -> m t
</pre></div></div><div class="top"><p class="src"><a name="v:forMOf" class="def">forMOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t<a href="src/Control-Lens-Traversal.html#forMOf" class="link">Source</a></p><div class="doc"><p><code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> is a flipped version of <code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code>, consistent with the definition of <code>forM</code>.
<code>
<code>forM</code> ≡ <code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code>
<code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> l ≡ <code>flip</code> (<code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> l)
</code>
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> s -> (a -> m b) -> m t
<code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> s -> (a -> m b) -> m t
<code><a href="Control-Lens-Traversal.html#v:forMOf">forMOf</a></code> :: <code>Monad</code> m => <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> s -> (a -> m b) -> m t
</pre></div></div><div class="top"><p class="src"><a name="v:sequenceOf" class="def">sequenceOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (WrappedMonad m) s t (m b) b -> s -> m t<a href="src/Control-Lens-Traversal.html#sequenceOf" class="link">Source</a></p><div class="doc"><p>Sequence the (monadic) effects targeted by a lens in a container from left to right.
</p><pre>
<code>sequence</code> ≡ <code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code>
<code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> l ≡ <code><a href="Control-Lens-Traversal.html#v:mapMOf">mapMOf</a></code> l id
<code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> l ≡ <code>unwrapMonad</code> . l <code>WrapMonad</code>
</pre><pre>
<code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t (m b) b -> s -> m t
<code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t (m b) b -> s -> m t
<code><a href="Control-Lens-Traversal.html#v:sequenceOf">sequenceOf</a></code> :: <code>Monad</code> m => <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t (m b) b -> s -> m t
</pre></div></div><div class="top"><p class="src"><a name="v:transposeOf" class="def">transposeOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> ZipList s t [a] a -> s -> [t]<a href="src/Control-Lens-Traversal.html#transposeOf" class="link">Source</a></p><div class="doc"><p>This generalizes <code><a href="Data-List.html#t:transpose">transpose</a></code> to an arbitrary <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p><p>Note: <code><a href="Data-List.html#t:transpose">transpose</a></code> handles ragged inputs more intelligently, but for non-ragged inputs:
</p><pre><code><a href="Data-List.html#t:transpose">transpose</a></code> ≡ <code><a href="Control-Lens-Traversal.html#v:transposeOf">transposeOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>transposeOf traverse [[1,2,3],[4,5,6]]
</code></strong>[[1,4],[2,5],[3,6]]
</pre><p>Since every <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> is a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, we can use this as a form of
monadic strength as well:
</p><pre><code><a href="Control-Lens-Traversal.html#v:transposeOf">transposeOf</a></code> <code>_2</code> :: (b, [a]) -> [(b, a)]</pre></div></div><div class="top"><p class="src"><a name="v:mapAccumLOf" class="def">mapAccumLOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (Backwards (State acc)) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)<a href="src/Control-Lens-Traversal.html#mapAccumLOf" class="link">Source</a></p><div class="doc"><p>This generalizes <code>mapAccumL</code> to an arbitrary <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p><pre><code>mapAccumL</code> ≡ <code><a href="Control-Lens-Traversal.html#v:mapAccumLOf">mapAccumLOf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><p><code><a href="Control-Lens-Traversal.html#v:mapAccumLOf">mapAccumLOf</a></code> accumulates state from left to right.
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:mapAccumLOf">mapAccumLOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
<code><a href="Control-Lens-Traversal.html#v:mapAccumLOf">mapAccumLOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
<code><a href="Control-Lens-Traversal.html#v:mapAccumLOf">mapAccumLOf</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
</pre></div></div><div class="top"><p class="src"><a name="v:mapAccumROf" class="def">mapAccumROf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)<a href="src/Control-Lens-Traversal.html#mapAccumROf" class="link">Source</a></p><div class="doc"><p>This generalizes <code>mapAccumR</code> to an arbitrary <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p><pre><code>mapAccumR</code> ≡ <code><a href="Control-Lens-Traversal.html#v:mapAccumROf">mapAccumROf</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><p><code><a href="Control-Lens-Traversal.html#v:mapAccumROf">mapAccumROf</a></code> accumulates state from right to left.
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:mapAccumROf">mapAccumROf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
<code><a href="Control-Lens-Traversal.html#v:mapAccumROf">mapAccumROf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
<code><a href="Control-Lens-Traversal.html#v:mapAccumROf">mapAccumROf</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
</pre></div></div><div class="top"><p class="src"><a name="v:scanr1Of" class="def">scanr1Of</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (State (Maybe a)) s t a a -> (a -> a -> a) -> s -> t<a href="src/Control-Lens-Traversal.html#scanr1Of" class="link">Source</a></p><div class="doc"><p>This permits the use of <code>scanr1</code> over an arbitrary <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>.
</p><pre><code>scanr1</code> ≡ <code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><pre>
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a a -> (a -> a -> a) -> s -> t
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a a -> (a -> a -> a) -> s -> t
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a a -> (a -> a -> a) -> s -> t
</pre></div></div><div class="top"><p class="src"><a name="v:scanl1Of" class="def">scanl1Of</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (Backwards (State (Maybe a))) s t a a -> (a -> a -> a) -> s -> t<a href="src/Control-Lens-Traversal.html#scanl1Of" class="link">Source</a></p><div class="doc"><p>This permits the use of <code>scanl1</code> over an arbitrary <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>.
</p><pre><code>scanl1</code> ≡ <code><a href="Control-Lens-Traversal.html#v:scanl1Of">scanl1Of</a></code> <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><pre>
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a a -> (a -> a -> a) -> s -> t
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a a -> (a -> a -> a) -> s -> t
<code><a href="Control-Lens-Traversal.html#v:scanr1Of">scanr1Of</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a a -> (a -> a -> a) -> s -> t
</pre></div></div><h1 id="g:4">Parts and Holes
</h1><div class="top"><p class="src"><a name="v:partsOf" class="def">partsOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a a f) s t a a -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t [a] [a]<a href="src/Control-Lens-Traversal.html#partsOf" class="link">Source</a></p><div class="doc"><p><code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> turns a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> into a <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> that resembles an early version of the <code>uniplate</code> (or <code>biplate</code>) type.
</p><p><em>Note:</em> You should really try to maintain the invariant of the number of children in the list.
</p><p>Any extras will be lost. If you do not supply enough, then the remainder will come from the original structure.
</p><p>So technically, this is only a lens if you do not change the number of results it returns.
</p><p>When applied to a <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> the result is merely a <code>Getter</code>.
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s a -> <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s [a]
<code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s a -> <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s [a]
<code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s a -> <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s [a]
<code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> :: <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> s a -> <code>Getter</code> s [a]
<code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> :: <code>Getter</code> s a -> <code>Getter</code> s [a]
</pre></div></div><div class="top"><p class="src"><a name="v:partsOf-39-" class="def">partsOf'</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a a) s t a a -> <a href="Control-Lens-Type.html#t:Lens">Lens</a> s t [a] [a]<a href="src/Control-Lens-Traversal.html#partsOf%27" class="link">Source</a></p><div class="doc"><p>A type-restricted version of <code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code> that can only be used with a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p></div></div><div class="top"><p class="src"><a name="v:unsafePartsOf" class="def">unsafePartsOf</a> :: Functor f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a b f) s t a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t [a] [b]<a href="src/Control-Lens-Traversal.html#unsafePartsOf" class="link">Source</a></p><div class="doc"><p><code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> turns a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> into a <code>uniplate</code> (or <code>biplate</code>) family.
</p><p>If you do not need the types of <code>s</code> and <code>t</code> to be different, it is recommended that
you use <code><a href="Control-Lens-Traversal.html#v:partsOf">partsOf</a></code>
</p><p>It is generally safer to traverse with the <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> rather than use this
combinator. However, it is sometimes convenient.
</p><p>This is unsafe because if you don't supply at least as many <code>b</code>'s as you were
given <code>a</code>'s, then the reconstruction of <code>t</code> <em>will</em> result in an error!
</p><p>When applied to a <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> the result is merely a <code>Getter</code> (and becomes safe).
</p><pre>
<code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> :: <code><a href="Control-Lens-Iso.html#t:Iso">Iso</a></code> s t a b -> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t [a] [b]
<code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t a b -> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t [a] [b]
<code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> :: <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b -> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s t [a] [b]
<code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> :: <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> s a -> <code>Getter</code> s [a]
<code><a href="Control-Lens-Traversal.html#v:unsafePartsOf">unsafePartsOf</a></code> :: <code>Getter</code> s a -> <code>Getter</code> s [a]
</pre></div></div><div class="top"><p class="src"><a name="v:unsafePartsOf-39-" class="def">unsafePartsOf'</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b) s t a b -> <a href="Control-Lens-Type.html#t:Lens">Lens</a> s t [a] [b]<a href="src/Control-Lens-Traversal.html#unsafePartsOf%27" class="link">Source</a></p></div><div class="top"><p class="src"><a name="v:holesOf" class="def">holesOf</a> :: <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a a) s t a a -> s -> [<a href="Control-Lens-Internal.html#t:Context">Context</a> a a t]<a href="src/Control-Lens-Traversal.html#holesOf" class="link">Source</a></p><div class="doc"><p>The one-level version of <code>contextsOf</code>. This extracts a list of the immediate children according to a given <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> as editable contexts.
</p><p>Given a context you can use <code>pos</code> to see the values, <code>peek</code> at what the structure would be like with an edited result, or simply <code>extract</code> the original structure.
</p><pre>
propChildren l x = <code>childrenOf</code> l x <code>==</code> <code>map</code> <code>pos</code> (<code><a href="Control-Lens-Traversal.html#v:holesOf">holesOf</a></code> l x)
propId l x = <code>all</code> (<code>==</code> x) [extract w | w <- <code><a href="Control-Lens-Traversal.html#v:holesOf">holesOf</a></code> l x]
</pre><pre>
<code><a href="Control-Lens-Traversal.html#v:holesOf">holesOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code>Iso</code> s a -> s -> [<code><a href="Control-Lens-Internal.html#t:Context">Context</a></code> a a s]
<code><a href="Control-Lens-Traversal.html#v:holesOf">holesOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code> s a -> s -> [<code><a href="Control-Lens-Internal.html#t:Context">Context</a></code> a a s]
<code><a href="Control-Lens-Traversal.html#v:holesOf">holesOf</a></code> :: <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s a -> s -> [<code><a href="Control-Lens-Internal.html#t:Context">Context</a></code> a a s]
</pre></div></div><h1 id="g:5">Common Traversals
</h1><div class="top"><p class="src"><span class="keyword">class</span> (Functor t, Foldable t) => <a name="t:Traversable" class="def">Traversable</a> t <span class="keyword">where</span></p><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:traverse" class="def">traverse</a> :: Applicative f => (a -> f b) -> t a -> f (t b)</p></div><div class="subs instances"><p id="control.i:Traversable" class="caption collapser" onclick="toggleSection('i:Traversable')">Instances</p><div id="section.i:Traversable" class="show"><table><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> []</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Maybe</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Identity</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Tree</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> NonEmpty</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> <a href="Control-Lens-Internal.html#t:Level">Level</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> IntMap</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Elem</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Seq</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Node</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Digit</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> FingerTree</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> ViewR</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> ViewL</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> Vector</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (Backwards f)</td><td class="doc empty"> </td></tr><tr><td class="src">Ix i => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (Array i)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (IdentityT f)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (MaybeT f)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (ListT f)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (HashMap k)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (Map k)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f, <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> g) => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (Compose f g)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> w => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (EnvT e w)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (ErrorT e f)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (WriterT w f)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> f => <a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a> (WriterT w f)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:traverseLeft" class="def">traverseLeft</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (Either a c) (Either b c) a b<a href="src/Control-Lens-Traversal.html#traverseLeft" class="link">Source</a></p><div class="doc"><p>A traversal for tweaking the left-hand value of an <code>Either</code>:
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>over traverseLeft (+1) (Left 2)
</code></strong>Left 3
<code class="prompt">>>> </code><strong class="userinput"><code>over traverseLeft (+1) (Right 2)
</code></strong>Right 2
<code class="prompt">>>> </code><strong class="userinput"><code>Right 42 ^.traverseLeft :: String
</code></strong>""
<code class="prompt">>>> </code><strong class="userinput"><code>Left "hello" ^.traverseLeft
</code></strong>"hello"
</pre><pre>traverseLeft :: <code>Applicative</code> f => (a -> f b) -> <code>Either</code> a c -> f (<code>Either</code> b c)</pre></div></div><div class="top"><p class="src"><a name="v:traverseRight" class="def">traverseRight</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (Either c a) (Either c b) a b<a href="src/Control-Lens-Traversal.html#traverseRight" class="link">Source</a></p><div class="doc"><p>traverse the right-hand value of an <code>Either</code>:
</p><pre><code><a href="Control-Lens-Traversal.html#v:traverseRight">traverseRight</a></code> ≡ <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code></pre><p>Unfortunately the instance for
<code><code><a href="Control-Lens-Traversal.html#t:Traversable">Traversable</a></code> (<code>Either</code> c)</code> is still missing from base,
so this can't just be <code><a href="Control-Lens-Traversal.html#v:traverse">traverse</a></code>
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>over traverseRight (+1) (Left 2)
</code></strong>Left 2
<code class="prompt">>>> </code><strong class="userinput"><code>over traverseRight (+1) (Right 2)
</code></strong>Right 3
<code class="prompt">>>> </code><strong class="userinput"><code>Right "hello" ^.traverseRight
</code></strong>"hello"
<code class="prompt">>>> </code><strong class="userinput"><code>Left "hello" ^.traverseRight :: [Double]
</code></strong>[]
</pre><pre>traverseRight :: <code>Applicative</code> f => (a -> f b) -> <code>Either</code> c a -> f (<code>Either</code> c a)</pre></div></div><div class="top"><p class="src"><a name="v:both" class="def">both</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> (a, a) (b, b) a b<a href="src/Control-Lens-Traversal.html#both" class="link">Source</a></p><div class="doc"><p>Traverse both parts of a tuple with matching types.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>both *~ 10 $ (1,2)
</code></strong>(10,20)
<code class="prompt">>>> </code><strong class="userinput"><code>over both length ("hello","world")
</code></strong>(5,5)
<code class="prompt">>>> </code><strong class="userinput"><code>("hello","world")^.both
</code></strong>"helloworld"
</pre></div></div><div class="top"><p class="src"><a name="v:beside" class="def">beside</a> :: Applicative f => <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s t a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f s' t' a b -> <a href="Control-Lens-Type.html#t:LensLike">LensLike</a> f (s, s') (t, t') a b<a href="src/Control-Lens-Traversal.html#beside" class="link">Source</a></p><div class="doc"><p>Apply a different <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> or <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code> to each side of a tuple.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>("hello",["world","!!!"])^..beside id traverse
</code></strong>["hello","world","!!!"]
</pre></div></div><div class="top"><p class="src"><a name="v:taking" class="def">taking</a> :: Applicative f => Int -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> (<a href="Control-Lens-Internal.html#t:BazaarT">BazaarT</a> a a f) s a -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> f s a<a href="src/Control-Lens-Traversal.html#taking" class="link">Source</a></p><div class="doc"><p>Visit the first <em>n</em> targets of a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code>, <code>Getter</code> or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[("hello","world"),("!!!","!!!")]^.. taking 2 (traverse.both)
</code></strong>["hello","world"]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..]^.. taking 3 traverse
</code></strong>[1,2,3]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>over (taking 5 traverse) succ "hello world"
</code></strong>"ifmmp world"
</pre></div></div><div class="top"><p class="src"><a name="v:dropping" class="def">dropping</a> :: Applicative f => Int -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> (<a href="Control-Lens-Internal.html#t:Indexing">Indexing</a> f) s a -> <a href="Control-Lens-Type.html#t:SimpleLensLike">SimpleLensLike</a> f s a<a href="src/Control-Lens-Traversal.html#dropping" class="link">Source</a></p><div class="doc"><p>Visit all but the first <em>n</em> targets of a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>, <code><a href="Control-Lens-Fold.html#t:Fold">Fold</a></code>, <code>Getter</code> or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>("hello","world") ^? dropping 1 both
</code></strong>Just "world"
</pre><p>Dropping works on infinite traversals as well.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..]^? dropping 1 folded
</code></strong>Just 2
</pre></div></div><h1 id="g:6">Cloning Traversals
</h1><div class="top"><p class="src"><a name="v:cloneTraversal" class="def">cloneTraversal</a> :: Applicative f => ((a -> <a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b b) -> s -> <a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b t) -> (a -> f b) -> s -> f t<a href="src/Control-Lens-Traversal.html#cloneTraversal" class="link">Source</a></p><div class="doc"><p>A <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> is completely characterized by its behavior on a <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code>.
</p><p>Cloning a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> is one way to make sure you aren't given
something weaker, such as a <code><a href="Control-Lens-Traversal.html#t:Fold">Fold</a></code> and can be
used as a way to pass around traversals that have to be monomorphic in <code>f</code>.
</p><p>Note: This only accepts a proper <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> (or <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>). To clone a <code><a href="Control-Lens-Type.html#t:Lens">Lens</a></code>
as such, use <code><a href="Control-Lens-Type.html#v:cloneLens">cloneLens</a></code>
</p><p>Note: It is usually better to <code><a href="Control-Lens-Traversal.html#v:ReifyTraversal">ReifyTraversal</a></code> and use <code><a href="Control-Lens-Traversal.html#v:reflectTraversal">reflectTraversal</a></code>
than to <code><a href="Control-Lens-Traversal.html#v:cloneTraversal">cloneTraversal</a></code>. The former can execute at full speed, while the
latter needs to round trip through the <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code>.
</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>let foo l a = (view (cloneTraversal l) a, set (cloneTraversal l) 10 a)
</code></strong><code class="prompt">>>> </code><strong class="userinput"><code>foo both ("hello","world")
</code></strong>("helloworld",(10,10))
</pre><pre><code><a href="Control-Lens-Traversal.html#v:cloneTraversal">cloneTraversal</a></code> :: <code><a href="Control-Lens-Type.html#t:LensLike">LensLike</a></code> (<code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> a b) s t a b -> <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b</pre></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:ReifiedTraversal" class="def">ReifiedTraversal</a> s t a b <a href="src/Control-Lens-Traversal.html#ReifiedTraversal" class="link">Source</a></p><div class="doc"><p>A form of <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> that can be stored monomorphically in a container.
</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ReifyTraversal" class="def">ReifyTraversal</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:reflectTraversal" class="def">reflectTraversal</a> :: <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> s t a b</dt><dd class="doc empty"> </dd></dl><div class="clear"></div></div></td></tr></table></div></div><h1 id="g:7">Simple
</h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:SimpleTraversal" class="def">SimpleTraversal</a> s a = <a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a> s s a a<a href="src/Control-Lens-Traversal.html#SimpleTraversal" class="link">Source</a></p><div class="doc"><pre>type SimpleTraversal = <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code></pre></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:SimpleReifiedTraversal" class="def">SimpleReifiedTraversal</a> s a = <a href="Control-Lens-Traversal.html#t:ReifiedTraversal">ReifiedTraversal</a> s s a a<a href="src/Control-Lens-Traversal.html#SimpleReifiedTraversal" class="link">Source</a></p><div class="doc"><pre>type SimpleReifiedTraversal = <code><a href="Control-Lens-Type.html#t:Simple">Simple</a></code> <code><a href="Control-Lens-Traversal.html#t:ReifiedTraversal">ReifiedTraversal</a></code></pre></div></div><h1 id="g:8">Exposed Implementation Details
</h1><div class="top"><p class="src"><span class="keyword">newtype</span> <a name="t:Bazaar" class="def">Bazaar</a> a b t <a href="src/Control-Lens-Internal.html#Bazaar" class="link">Source</a></p><div class="doc"><p>This is used to characterize a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code>.
</p><p>a.k.a. indexed Cartesian store comonad, indexed Kleene store comonad, or an indexed <code>FunList</code>.
</p><p><a href="http://twanvl.nl/blog/haskell/non-regular1">http://twanvl.nl/blog/haskell/non-regular1</a>
</p><p><code><code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> a b t</code> is isomorphic to <code>data Bazaar a b t = Buy t | Trade (Bazaar a b (b -> t)) a</code>,
and to <code>exists s. (s, <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> s t a b)</code>.
</p><p>A <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> is like a <code><a href="Control-Lens-Traversal.html#t:Traversal">Traversal</a></code> that has already been applied to some structure.
</p><p>Where a <code><code><a href="Control-Lens-Internal.html#t:Context">Context</a></code> a b t</code> holds an <code>a</code> and a function from <code>b</code> to
<code>t</code>, a <code><code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> a b t</code> holds N <code>a</code>s and a function from N
<code>b</code>s to <code>t</code>.
</p><p>Mnemonically, a <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code> holds many stores and you can easily add more.
</p><p>This is a final encoding of <code><a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a></code>.
</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:Bazaar" class="def">Bazaar</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:runBazaar" class="def">runBazaar</a> :: <span class="keyword">forall</span> f. Applicative f => (a -> f b) -> f t</dt><dd class="doc empty"> </dd></dl><div class="clear"></div></div></td></tr></table></div><div class="subs instances"><p id="control.i:Bazaar" class="caption collapser" onclick="toggleSection('i:Bazaar')">Instances</p><div id="section.i:Bazaar" class="show"><table><tr><td class="src">Functor (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b)</td><td class="doc empty"> </td></tr><tr><td class="src">Applicative (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b)</td><td class="doc empty"> </td></tr><tr><td class="src">~ * a b => ComonadApply (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b)</td><td class="doc empty"> </td></tr><tr><td class="src">~ * a b => Comonad (<a href="Control-Lens-Traversal.html#t:Bazaar">Bazaar</a> a b)</td><td class="doc empty"> </td></tr></table></div></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.13.1</p></div></body></html>