/usr/share/doc/libghc-microlens-doc/html/Lens-Micro.html is in libghc-microlens-doc 0.4.7.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | <!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>Lens.Micro</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script src="file:///usr/share/javascript/mathjax/MathJax.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Lens-Micro.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Lens-Micro.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">microlens-0.4.7.0: A tiny lens library with no dependencies. If you're writing an app, you probably want microlens-platform, not this.</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Copyright</th><td>(C) 2013-2016 Edward Kmett, 2015-2016 Artyom</td></tr><tr><th>License</th><td>BSD-style (see the file LICENSE)</td></tr><tr><th>Safe Haskell</th><td>Trustworthy</td></tr><tr><th>Language</th><td>Haskell2010</td></tr></table><p class="caption">Lens.Micro</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Setter: modifies something in a structure</a></li><li><a href="#g:2">Getter: extracts a value from a structure</a></li><li><a href="#g:3">Fold: extracts multiple elements</a></li><li><a href="#g:4">Lens: a combined getter-and-setter</a></li><li><a href="#g:5">Iso: a lens that only changes the representation</a></li><li><a href="#g:6">Traversal: a lens iterating over several elements</a></li><li><a href="#g:7">Prism: a traversal iterating over at most 1 element</a></li><li><a href="#g:8">Other types</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>This module provides the essential functionality. There are other packages in the microlens family – mix and match them at will. If you're writing an app, you want <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a> – it provides the most functionality.</p><ul><li><a href="http://hackage.haskell.org/package/microlens-mtl">microlens-mtl</a> – (<code>+=</code>) and friends, <code>use</code>, <code>zoom</code>/<code>magnify</code></li><li><a href="http://hackage.haskell.org/package/microlens-th">microlens-th</a> – <code>makeLenses</code> and <code>makeFields</code></li><li><a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a> – everything in microlens + instances to make <code>each</code>/<code>at</code>/<code>ix</code> usable with arrays, <code>ByteString</code>, and containers</li><li><a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a> – microlens-ghc + microlens-mtl + microlens-th + instances for <code>Text</code>, <code>Vector</code>, and <code>HashMap</code></li><li><a href="http://hackage.haskell.org/package/microlens-contra">microlens-contra</a> – <code>Fold</code> and <code>Getter</code> that are exact copies of types in lens</li></ul><p>Unofficial:</p><ul><li><a href="http://hackage.haskell.org/package/microlens-aeson">microlens-aeson</a> – a port of <a href="http://hackage.haskell.org/package/lens-aeson">lens-aeson</a></li></ul></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"><a href="#v:-38-">(&)</a> :: a -> (a -> b) -> b</li><li class="src short"><a href="#v:-60--38--62-">(<&>)</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => f a -> (a -> b) -> f b</li><li class="src short"><span class="keyword">type</span> <a href="#t:ASetter">ASetter</a> s t a b = (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a> b) -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a> t</li><li class="src short"><span class="keyword">type</span> <a href="#t:ASetter-39-">ASetter'</a> s a = <a href="Lens-Micro.html#t:ASetter">ASetter</a> s s a a</li><li class="src short"><a href="#v:sets">sets</a> :: ((a -> b) -> s -> t) -> <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b</li><li class="src short"><a href="#v:-37--126-">(%~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> (a -> b) -> s -> t</li><li class="src short"><a href="#v:over">over</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> (a -> b) -> s -> t</li><li class="src short"><a href="#v:.-126-">(.~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> b -> s -> t</li><li class="src short"><a href="#v:set">set</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> b -> s -> t</li><li class="src short"><a href="#v:-63--126-">(?~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> b) -> b -> s -> t</li><li class="src short"><a href="#v:-60--37--126-">(<%~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> b) s t a b -> (a -> b) -> s -> (b, t)</li><li class="src short"><a href="#v:-60--60--37--126-">(<<%~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> a) s t a b -> (a -> b) -> s -> (a, t)</li><li class="src short"><a href="#v:-60--60-.-126-">(<<.~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> a) s t a b -> b -> s -> (a, t)</li><li class="src short"><a href="#v:mapped">mapped</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => <a href="Lens-Micro.html#t:ASetter">ASetter</a> (f a) (f b) a b</li><li class="src short"><span class="keyword">type</span> <a href="#t:SimpleGetter">SimpleGetter</a> s a = <span class="keyword">forall</span> r. <a href="Lens-Micro.html#t:Getting">Getting</a> r s a</li><li class="src short"><span class="keyword">type</span> <a href="#t:Getting">Getting</a> r s a = (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a> r a) -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a> r s</li><li class="src short"><a href="#v:-94-.">(^.)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> a s a -> a</li><li class="src short"><a href="#v:to">to</a> :: (s -> a) -> <a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a> s a</li><li class="src short"><span class="keyword">type</span> <a href="#t:SimpleFold">SimpleFold</a> s a = <span class="keyword">forall</span> r. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a> r => <a href="Lens-Micro.html#t:Getting">Getting</a> r s a</li><li class="src short"><a href="#v:-94-..">(^..)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> [a]) s a -> [a]</li><li class="src short"><a href="#v:toListOf">toListOf</a> :: <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> [a]) s a -> s -> [a]</li><li class="src short"><a href="#v:-94--63-">(^?)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:First">First</a> a) s a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a</li><li class="src short"><a href="#v:-94--63--33-">(^?!)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> a) s a -> a</li><li class="src short"><a href="#v:traverseOf_">traverseOf_</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => <a href="Lens-Micro.html#t:Getting">Getting</a> (Traversed r f) s a -> (a -> f r) -> s -> f ()</li><li class="src short"><a href="#v:has">has</a> :: <a href="Lens-Micro.html#t:Getting">Getting</a> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Any">Any</a> s a -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Bool.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:folded">folded</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a> f => <a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a> (f a) a</li><li class="src short"><a href="#v:folding">folding</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a> f => (s -> f a) -> <a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a> s a</li><li class="src short"><span class="keyword">type</span> <a href="#t:Lens">Lens</a> s t a b = <span class="keyword">forall</span> f. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => (a -> f b) -> s -> f t</li><li class="src short"><span class="keyword">type</span> <a href="#t:Lens-39-">Lens'</a> s a = <a href="Lens-Micro.html#t:Lens">Lens</a> s s a a</li><li class="src short"><a href="#v:lens">lens</a> :: (s -> a) -> (s -> b -> t) -> <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:at">at</a> :: <a href="Lens-Micro-Internal.html#t:At">At</a> m => <a href="Lens-Micro-Internal.html#t:Index">Index</a> m -> <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> m (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> (<a href="Lens-Micro-Internal.html#t:IxValue">IxValue</a> m))</li><li class="src short"><a href="#v:_1">_1</a> :: <a href="Lens-Micro-Internal.html#t:Field1">Field1</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:_2">_2</a> :: <a href="Lens-Micro-Internal.html#t:Field2">Field2</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:_3">_3</a> :: <a href="Lens-Micro-Internal.html#t:Field3">Field3</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:_4">_4</a> :: <a href="Lens-Micro-Internal.html#t:Field4">Field4</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:_5">_5</a> :: <a href="Lens-Micro-Internal.html#t:Field5">Field5</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b</li><li class="src short"><a href="#v:strict">strict</a> :: <a href="Lens-Micro-Internal.html#t:Strict">Strict</a> lazy strict => <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> lazy strict</li><li class="src short"><a href="#v:lazy">lazy</a> :: <a href="Lens-Micro-Internal.html#t:Strict">Strict</a> lazy strict => <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> strict lazy</li><li class="src short"><a href="#v:non">non</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Eq.html#t:Eq">Eq</a> a => a -> <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) a</li><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. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a> f => (a -> f b) -> s -> f t</li><li class="src short"><span class="keyword">type</span> <a href="#t:Traversal-39-">Traversal'</a> s a = <a href="Lens-Micro.html#t:Traversal">Traversal</a> s s a a</li><li class="src short"><a href="#v:traverseOf">traverseOf</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> f s t a b -> (a -> f b) -> s -> f t</li><li class="src short"><a href="#v:singular">singular</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a a -> <a href="Lens-Micro.html#t:Lens">Lens</a> s t a a</li><li class="src short"><a href="#v:failing">failing</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b -> <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b -> <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b</li><li class="src short"><a href="#v:filtered">filtered</a> :: (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Bool.html#t:Bool">Bool</a>) -> <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> a a</li><li class="src short"><a href="#v:both">both</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (a, a) (b, b) a b</li><li class="src short"><a href="#v:traversed">traversed</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a> f => <a href="Lens-Micro.html#t:Traversal">Traversal</a> (f a) (f b) a b</li><li class="src short"><a href="#v:each">each</a> :: <a href="Lens-Micro-Internal.html#t:Each">Each</a> s t a b => <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b</li><li class="src short"><a href="#v:ix">ix</a> :: <a href="Lens-Micro-Internal.html#t:Ixed">Ixed</a> m => <a href="Lens-Micro-Internal.html#t:Index">Index</a> m -> <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> m (<a href="Lens-Micro-Internal.html#t:IxValue">IxValue</a> m)</li><li class="src short"><a href="#v:_head">_head</a> :: <a href="Lens-Micro-Internal.html#t:Cons">Cons</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s a</li><li class="src short"><a href="#v:_tail">_tail</a> :: <a href="Lens-Micro-Internal.html#t:Cons">Cons</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s s</li><li class="src short"><a href="#v:_init">_init</a> :: <a href="Lens-Micro-Internal.html#t:Snoc">Snoc</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s s</li><li class="src short"><a href="#v:_last">_last</a> :: <a href="Lens-Micro-Internal.html#t:Snoc">Snoc</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s a</li><li class="src short"><a href="#v:mapAccumLOf">mapAccumLOf</a> :: <a href="Lens-Micro.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:_Left">_Left</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a' b) a a'</li><li class="src short"><a href="#v:_Right">_Right</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b') b b'</li><li class="src short"><a href="#v:_Just">_Just</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a') a a'</li><li class="src short"><a href="#v:_Nothing">_Nothing</a> :: <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) ()</li><li class="src short"><span class="keyword">type</span> <a href="#t:LensLike">LensLike</a> f s t a b = (a -> f b) -> s -> f t</li><li class="src short"><span class="keyword">type</span> <a href="#t:LensLike-39-">LensLike'</a> f s a = <a href="Lens-Micro.html#t:LensLike">LensLike</a> f s s a a</li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><a id="v:-38-" class="def">(&)</a> :: a -> (a -> b) -> b <span class="fixity">infixl 1</span><span class="rightedge"></span> <a href="#v:-38-" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:-38-">&</a></code> is a reverse application operator. This provides notational
convenience. Its precedence is one higher than that of the forward
application operator <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:-36-">$</a></code>, which allows <code><a href="Lens-Micro.html#v:-38-">&</a></code> to be nested in <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:-36-">$</a></code>.</p><p><em>Since: 4.8.0.0</em></p></div></div><div class="doc"><p>This operator is useful when you want to modify something several times. For instance, if you want to change 1st and 3rd elements of a tuple, you can write this:</p><pre>(1,2,3) <code><a href="Lens-Micro.html#v:-38-">&</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code> <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> 0
<code><a href="Lens-Micro.html#v:-38-">&</a></code> <code><a href="Lens-Micro.html#v:_3">_3</a></code> <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:negate">negate</a></code>
</pre><p>instead of e.g. this:</p><pre>(<code><a href="Lens-Micro.html#v:_1">_1</a></code> <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> 0) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> (<code><a href="Lens-Micro.html#v:_3">_3</a></code> <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:negate">negate</a></code>) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:-36-">$</a></code> (1,2,3)
</pre><p>or this:</p><pre><code><a href="Lens-Micro.html#v:set">set</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code> 0 <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>
<code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:_3">_3</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:negate">negate</a></code>
<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:-36-">$</a></code> (1,2,3)
</pre></div><div class="top"><p class="src"><a id="v:-60--38--62-" class="def">(<&>)</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => f a -> (a -> b) -> f b <span class="fixity">infixl 1</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%3C%26%3E" class="link">Source</a> <a href="#v:-60--38--62-" class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:-60--38--62-"><&></a></code>) is flipped (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code>):</p><pre>x <code><a href="Lens-Micro.html#v:-60--38--62-"><&></a></code> f = f <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> x
</pre><p>It's often useful when writing lenses. For instance, let's say you're writing <code><a href="Lens-Micro.html#v:ix">ix</a></code> for <code>Map</code>; if the key is found in the map, you have to apply a function to it and then change the map based on the new value – which requires a lambda, like this:</p><pre><code><a href="Lens-Micro.html#v:ix">ix</a></code> key f map = case Map.lookup key map of
Just val -> (\val' -> Map.insert key val' map) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f val
Nothing -> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> map
</pre><p>With (<code><a href="Lens-Micro.html#v:-60--38--62-"><&></a></code>) you can get rid of parentheses and move the long lambda expression to the right of the value (like when you use <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Monad.html#v:-62--62--61-">>>=</a></code>):</p><pre><code><a href="Lens-Micro.html#v:ix">ix</a></code> key f map = case Map.lookup key map of
Just val -> f val <code><a href="Lens-Micro.html#v:-60--38--62-"><&></a></code> \val' -> Map.insert key val' map
Nothing -> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> map
</pre></div></div><h1 id="g:1">Setter: modifies something in a structure</h1><div class="doc"><p>A setter is, broadly speaking, something that lets you modify a part of some value. Most likely you already know some setters:</p><ul><li><pre><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Arrow.html#v:first">first</a></code> :: (a -> b) -> (a, x) -> (b, x)</pre><p>(modifies 1st element of a pair; corresponds to <code><a href="Lens-Micro.html#v:_1">_1</a></code>)</p></li><li><pre><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Arrow.html#v:left">left</a></code> :: (a -> b) -> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code> a x -> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code> b x</pre><p>(modifies left branch of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code>; corresponds to <code><a href="Lens-Micro.html#v:_Left">_Left</a></code>)</p></li><li><pre><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:map">map</a></code> :: (a -> b) -> [a] -> [b]</pre><p>(modifies every element in a list; corresponds to <code><a href="Lens-Micro.html#v:mapped">mapped</a></code>)</p></li></ul><p>As you see, a setter takes a function, a value, and applies the function to some part (or several parts) of the value. Moreover, setters can be pretty specific – for instance, a function that modifies the 3rd element of a list is a setter too:</p><pre>-- Modify 3rd element in a list, if present.
modify3rd :: (a -> a) -> [a] -> [a]
modify3rd f (a:b:c:xs) = a : b : f c : xs
modify3rd _ xs = xs
</pre><p>A nice thing about setters is that they compose easily – you can write <code><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:map">map</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Arrow.html#v:left">left</a></code></code> and it would be a function that takes a list of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code>s and modifies all of them that are <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code>s.</p><p>This library provides its own type for setters – <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code>; it's needed so that some functions in this library (like <code><a href="Lens-Micro.html#v:_1">_1</a></code>) would be usable both as setters and as getters. You can turn an ordinary function like <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:map">map</a></code> to a “lensy” setter with <code><a href="Lens-Micro.html#v:sets">sets</a></code>.</p><p>To apply a setter to a value, use (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) or <code><a href="Lens-Micro.html#v:over">over</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1,2,3] & mapped %~ succ
</code></strong>[2,3,4]
<code class="prompt">>>> </code><strong class="userinput"><code>over _head toUpper "jane"
</code></strong>"Jane"
</pre><p>To modify a value deeper inside the structure, use (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>["abc","def","ghi"] & ix 1 . ix 2 %~ toUpper
</code></strong>["abc","deF","ghi"]
</pre><p>To set a value instead of modifying it, use <code><a href="Lens-Micro.html#v:set">set</a></code> or (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>"abc" & mapped .~ 'x'
</code></strong>"xxx"
<code class="prompt">>>> </code><strong class="userinput"><code>set _2 'X' ('a','b','c')
</code></strong>('a','X','c')
</pre><p>It's also possible to get both the old and the new value back – see (<code><a href="Lens-Micro.html#v:-60--37--126-"><%~</a></code>) and (<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>).</p></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:ASetter" class="def">ASetter</a> s t a b = (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a> b) -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a> t <a href="src/Lens-Micro-Type.html#ASetter" class="link">Source</a> <a href="#t:ASetter" class="selflink">#</a></p><div class="doc"><p><code>ASetter s t a b</code> is something that turns a function modifying a value into a function modifying a <em>structure</em>. If you ignore <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a></code> (as <code>Identity a</code> is the same thing as <code>a</code>), the type is:</p><pre>type ASetter s t a b = (a -> b) -> s -> t
</pre><p>The reason <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a></code> is used here is for <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code> to be composable with other types, such as <code><a href="Lens-Micro.html#t:Lens">Lens</a></code>.</p><p>Technically, if you're writing a library, you shouldn't use this type for setters you are exporting from your library; the right type to use is <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:Setter">Setter</a></code>, but it is not provided by this package (because then it'd have to depend on <a href="http://hackage.haskell.org/package/distributive">distributive</a>). It's completely alright, however, to export functions which take an <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code> as an argument.</p></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:ASetter-39-" class="def">ASetter'</a> s a = <a href="Lens-Micro.html#t:ASetter">ASetter</a> s s a a <a href="src/Lens-Micro-Type.html#ASetter%27" class="link">Source</a> <a href="#t:ASetter-39-" class="selflink">#</a></p><div class="doc"><p>This is a type alias for monomorphic setters which don't change the type of the container (or of the value inside). It's useful more often than the same type in lens, because we can't provide real setters and so it does the job of both <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:ASetter-39-">ASetter'</a></code> and <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Setter.html#t:Setter-39-">Setter'</a></code>.</p></div></div><div class="top"><p class="src"><a id="v:sets" class="def">sets</a> :: ((a -> b) -> s -> t) -> <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b <a href="src/Lens-Micro-Internal.html#sets" class="link">Source</a> <a href="#v:sets" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:sets">sets</a></code> creates an <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code> from an ordinary function. (The only thing it does is wrapping and unwrapping <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a></code>.)</p></div></div><div class="top"><p class="src"><a id="v:-37--126-" class="def">(%~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> (a -> b) -> s -> t <span class="fixity">infixr 4</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%25~" class="link">Source</a> <a href="#v:-37--126-" class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) applies a function to the target; an alternative explanation is that it is an inverse of <code><a href="Lens-Micro.html#v:sets">sets</a></code>, which turns a setter into an ordinary function. <code><code><a href="Lens-Micro.html#v:mapped">mapped</a></code> <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:reverse">reverse</a></code></code> is the same thing as <code><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:fmap">fmap</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:reverse">reverse</a></code></code>.</p><p>See <code><a href="Lens-Micro.html#v:over">over</a></code> if you want a non-operator synonym.</p><p>Negating the 1st element of a pair:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2) & _1 %~ negate
</code></strong>(-1,2)
</pre><p>Turning all <code>Left</code>s in a list to upper case:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(mapped._Left.mapped %~ toUpper) [Left "foo", Right "bar"]
</code></strong>[Left "FOO",Right "bar"]
</pre></div></div><div class="top"><p class="src"><a id="v:over" class="def">over</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> (a -> b) -> s -> t <a href="src/Lens-Micro.html#over" class="link">Source</a> <a href="#v:over" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:over">over</a></code> is a synonym for (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>).</p><p>Getting <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:fmap">fmap</a></code> in a roundabout way:</p><pre><code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a></code> f => (a -> b) -> f a -> f b
<code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:fmap">fmap</a></code>
</pre><p>Applying a function to both components of a pair:</p><pre><code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:both">both</a></code> :: (a -> b) -> (a, a) -> (b, b)
<code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:both">both</a></code> = \f t -> (f (fst t), f (snd t))
</pre><p>Using <code><code><a href="Lens-Micro.html#v:over">over</a></code> <code><a href="Lens-Micro.html#v:_2">_2</a></code></code> as a replacement for <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Arrow.html#v:second">second</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>over _2 show (10,20)
</code></strong>(10,"20")
</pre></div></div><div class="top"><p class="src"><a id="v:.-126-" class="def">(.~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> b -> s -> t <span class="fixity">infixr 4</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#.~" class="link">Source</a> <a href="#v:.-126-" class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) assigns a value to the target. It's the same thing as using (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) with <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:const">const</a></code>:</p><pre>l <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> x = l <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:const">const</a></code> x
</pre><p>See <code><a href="Lens-Micro.html#v:set">set</a></code> if you want a non-operator synonym.</p><p>Here it is used to change 2 fields of a 3-tuple:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(0,0,0) & _1 .~ 1 & _3 .~ 3
</code></strong>(1,0,3)
</pre></div></div><div class="top"><p class="src"><a id="v:set" class="def">set</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a b -> b -> s -> t <a href="src/Lens-Micro.html#set" class="link">Source</a> <a href="#v:set" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:set">set</a></code> is a synonym for (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>).</p><p>Setting the 1st component of a pair:</p><pre><code><a href="Lens-Micro.html#v:set">set</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code> :: x -> (a, b) -> (x, b)
<code><a href="Lens-Micro.html#v:set">set</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code> = \x t -> (x, snd t)
</pre><p>Using it to rewrite (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36-"><$</a></code>):</p><pre><code><a href="Lens-Micro.html#v:set">set</a></code> <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a></code> f => a -> f b -> f a
<code><a href="Lens-Micro.html#v:set">set</a></code> <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> = (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36-"><$</a></code>)
</pre></div></div><div class="top"><p class="src"><a id="v:-63--126-" class="def">(?~)</a> :: <a href="Lens-Micro.html#t:ASetter">ASetter</a> s t a (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> b) -> b -> s -> t <span class="fixity">infixr 4</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%3F~" class="link">Source</a> <a href="#v:-63--126-" class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:-63--126-">?~</a></code>) is a version of (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) that wraps the value into <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code> before setting.</p><pre>l ?~ b = l .~ Just b
</pre><p>It can be useful in combination with <code><a href="Lens-Micro.html#v:at">at</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Map.empty & at 3 ?~ x
</code></strong>fromList [(3,x)]
</pre></div></div><div class="top"><p class="src"><a id="v:-60--37--126-" class="def">(<%~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> b) s t a b -> (a -> b) -> s -> (b, t) <a href="src/Lens-Micro.html#%3C%25~" class="link">Source</a> <a href="#v:-60--37--126-" class="selflink">#</a></p><div class="doc"><p>This is a version of (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) which modifies the structure and returns it along with the new value:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1, 2) & _1 <%~ negate
</code></strong>(-1, (-1, 2))
</pre><p>Simpler type signatures:</p><pre>(<code><a href="Lens-Micro.html#v:-60--37--126-"><%~</a></code>) :: <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> s t a b -> (a -> b) -> s -> (b, t)
(<code><a href="Lens-Micro.html#v:-60--37--126-"><%~</a></code>) :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a></code> b => <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> s t a b -> (a -> b) -> s -> (b, t)
</pre><p>Since it does getting in addition to setting, you can't use it with <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code> (but you can use it with lens and traversals).</p></div></div><div class="top"><p class="src"><a id="v:-60--60--37--126-" class="def">(<<%~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> a) s t a b -> (a -> b) -> s -> (a, t) <a href="src/Lens-Micro.html#%3C%3C%25~" class="link">Source</a> <a href="#v:-60--60--37--126-" class="selflink">#</a></p><div class="doc"><p>This is a version of (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) which modifies the structure and returns it along with the old value:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1, 2) & _1 <<%~ negate
</code></strong>(1, (-1, 2))
</pre><p>Simpler type signatures:</p><pre>(<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>) :: <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> s t a b -> (a -> b) -> s -> (a, t)
(<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>) :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a></code> a => <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> s t a b -> (a -> b) -> s -> (a, t)
</pre></div></div><div class="top"><p class="src"><a id="v:-60--60-.-126-" class="def">(<<.~)</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/ghc-prim-0.5.0.0/GHC-Tuple.html#t:-40--44--41-">(,)</a> a) s t a b -> b -> s -> (a, t) <a href="src/Lens-Micro.html#%3C%3C.~" class="link">Source</a> <a href="#v:-60--60-.-126-" class="selflink">#</a></p><div class="doc"><p>This is a version of (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) which modifies the structure and returns it along with the old value:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1, 2) & _1 <<.~ 0
</code></strong>(1, (0, 2))
</pre><p>Simpler type signatures:</p><pre>(<code><a href="Lens-Micro.html#v:-60--60-.-126-"><<.~</a></code>) :: <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> s t a b -> b -> s -> (a, t)
(<code><a href="Lens-Micro.html#v:-60--60-.-126-"><<.~</a></code>) :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a></code> a => <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> s t a b -> b -> s -> (a, t)
</pre></div></div><div class="top"><p class="src"><a id="v:mapped" class="def">mapped</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => <a href="Lens-Micro.html#t:ASetter">ASetter</a> (f a) (f b) a b <a href="src/Lens-Micro.html#mapped" class="link">Source</a> <a href="#v:mapped" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:mapped">mapped</a></code> is a setter for everything contained in a functor. You can use it to map over lists, <code>Maybe</code>, or even <code>IO</code> (which is something you can't do with <code><a href="Lens-Micro.html#v:traversed">traversed</a></code> or <code><a href="Lens-Micro.html#v:each">each</a></code>).</p><p>Here <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> is used to turn a value to all non-<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> values in a list:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[Just 3,Nothing,Just 5] & mapped.mapped .~ 0
</code></strong>[Just 0,Nothing,Just 0]
</pre><p>Keep in mind that while <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> is a more powerful setter than <code><a href="Lens-Micro.html#v:each">each</a></code>, it can't be used as a getter! This won't work (and will fail with a type error):</p><pre>[(1,2),(3,4),(5,6)] <code><a href="Lens-Micro.html#v:-94-..">^..</a></code> <code><a href="Lens-Micro.html#v:mapped">mapped</a></code> . <code><a href="Lens-Micro.html#v:both">both</a></code>
</pre></div></div><h1 id="g:2">Getter: extracts a value from a structure</h1><div class="doc"><p>A getter extracts something from a value; in fact, any function is a getter. However, same as with setters, this library uses a special type for getters so that functions like <code><a href="Lens-Micro.html#v:_1">_1</a></code> would be usable both as a setter and a getter. An ordinary function can be turned into a getter with <code><a href="Lens-Micro.html#v:to">to</a></code>.</p><p>Using a getter is done with (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) or <code><a href="Lens-Micro-Extras.html#v:view">view</a></code> from <a href="Lens-Micro-Extras.html">Lens.Micro.Extras</a>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>('x','y') ^. _1
</code></strong>'x'
<code class="prompt">>>> </code><strong class="userinput"><code>view (ix 2) [0..5]
</code></strong>2
</pre><p>Getters can be composed with (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[(1,2),(3,4),(5,6)] ^. ix 1 . _2
</code></strong>4
</pre><p>A getter always returns exactly 1 element (getters that can return more than one element are called folds and are present in this library as well).</p></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:SimpleGetter" class="def">SimpleGetter</a> s a = <span class="keyword">forall</span> r. <a href="Lens-Micro.html#t:Getting">Getting</a> r s a <a href="src/Lens-Micro-Type.html#SimpleGetter" class="link">Source</a> <a href="#t:SimpleGetter" class="selflink">#</a></p><div class="doc"><p>A <code>SimpleGetter s a</code> extracts <code>a</code> from <code>s</code>; so, it's the same thing as <code>(s -> a)</code>, but you can use it in lens chains because its type looks like this:</p><pre>type SimpleGetter s a =
forall r. (a -> Const r a) -> s -> Const r s
</pre><p>Since <code>Const r</code> is a functor, <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code> has the same shape as other lens types and can be composed with them. To get <code>(s -> a)</code> out of a <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code>, choose <code>r ~ a</code> and feed <code>Const :: a -> Const a a</code> to the getter:</p><pre>-- the actual signature is more permissive:
-- <code><a href="Lens-Micro-Extras.html#v:view">view</a></code> :: <code><a href="Lens-Micro.html#t:Getting">Getting</a></code> a s a -> s -> a
<code><a href="Lens-Micro-Extras.html#v:view">view</a></code> :: <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code> s a -> s -> a
<code><a href="Lens-Micro-Extras.html#v:view">view</a></code> getter = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#v:getConst">getConst</a></code> . getter <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a></code>
</pre><p>The actual <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Getter.html#t:Getter">Getter</a></code> from lens is more general:</p><pre>type Getter s a =
forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s
</pre><p>I'm not currently aware of any functions that take lens's <code>Getter</code> but won't accept <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code>, but you should try to avoid exporting <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code>s anyway to minimise confusion. Alternatively, look at <a href="http://hackage.haskell.org/package/microlens-contra">microlens-contra</a>, which provides a fully lens-compatible <code>Getter</code>.</p><p>Lens users: you can convert a <code><a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a></code> to <code>Getter</code> by applying <code>to . view</code> to it.</p></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Getting" class="def">Getting</a> r s a = (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a> r a) -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a> r s <a href="src/Lens-Micro-Type.html#Getting" class="link">Source</a> <a href="#t:Getting" class="selflink">#</a></p><div class="doc"><p>Functions that operate on getters and folds – such as (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>), (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>), (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) – use <code>Getter r s a</code> (with different values of <code>r</code>) to describe what kind of result they need. For instance, (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) needs the getter to be able to return a single value, and so it accepts a getter of type <code>Getting a s a</code>. (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>) wants the getter to gather values together, so it uses <code>Getting (Endo [a]) s a</code> (it could've used <code>Getting [a] s a</code> instead, but it's faster with <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#v:Endo">Endo</a></code>). The choice of <code>r</code> depends on what you want to do with elements you're extracting from <code>s</code>.</p></div></div><div class="top"><p class="src"><a id="v:-94-." class="def">(^.)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> a s a -> a <span class="fixity">infixl 8</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%5E." class="link">Source</a> <a href="#v:-94-." class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) applies a getter to a value; in other words, it gets a value out of a structure using a getter (which can be a lens, traversal, fold, etc.).</p><p>Getting 1st field of a tuple:</p><pre>(<code><a href="Lens-Micro.html#v:-94-.">^.</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code>) :: (a, b) -> a
(<code><a href="Lens-Micro.html#v:-94-.">^.</a></code> <code><a href="Lens-Micro.html#v:_1">_1</a></code>) = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Tuple.html#v:fst">fst</a></code>
</pre><p>When (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) is used with a traversal, it combines all results using the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a></code> instance for the resulting type. For instance, for lists it would be simple concatenation:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>("str","ing") ^. each
</code></strong>"string"
</pre><p>The reason for this is that traversals use <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a></code>, and the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a></code> instance for <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a></code> uses monoid concatenation to combine “effects” of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a></code>.</p><p>A non-operator version of (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) is called <code>view</code>, and it's a bit more general than (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) (it works in <code>MonadReader</code>). If you need the general version, you can get it from <a href="http://hackage.haskell.org/package/microlens-mtl">microlens-mtl</a>; otherwise there's <code><a href="Lens-Micro-Extras.html#v:view">view</a></code> available in <a href="Lens-Micro-Extras.html">Lens.Micro.Extras</a>.</p></div></div><div class="top"><p class="src"><a id="v:to" class="def">to</a> :: (s -> a) -> <a href="Lens-Micro.html#t:SimpleGetter">SimpleGetter</a> s a <a href="src/Lens-Micro.html#to" class="link">Source</a> <a href="#v:to" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:to">to</a></code> creates a getter from any function:</p><pre>a <code><a href="Lens-Micro.html#v:-94-.">^.</a></code> <code><a href="Lens-Micro.html#v:to">to</a></code> f = f a
</pre><p>It's most useful in chains, because it lets you mix lenses and ordinary functions. Suppose you have a record which comes from some third-party library and doesn't have any lens accessors. You want to do something like this:</p><pre>value ^. _1 . field . at 2
</pre><p>However, <code>field</code> isn't a getter, and you have to do this instead:</p><pre>field (value ^. _1) ^. at 2
</pre><p>but now <code>value</code> is in the middle and it's hard to read the resulting code. A variant with <code><a href="Lens-Micro.html#v:to">to</a></code> is prettier and more readable:</p><pre>value ^. _1 . to field . at 2
</pre></div></div><h1 id="g:3">Fold: extracts multiple elements</h1><div class="doc"><p>Folds are getters that can return more than one element (or no elements at all). <a href="http://comonad.com/reader/2015/free-monoids-in-haskell/">Except for some rare cases</a>, a fold is the same thing as <code>(s -> [a])</code>; you can use <code><a href="Lens-Micro.html#v:folding">folding</a></code> to turn any function of type <code>(s -> f a)</code> (where <code>f</code> is <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a></code>) into a fold.</p><p>Folds can be applied to values by using operators like (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>), (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>), etc:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2) ^.. both
</code></strong>[1,2]
</pre><p>A nice thing about folds is that you can combine them with (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#v:-60--62-"><></a></code>) to concatenate their outputs:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3) ^.. (_2 <> _1)
</code></strong>[2,1]
</pre><p>When you need to get all elements of the same type in a complicated structure, (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#v:-60--62-"><></a></code>) can be more helpful than <code><a href="Lens-Micro.html#v:each">each</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>([1,2], 3, [Nothing, Just 4]) ^.. (_1.each <> _2 <> _3.each._Just)
</code></strong>[1,2,3,4]
</pre><p>(Just like setters and getters before, folds can be composed with (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>).)</p><p>The (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#v:-60--62-"><></a></code>) trick works nicely with (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>), too. For instance, if you want to get the 9th element of the list, but would be fine with 5th too if the list is too short, you could combine <code>ix 9</code> and <code>ix 5</code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[0..9] ^? (ix 9 <> ix 5)
</code></strong>Just 9
<code class="prompt">>>> </code><strong class="userinput"><code>[0..8] ^? (ix 9 <> ix 5)
</code></strong>Just 5
<code class="prompt">>>> </code><strong class="userinput"><code>[0..3] ^? (ix 9 <> ix 5)
</code></strong>Nothing
</pre><p>(Unfortunately, this trick won't help you with setting or modifying.)</p></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:SimpleFold" class="def">SimpleFold</a> s a = <span class="keyword">forall</span> r. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a> r => <a href="Lens-Micro.html#t:Getting">Getting</a> r s a <a href="src/Lens-Micro-Type.html#SimpleFold" class="link">Source</a> <a href="#t:SimpleFold" class="selflink">#</a></p><div class="doc"><p>A <code>SimpleFold s a</code> extracts several <code>a</code>s from <code>s</code>; so, it's pretty much the same thing as <code>(s -> [a])</code>, but you can use it with lens operators.</p><p>The actual <code>Fold</code> from lens is more general:</p><pre>type Fold s a =
forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s
</pre><p>There are several functions in lens that accept lens's <code>Fold</code> but won't accept <code><a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a></code>; I'm aware of
<code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:takingWhile">takingWhile</a></code>,
<code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:droppingWhile">droppingWhile</a></code>,
<code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:backwards">backwards</a></code>,
<code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:foldByOf">foldByOf</a></code>,
<code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Fold.html#v:foldMapByOf">foldMapByOf</a></code>.
For this reason, try not to export <code><a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a></code>s if at all possible. <a href="http://hackage.haskell.org/package/microlens-contra">microlens-contra</a> provides a fully lens-compatible <code>Fold</code>.</p><p>Lens users: you can convert a <code><a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a></code> to <code>Fold</code> by applying <code>folded . toListOf</code> to it.</p></div></div><div class="top"><p class="src"><a id="v:-94-.." class="def">(^..)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> [a]) s a -> [a] <span class="fixity">infixl 8</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%5E.." class="link">Source</a> <a href="#v:-94-.." class="selflink">#</a></p><div class="doc"><p><code>s ^.. t</code> returns the list of all values that <code>t</code> gets from <code>s</code>.</p><p>A <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> contains either 0 or 1 values:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 3 ^.. _Just
</code></strong>[3]
</pre><p>Gathering all values in a list of tuples:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[(1,2),(3,4)] ^.. each.each
</code></strong>[1,2,3,4]
</pre></div></div><div class="top"><p class="src"><a id="v:toListOf" class="def">toListOf</a> :: <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> [a]) s a -> s -> [a] <a href="src/Lens-Micro.html#toListOf" class="link">Source</a> <a href="#v:toListOf" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:toListOf">toListOf</a></code> is a synonym for (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>).</p></div></div><div class="top"><p class="src"><a id="v:-94--63-" class="def">(^?)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:First">First</a> a) s a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a <span class="fixity">infixl 8</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%5E%3F" class="link">Source</a> <a href="#v:-94--63-" class="selflink">#</a></p><div class="doc"><p><code>s ^? t</code> returns the 1st element <code>t</code> returns, or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> if <code>t</code> doesn't return anything. It's trivially implemented by passing the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:First">First</a></code> monoid to the getter.</p><p>Safe <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:head">head</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[] ^? each
</code></strong>Nothing
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..3] ^? each
</code></strong>Just 1
</pre><p>Converting <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code> to <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Left 1 ^? _Right
</code></strong>Nothing
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Right 1 ^? _Right
</code></strong>Just 1
</pre><p>A non-operator version of (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) is called <code>preview</code>, and – like <code>view</code> – it's a bit more general than (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) (it works in <code>MonadReader</code>). If you need the general version, you can get it from <a href="http://hackage.haskell.org/package/microlens-mtl">microlens-mtl</a>; otherwise there's <code><a href="Lens-Micro-Extras.html#v:preview">preview</a></code> available in <a href="Lens-Micro-Extras.html">Lens.Micro.Extras</a>.</p></div></div><div class="top"><p class="src"><a id="v:-94--63--33-" class="def">(^?!)</a> :: s -> <a href="Lens-Micro.html#t:Getting">Getting</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Endo">Endo</a> a) s a -> a <span class="fixity">infixl 8</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#%5E%3F%21" class="link">Source</a> <a href="#v:-94--63--33-" class="selflink">#</a></p><div class="doc"><p>(<code><a href="Lens-Micro.html#v:-94--63--33-">^?!</a></code>) is an unsafe variant of (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) – instead of using <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> to indicate that there were no elements returned, it throws an exception.</p></div></div><div class="top"><p class="src"><a id="v:traverseOf_" class="def">traverseOf_</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => <a href="Lens-Micro.html#t:Getting">Getting</a> (Traversed r f) s a -> (a -> f r) -> s -> f () <a href="src/Lens-Micro.html#traverseOf_" class="link">Source</a> <a href="#v:traverseOf_" class="selflink">#</a></p><div class="doc"><p>Apply an action to all targets and discard the result (like <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#v:mapM_">mapM_</a></code> or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#v:traverse_">traverse_</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>traverseOf_ both putStrLn ("hello", "world")
</code></strong>hello
world
</pre><p>Works with anything that allows getting, including lenses and getters (so, anything except for <code><a href="Lens-Micro.html#t:ASetter">ASetter</a></code>). Should be faster than <code><a href="Lens-Micro.html#v:traverseOf">traverseOf</a></code> when you don't need the result.</p></div></div><div class="top"><p class="src"><a id="v:has" class="def">has</a> :: <a href="Lens-Micro.html#t:Getting">Getting</a> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Any">Any</a> s a -> s -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Bool.html#t:Bool">Bool</a> <a href="src/Lens-Micro.html#has" class="link">Source</a> <a href="#v:has" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:has">has</a></code> checks whether a getter (any getter, including lenses, traversals, and folds) returns at least 1 value.</p><p>Checking whether a list is non-empty:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>has each []
</code></strong>False
</pre><p>You can also use it with e.g. <code><a href="Lens-Micro.html#v:_Left">_Left</a></code> (and other 0-or-1 traversals) as a replacement for <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:isNothing">isNothing</a></code>, <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:isJust">isJust</a></code> and other <code>isConstructorName</code> functions:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>has _Left (Left 1)
</code></strong>True
</pre></div></div><div class="top"><p class="src"><a id="v:folded" class="def">folded</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a> f => <a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a> (f a) a <a href="src/Lens-Micro-Internal.html#folded" class="link">Source</a> <a href="#v:folded" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:folded">folded</a></code> is a fold for anything <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a></code>. In a way, it's an opposite of
<code>mapped</code> – the most powerful getter, but can't be used as a setter.</p></div></div><div class="top"><p class="src"><a id="v:folding" class="def">folding</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a> f => (s -> f a) -> <a href="Lens-Micro.html#t:SimpleFold">SimpleFold</a> s a <a href="src/Lens-Micro.html#folding" class="link">Source</a> <a href="#v:folding" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:folding">folding</a></code> creates a fold out of any function that returns a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#t:Foldable">Foldable</a></code> container (for instance, a list):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..5] ^.. folding tail
</code></strong>[2,3,4,5]
</pre></div></div><h1 id="g:4">Lens: a combined getter-and-setter</h1><div class="doc"><p>Lenses are composable “pointers” at values inside some bigger structure (e.g. <code><a href="Lens-Micro.html#v:_1">_1</a></code> points at the first element of a tuple). You can use (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) to get, (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) to set, and (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) to modify:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2) ^. _1
</code></strong>1
<code class="prompt">>>> </code><strong class="userinput"><code>(1,2) & _1 .~ 3
</code></strong>(3,2)
<code class="prompt">>>> </code><strong class="userinput"><code>(1,2) & _1 %~ negate
</code></strong>(-1,2)
</pre><p>To apply a monadic action (or an <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a></code> action, or even a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a></code> action) to the pointed value, just apply the lens directly or use <code><a href="Lens-Micro.html#v:traverseOf">traverseOf</a></code> (or <code><a href="Lens-Micro.html#v:traverseOf_">traverseOf_</a></code> if you don't need the result):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>traverseOf_ _1 print (1,2)
</code></strong>1
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>_1 id (Just 1, 2)
</code></strong>Just (1, 2)
<code class="prompt">>>> </code><strong class="userinput"><code>_1 id (Nothing, 2)
</code></strong>Nothing
</pre><p>A <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> can only point at a single value inside a structure (unlike a <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code>).</p><p>(<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>) composes lenses (i.e. if a <code>B</code> is a part of <code>A</code>, and a <code>C</code> is a part of <code>B</code>, then <code>b.c</code> lets you operate on <code>C</code> inside <code>A</code>). You can create lenses with <code><a href="Lens-Micro.html#v:lens">lens</a></code>, or you can write them by hand.</p><p>There are several ways to get lenses for some datatype:</p><ul><li>They can already be provided by the package, by <code>microlens</code>, or by some other package like <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a>.</li><li>They can be provided by some unofficial package (like <a href="http://hackage.haskell.org/package/microlens-aeson">microlens-aeson</a>).</li><li>You can get them by combining already existing lenses.</li><li>You can derive them with Template Haskell (with <a href="http://hackage.haskell.org/package/microlens-th">microlens-th</a>).</li><li>You can write them with <code><a href="Lens-Micro.html#v:lens">lens</a></code> if you have a setter and a getter. It's a simple and good way.</li><li>You can write them manually (sometimes it looks a bit better than the variant with <code><a href="Lens-Micro.html#v:lens">lens</a></code>, sometimes worse). The generic template is as follows:</li></ul><pre>somelens :: Lens s t a b
-- “f” is the “a -> f b” function, “s” is the structure.
somelens f s =
let
a = ... -- Extract the value from “s”.
rebuildWith b = ... -- Write a function which would
-- combine “s” and modified value
-- to produce new structure.
in
rebuildWith <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f a -- Apply the structure-producing
-- function to the modified value.
</pre><p>Here's the <code><a href="Lens-Micro.html#v:_1">_1</a></code> lens, for instance:</p><pre><code><a href="Lens-Micro.html#v:_1">_1</a></code> :: <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> (a, x) (b, x) a b
<code><a href="Lens-Micro.html#v:_1">_1</a></code> f (a, x) = (\b -> (b, x)) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f a
</pre><p>Here's a more complicated lens, which extracts <em>several</em> values from a structure (in a tuple):</p><pre>type Age = Int
type City = String
type Country = String
data Person = Person Age City Country
-- This lens lets you access all location-related information about a person.
location :: <code><a href="Lens-Micro.html#t:Lens-39-">Lens'</a></code> Person (City, Country)
location f (Person age city country) =
(\(city', country') -> Person age city' country') <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f (city, country)
</pre><p>You even can choose to use a lens to present <em>all</em> information contained in the structure (in a different way). Such lenses are called <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Iso.html#t:Iso">Iso</a></code> in lens's terminology. For instance (assuming you don't mind functions that can error out), here's a lens which lets you act on the string representation of a value:</p><pre>string :: (Read a, Show a) => <code><a href="Lens-Micro.html#t:Lens-39-">Lens'</a></code> a String
string f s = read <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f (show s)
</pre><p>Using it to reverse a number:</p><pre>>>> 123 <code><a href="Lens-Micro.html#v:-38-">&</a></code> string <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> reverse
321
</pre></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Lens" class="def">Lens</a> s t a b = <span class="keyword">forall</span> f. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a> f => (a -> f b) -> s -> f t <a href="src/Lens-Micro-Type.html#Lens" class="link">Source</a> <a href="#t:Lens" class="selflink">#</a></p><div class="doc"><p><code>Lens s t a b</code> is the lowest common denominator of a setter and a getter, something that has the power of both; it has a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a></code> constraint, and since both <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Const.html#t:Const">Const</a></code> and <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor-Identity.html#t:Identity">Identity</a></code> are functors, it can be used whenever a getter or a setter is needed.</p><ul><li><code>a</code> is the type of the value inside of structure</li><li><code>b</code> is the type of the replaced value</li><li><code>s</code> is the type of the whole structure</li><li><code>t</code> is the type of the structure after replacing <code>a</code> in it with <code>b</code></li></ul></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Lens-39-" class="def">Lens'</a> s a = <a href="Lens-Micro.html#t:Lens">Lens</a> s s a a <a href="src/Lens-Micro-Type.html#Lens%27" class="link">Source</a> <a href="#t:Lens-39-" class="selflink">#</a></p><div class="doc"><p>This is a type alias for monomorphic lenses which don't change the type of the container (or of the value inside).</p></div></div><div class="top"><p class="src"><a id="v:lens" class="def">lens</a> :: (s -> a) -> (s -> b -> t) -> <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro.html#lens" class="link">Source</a> <a href="#v:lens" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:lens">lens</a></code> creates a <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> from a getter and a setter. The resulting lens isn't the most effective one (because of having to traverse the structure twice when modifying), but it shouldn't matter much.</p><p>A (partial) lens for list indexing:</p><pre>ix :: Int -> <code><a href="Lens-Micro.html#t:Lens-39-">Lens'</a></code> [a] a
ix i = <code><a href="Lens-Micro.html#v:lens">lens</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:-33--33-">!!</a></code> i) -- getter
(\s b -> take i s ++ b : drop (i+1) s) -- setter
</pre><p>Usage:</p><pre>>>> [1..9] <code><a href="Lens-Micro.html#v:-94-.">^.</a></code> ix 3
4
>>> [1..9] & ix 3 <code><a href="Lens-Micro.html#v:-37--126-">%~</a></code> negate
[1,2,3,-4,5,6,7,8,9]
</pre><p>When getting, the setter is completely unused; when setting, the getter is unused. Both are used only when the value is being modified. For instance, here we define a lens for the 1st element of a list, but instead of a legitimate getter we use <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:undefined">undefined</a></code>. Then we use the resulting lens for <em>setting</em> and it works, which proves that the getter wasn't used:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1,2,3] & lens undefined (\s b -> b : tail s) .~ 10
</code></strong>[10,2,3]
</pre></div></div><div class="top"><p class="src"><a id="v:at" class="def">at</a> :: <a href="Lens-Micro-Internal.html#t:At">At</a> m => <a href="Lens-Micro-Internal.html#t:Index">Index</a> m -> <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> m (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> (<a href="Lens-Micro-Internal.html#t:IxValue">IxValue</a> m)) <a href="src/Lens-Micro-Internal.html#at" class="link">Source</a> <a href="#v:at" class="selflink">#</a></p><div class="doc"><p>This lens lets you read, write, or delete elements in <code>Map</code>-like structures. It returns <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> when the value isn't found, just like <code>lookup</code>:</p><pre>Data.Map.lookup k m = m <code><a href="Lens-Micro.html#v:-94-.">^.</a></code> at k
</pre><p>However, it also lets you insert and delete values by setting the value to <code><code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code> value</code> or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code>:</p><pre>Data.Map.insert k a m = m <code><a href="Lens-Micro.html#v:-38-">&</a></code> at k <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> Just a
Data.Map.delete k m = m <code><a href="Lens-Micro.html#v:-38-">&</a></code> at k <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> Nothing
</pre><p>Or you could use (<code><a href="Lens-Micro.html#v:-63--126-">?~</a></code>) instead of (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>):</p><pre>Data.Map.insert k a m = m <code><a href="Lens-Micro.html#v:-38-">&</a></code> at k <code><a href="Lens-Micro.html#v:-63--126-">?~</a></code> a
</pre><p>Note that <code><a href="Lens-Micro.html#v:at">at</a></code> doesn't work for arrays or lists. You can't delete an arbitrary element from an array (what would be left in its place?), and you can't set an arbitrary element in a list because if the index is out of list's bounds, you'd have to somehow fill the stretch between the last element and the element you just inserted (i.e. <code>[1,2,3] & at 10 .~ 5</code> is undefined). If you want to modify an already existing value in an array or list, you should use <code><a href="Lens-Micro.html#v:ix">ix</a></code> instead.</p><p><code><a href="Lens-Micro.html#v:at">at</a></code> is often used with <code><a href="Lens-Micro.html#v:non">non</a></code>. See the documentation of <code><a href="Lens-Micro.html#v:non">non</a></code> for examples.</p><p>Note that <code><a href="Lens-Micro.html#v:at">at</a></code> isn't strict for <code>Map</code>, even if you're using <code>Data.Map.Strict</code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Data.Map.Strict.size (Data.Map.Strict.empty & at 1 .~ Just undefined)
</code></strong>1
</pre><p>The reason for such behavior is that there's actually no “strict <code>Map</code>” type; <code>Data.Map.Strict</code> just provides some strict functions for ordinary <code>Map</code>s.</p><p>This package doesn't actually provide any instances for <code><a href="Lens-Micro.html#v:at">at</a></code>, but there are instances for <code>Map</code> and <code>IntMap</code> in <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a> and an instance for <code>HashMap</code> in <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a>.</p></div></div><div class="top"><p class="src"><a id="v:_1" class="def">_1</a> :: <a href="Lens-Micro-Internal.html#t:Field1">Field1</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro-Internal.html#_1" class="link">Source</a> <a href="#v:_1" class="selflink">#</a></p><div class="doc"><p>Gives access to the 1st field of a tuple (up to 5-tuples).</p><p>Getting the 1st component:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3,4,5) ^. _1
</code></strong>1
</pre><p>Setting the 1st component:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3) & _1 .~ 10
</code></strong>(10,2,3)
</pre><p>Note that this lens is lazy, and can set fields even of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:undefined">undefined</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>set _1 10 undefined :: (Int, Int)
</code></strong>(10,*** Exception: Prelude.undefined
</pre><p>This is done to avoid violating a lens law stating that you can get back what you put:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>view _1 . set _1 10 $ (undefined :: (Int, Int))
</code></strong>10
</pre><p>The implementation (for 2-tuples) is:</p><pre><code><a href="Lens-Micro.html#v:_1">_1</a></code> f t = (,) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Tuple.html#v:fst">fst</a></code> t)
<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:-60--42--62-"><*></a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Tuple.html#v:snd">snd</a></code> t)
</pre><p>or, alternatively,</p><pre><code><a href="Lens-Micro.html#v:_1">_1</a></code> f ~(a,b) = (\a' -> (a',b)) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f a
</pre><p>(where <code>~</code> means a <a href="https://wiki.haskell.org/Lazy_pattern_match">lazy pattern</a>).</p><p><code><a href="Lens-Micro.html#v:_2">_2</a></code>, <code><a href="Lens-Micro.html#v:_3">_3</a></code>, <code><a href="Lens-Micro.html#v:_4">_4</a></code>, and <code><a href="Lens-Micro.html#v:_5">_5</a></code> are also available (see below).</p></div></div><div class="top"><p class="src"><a id="v:_2" class="def">_2</a> :: <a href="Lens-Micro-Internal.html#t:Field2">Field2</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro-Internal.html#_2" class="link">Source</a> <a href="#v:_2" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:_3" class="def">_3</a> :: <a href="Lens-Micro-Internal.html#t:Field3">Field3</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro-Internal.html#_3" class="link">Source</a> <a href="#v:_3" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:_4" class="def">_4</a> :: <a href="Lens-Micro-Internal.html#t:Field4">Field4</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro-Internal.html#_4" class="link">Source</a> <a href="#v:_4" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:_5" class="def">_5</a> :: <a href="Lens-Micro-Internal.html#t:Field5">Field5</a> s t a b => <a href="Lens-Micro.html#t:Lens">Lens</a> s t a b <a href="src/Lens-Micro-Internal.html#_5" class="link">Source</a> <a href="#v:_5" class="selflink">#</a></p></div><h1 id="g:5">Iso: a lens that only changes the representation</h1><div class="doc"><p>Isos (or isomorphisms) are lenses that convert a value instead of targeting a part of it; in other words, inside of every list lives a reversed list, inside of every strict <code>Text</code> lives a lazy <code>Text</code>, and inside of every <code>(a, b)</code> lives a <code>(b, a)</code>. Since an isomorphism doesn't lose any information, it's possible to <em>reverse</em> it and use it in the opposite direction by using <code>from</code> from the lens library:</p><pre>from :: Iso' s a -> Iso' a s
</pre><p>However, it's not possible for microlens to export isomorphisms, because their type depends on <code><a href="http://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html#t:Profunctor">Profunctor</a></code>, which resides in the <a href="http://hackage.haskell.org/package/profunctors">profunctors</a> library, which is a somewhat huge dependency. So, all isomorphisms included here are lenses instead (and thus you can't use them in the opposite direction).</p></div><div class="top"><p class="src"><a id="v:strict" class="def">strict</a> :: <a href="Lens-Micro-Internal.html#t:Strict">Strict</a> lazy strict => <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> lazy strict <a href="src/Lens-Micro-Internal.html#strict" class="link">Source</a> <a href="#v:strict" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:strict">strict</a></code> lets you convert between strict and lazy versions of a datatype:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>let someText = "hello" :: Lazy.Text
</code></strong><code class="prompt">>>> </code><strong class="userinput"><code>someText ^. strict
</code></strong>"hello" :: Strict.Text
</pre><p>It can also be useful if you have a function that works on a strict type but your type is lazy:</p><pre>stripDiacritics :: Strict.Text -> Strict.Text
stripDiacritics = ...
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>let someText = "Paul Erdős" :: Lazy.Text
</code></strong><code class="prompt">>>> </code><strong class="userinput"><code>someText & strict %~ stripDiacritics
</code></strong>"Paul Erdos" :: Lazy.Text
</pre><p><code><a href="Lens-Micro.html#v:strict">strict</a></code> works on <code>ByteString</code> and <code>StateT</code>/<code>WriterT</code>/<code>RWST</code> if you use <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a>, and additionally on <code>Text</code> if you use <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a>.</p></div></div><div class="top"><p class="src"><a id="v:lazy" class="def">lazy</a> :: <a href="Lens-Micro-Internal.html#t:Strict">Strict</a> lazy strict => <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> strict lazy <a href="src/Lens-Micro-Internal.html#lazy" class="link">Source</a> <a href="#v:lazy" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:lazy">lazy</a></code> is like <code><a href="Lens-Micro.html#v:strict">strict</a></code> but works in opposite direction:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>let someText = "hello" :: Strict.Text
</code></strong><code class="prompt">>>> </code><strong class="userinput"><code>someText ^. lazy
</code></strong>"hello" :: Lazy.Text
</pre></div></div><div class="top"><p class="src"><a id="v:non" class="def">non</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Eq.html#t:Eq">Eq</a> a => a -> <a href="Lens-Micro.html#t:Lens-39-">Lens'</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) a <a href="src/Lens-Micro.html#non" class="link">Source</a> <a href="#v:non" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:non">non</a></code> lets you “relabel” a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> by equating <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> to an arbitrary value (which you can choose):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 ^. non 0
</code></strong>1
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Nothing ^. non 0
</code></strong>0
</pre><p>The most useful thing about <code><a href="Lens-Micro.html#v:non">non</a></code> is that relabeling also works in other direction. If you try to <code><a href="Lens-Micro.html#v:set">set</a></code> the “forbidden” value, it'll be turned to <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 & non 0 .~ 0
</code></strong>Nothing
</pre><p>Setting anything else works just fine:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 & non 0 .~ 5
</code></strong>Just 5
</pre><p>Same happens if you try to modify a value:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 & non 0 %~ subtract 1
</code></strong>Nothing
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 & non 0 .~ (+ 1)
</code></strong>Just 2
</pre><p><code><a href="Lens-Micro.html#v:non">non</a></code> is often useful when combined with <code><a href="Lens-Micro.html#v:at">at</a></code>. For instance, if you have a map of songs and their playcounts, it makes sense not to store songs with 0 plays in the map; <code><a href="Lens-Micro.html#v:non">non</a></code> can act as a filter that wouldn't pass such entries.</p><p>Decrease playcount of a song to 0, and it'll be gone:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>fromList [("Soon",1),("Yesterday",3)] & at "Soon" . non 0 %~ subtract 1
</code></strong>fromList [("Yesterday",3)]
</pre><p>Try to add a song with 0 plays, and it won't be added:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 0
</code></strong>fromList [("Yesterday",3)]
</pre><p>But it will be added if you set any other number:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 1
</code></strong>fromList [("Soon",1),("Yesterday",3)]
</pre><p><code><a href="Lens-Micro.html#v:non">non</a></code> is also useful when working with nested maps. Here a nested map is created when it's missing:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Map.empty & at "Dez Mona" . non Map.empty . at "Soon" .~ Just 1
</code></strong>fromList [("Dez Mona",fromList [("Soon",1)])]
</pre><p>and here it is deleted when its last entry is deleted (notice that <code><a href="Lens-Micro.html#v:non">non</a></code> is used twice here):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>fromList [("Dez Mona",fromList [("Soon",1)])] & at "Dez Mona" . non Map.empty . at "Soon" . non 0 %~ subtract 1
</code></strong>fromList []
</pre><p>To understand the last example better, observe the flow of values in it:</p><ul><li>the map goes into <code>at "Dez Mona"</code></li><li>the nested map (wrapped into <code>Just</code>) goes into <code>non Map.empty</code></li><li><code>Just</code> is unwrapped and the nested map goes into <code>at "Soon"</code></li><li><code>Just 1</code> is unwrapped by <code>non 0</code></li></ul><p>Then the final value – i.e. 1 – is modified by <code>subtract 1</code> and the result (which is 0) starts flowing backwards:</p><ul><li><code>non 0</code> sees the 0 and produces a <code>Nothing</code></li><li><code>at "Soon"</code> sees <code>Nothing</code> and deletes the corresponding value from the map</li><li>the resulting empty map is passed to <code>non Map.empty</code>, which sees that it's empty and thus produces <code>Nothing</code></li><li><code>at "Dez Mona"</code> sees <code>Nothing</code> and removes the key from the map</li></ul></div></div><h1 id="g:6">Traversal: a lens iterating over several elements</h1><div class="doc"><p>Traversals are like lenses but they can point at multiple values. Use (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>) to get all values, (<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) to get the 1st value, (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) to set values, (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) to modify them. (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code>) composes traversals just as it composes lenses. (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) can be used with traversals as well, but don't confuse it with (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>) – (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>) gets all traversed values, (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) combines traversed values using the (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#v:-60--62-"><></a></code>) operation (if the values are instances of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Monoid.html#t:Monoid">Monoid</a></code>; if they aren't, it won't compile). <code><a href="Lens-Micro.html#v:traverseOf">traverseOf</a></code> and <code><a href="Lens-Micro.html#v:traverseOf_">traverseOf_</a></code> apply an action to all pointed values of a traversal.</p><p>Traversals don't differ from lenses when it comes to setting – you can use usual (<code><a href="Lens-Micro.html#v:-37--126-">%~</a></code>) and (<code><a href="Lens-Micro.html#v:.-126-">.~</a></code>) to modify and set values. Getting is a bit different, because you have to decide what to do in the case of multiple values. In particular, you can use these combinators (as well as everything else in the “Folds” section):</p><ul><li>(<code><a href="Lens-Micro.html#v:-94-..">^..</a></code>) gets a list of values</li><li>(<code><a href="Lens-Micro.html#v:-94--63-">^?</a></code>) gets the 1st value (or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> if there are no values)</li><li>(<code><a href="Lens-Micro.html#v:-94--63--33-">^?!</a></code>) gets the 1st value and throws an exception if there are no values</li></ul><p>If you are sure that the traversal will traverse at least one value, you can convert it to a lens with <code><a href="Lens-Micro.html#v:singular">singular</a></code>.</p><p><code><a href="Lens-Micro.html#v:traversed">traversed</a></code> is a universal traversal for anything that belongs to the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a></code> typeclass. However, in many cases <code><a href="Lens-Micro.html#v:each">each</a></code> works as well and is shorter and nicer-looking.</p></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Traversal" class="def">Traversal</a> s t a b = <span class="keyword">forall</span> f. <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a> f => (a -> f b) -> s -> f t <a href="src/Lens-Micro-Type.html#Traversal" class="link">Source</a> <a href="#t:Traversal" class="selflink">#</a></p><div class="doc"><p><code>Traversal s t a b</code> is a generalisation of <code><a href="Lens-Micro.html#t:Lens">Lens</a></code> which allows many targets (possibly 0). It's achieved by changing the constraint to <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a></code> instead of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#t:Functor">Functor</a></code> – indeed, the point of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#t:Applicative">Applicative</a></code> is that you can combine effects, which is just what we need to have many targets.</p><p>Ultimately, traversals should follow 2 laws:</p><pre>t pure ≡ pure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)
</pre><p>The 1st law states that you can't change the shape of the structure or do anything funny with elements (traverse elements which aren't in the structure, create new elements out of thin air, etc.). The 2nd law states that you should be able to fuse 2 identical traversals into one. For a more detailed explanation of the laws, see <a href="http://artyom.me/lens-over-tea-2#traversal-laws">this blog post</a> (if you prefer rambling blog posts), or <a href="https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf">The Essence Of The Iterator Pattern</a> (if you prefer papers).</p><p>Traversing any value twice is a violation of traversal laws. You can, however, traverse values in any order.</p></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Traversal-39-" class="def">Traversal'</a> s a = <a href="Lens-Micro.html#t:Traversal">Traversal</a> s s a a <a href="src/Lens-Micro-Type.html#Traversal%27" class="link">Source</a> <a href="#t:Traversal-39-" class="selflink">#</a></p><div class="doc"><p>This is a type alias for monomorphic traversals which don't change the type of the container (or of the values inside).</p></div></div><div class="top"><p class="src"><a id="v:traverseOf" class="def">traverseOf</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> f s t a b -> (a -> f b) -> s -> f t <a href="src/Lens-Micro.html#traverseOf" class="link">Source</a> <a href="#v:traverseOf" class="selflink">#</a></p><div class="doc"><p>Apply an action to all targets (like <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#v:mapM">mapM</a></code> or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#v:traverse">traverse</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>traverseOf both readFile ("file1", "file2")
</code></strong>(<contents of file1>, <contents of file2>)
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>traverseOf _1 id (Just 1, 2)
</code></strong>Just (1, 2)
<code class="prompt">>>> </code><strong class="userinput"><code>traverseOf _1 id (Nothing, 2)
</code></strong>Nothing
</pre><p>You can also just apply the lens/traversal directly (but <code><a href="Lens-Micro.html#v:traverseOf">traverseOf</a></code> might be more readable):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>both readFile ("file1", "file2")
</code></strong>(<contents of file1>, <contents of file2>)
</pre><p>If you don't need the result, use <code><a href="Lens-Micro.html#v:traverseOf_">traverseOf_</a></code>.</p></div></div><div class="top"><p class="src"><a id="v:singular" class="def">singular</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a a -> <a href="Lens-Micro.html#t:Lens">Lens</a> s t a a <a href="src/Lens-Micro.html#singular" class="link">Source</a> <a href="#v:singular" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:singular">singular</a></code> turns a traversal into a lens that behaves like a single-element traversal:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1,2,3] ^. signular each
</code></strong>1
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1,2,3] & singular each %~ negate
</code></strong>[-1,2,3]
</pre><p>If there is nothing to return, it'll throw an error:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[] ^. singular each
</code></strong>*** Exception: Lens.Micro.singular: empty traversal
</pre><p>However, it won't fail if you are merely setting the value:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[] & singular each %~ negate
</code></strong></pre></div></div><div class="top"><p class="src"><a id="v:failing" class="def">failing</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b -> <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b -> <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b <span class="fixity">infixl 5</span><span class="rightedge"></span> <a href="src/Lens-Micro.html#failing" class="link">Source</a> <a href="#v:failing" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:failing">failing</a></code> lets you chain traversals together; if the 1st traversal fails, the 2nd traversal will be used.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>([1,2],[3]) & failing (_1.each) (_2.each) .~ 0
</code></strong>([0,0],[3])
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>([],[3]) & failing (_1.each) (_2.each) .~ 0
</code></strong>([],[0])
</pre><p>Note that the resulting traversal won't be valid unless either both traversals don't touch each others' elements, or both traversals return exactly the same results. To see an example of how <code><a href="Lens-Micro.html#v:failing">failing</a></code> can generate invalid traversals, see <a href="http://stackoverflow.com/questions/27138856/why-does-failing-from-lens-produce-invalid-traversals">this Stackoverflow question</a>.</p></div></div><div class="top"><p class="src"><a id="v:filtered" class="def">filtered</a> :: (a -> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Bool.html#t:Bool">Bool</a>) -> <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> a a <a href="src/Lens-Micro.html#filtered" class="link">Source</a> <a href="#v:filtered" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:filtered">filtered</a></code> is a traversal that filters elements “passing” thru it:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3,4) ^.. each
</code></strong>[1,2,3,4]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3,4) ^.. each . filtered even
</code></strong>[2,4]
</pre><p>It also can be used to modify elements selectively:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2,3,4) & each . filtered even %~ (*100)
</code></strong>(1,200,3,400)
</pre><p>The implementation of <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> is very simple. Consider this traversal, which always “traverses” just the value it's given:</p><pre>id :: <code><a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a></code> a a
id f s = f s
</pre><p>And this traversal, which traverses nothing (in other words, <em>doesn't</em> traverse the value it's given):</p><pre>ignored :: <code><a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a></code> a a
ignored f s = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> s
</pre><p>And now combine them into a traversal that conditionally traverses the value it's given, and you get <code><a href="Lens-Micro.html#v:filtered">filtered</a></code>:</p><pre>filtered :: (a -> Bool) -> <code><a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a></code> a a
filtered p s = if p s then f s else <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> s
</pre><p>By the way, note that <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> can generate illegal traversals – sometimes this can bite you. In particular, an optimisation that should be safe becomes unsafe. (To the best of my knowledge, this optimisation never happens automatically. If you just use <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> to modify/view something, you're safe. If you don't define any traversals that use <code><a href="Lens-Micro.html#v:filtered">filtered</a></code>, you're safe too.)</p><p>Let's use <code>evens</code> as an example:</p><pre>evens = <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:even">even</a></code>
</pre><p>If <code>evens</code> was a legal traversal, you'd be able to fuse several applications of <code>evens</code> like this:</p><pre><code><a href="Lens-Micro.html#v:over">over</a></code> evens f <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="Lens-Micro.html#v:over">over</a></code> evens g = <code><a href="Lens-Micro.html#v:over">over</a></code> evens (f <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> g)
</pre><p>Unfortunately, in case of <code>evens</code> this isn't a correct optimisation:</p><ul><li>the left-side variant applies <code>g</code> to all even numbers, and then applies <code>f</code> to all even numbers that are left after <code>f</code> (because <code>f</code> might've turned some even numbers into odd ones)</li><li>the right-side variant applies <code>f</code> and <code>g</code> to all even numbers</li></ul><p>Of course, when you are careful and know what you're doing, you won't try to make such an optimisation. However, if you export an illegal traversal created with <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> and someone tries to use it, they might mistakenly assume that it's legal, do the optimisation, and silently get an incorrect result.</p><p>If you are using <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> with some another traversal that doesn't overlap with -whatever the predicate checks-, the resulting traversal will be legal. For instance, here the predicate looks at the 1st element of a tuple, but the resulting traversal only gives you access to the 2nd:</p><pre>pairedWithEvens :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> [(Int, a)] [(Int, b)] a b
pairedWithEvens = <code><a href="Lens-Micro.html#v:each">each</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="Lens-Micro.html#v:filtered">filtered</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#v:even">even</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Tuple.html#v:fst">fst</a></code>) <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="Lens-Micro.html#v:_2">_2</a></code>
</pre><p>Since you can't do anything with the 1st components thru this traversal, the following holds for any <code>f</code> and <code>g</code>:</p><pre><code><a href="Lens-Micro.html#v:over">over</a></code> pairedWithEvens f <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="Lens-Micro.html#v:over">over</a></code> pairedWithEvens g = <code><a href="Lens-Micro.html#v:over">over</a></code> pairedWithEvens (f <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> g)
</pre></div></div><div class="top"><p class="src"><a id="v:both" class="def">both</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (a, a) (b, b) a b <a href="src/Lens-Micro.html#both" class="link">Source</a> <a href="#v:both" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:both">both</a></code> traverses both fields of a tuple. Unlike <code><a href="http://hackage.haskell.org/package/lens/docs/Control-Lens-Traversal.html#v:both">both</a></code> from lens, it only works for pairs – not for triples or <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code>.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>("str","ing") ^. both
</code></strong>"string"
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>("str","ing") & both %~ reverse
</code></strong>("rts","gni")
</pre></div></div><div class="top"><p class="src"><a id="v:traversed" class="def">traversed</a> :: <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a> f => <a href="Lens-Micro.html#t:Traversal">Traversal</a> (f a) (f b) a b <a href="src/Lens-Micro-Internal.html#traversed" class="link">Source</a> <a href="#v:traversed" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:traversed">traversed</a></code> traverses any <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a></code> container (list, vector, <code>Map</code>, <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code>, you name it):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 ^.. traversed
</code></strong>[1]
</pre><p><code><a href="Lens-Micro.html#v:traversed">traversed</a></code> is the same as <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#v:traverse">traverse</a></code>, but can be faster thanks to magic rewrite rules.</p></div></div><div class="top"><p class="src"><a id="v:each" class="def">each</a> :: <a href="Lens-Micro-Internal.html#t:Each">Each</a> s t a b => <a href="Lens-Micro.html#t:Traversal">Traversal</a> s t a b <a href="src/Lens-Micro-Internal.html#each" class="link">Source</a> <a href="#v:each" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:each">each</a></code> tries to be a universal <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> – it behaves like <code><a href="Lens-Micro.html#v:traversed">traversed</a></code> in most situations, but also adds support for e.g. tuples with same-typed values:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(1,2) & each %~ succ
</code></strong>(2,3)
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>["x", "y", "z"] ^. each
</code></strong>"xyz"
</pre><p>However, note that <code><a href="Lens-Micro.html#v:each">each</a></code> doesn't work on <em>every</em> instance of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a></code>. If you have a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#t:Traversable">Traversable</a></code> which isn't supported by <code><a href="Lens-Micro.html#v:each">each</a></code>, you can use <code><a href="Lens-Micro.html#v:traversed">traversed</a></code> instead. Personally, I like using <code><a href="Lens-Micro.html#v:each">each</a></code> instead of <code><a href="Lens-Micro.html#v:traversed">traversed</a></code> whenever possible – it's shorter and more descriptive.</p><p>You can use <code><a href="Lens-Micro.html#v:each">each</a></code> with these things:</p><pre><code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> [a] [b] a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> a) (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> b) a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (a,a) (b,b) a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (a,a,a) (b,b,b) a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (a,a,a,a) (b,b,b,b) a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (a,a,a,a,a) (b,b,b,b,b) a b
<code><a href="Lens-Micro.html#v:each">each</a></code> :: (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#t:RealFloat">RealFloat</a></code> a, <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Prelude.html#t:RealFloat">RealFloat</a></code> b) => <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Complex.html#t:Complex">Complex</a></code> a) (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Complex.html#t:Complex">Complex</a></code> b) a b
</pre><p>You can also use <code><a href="Lens-Micro.html#v:each">each</a></code> with types from <a href="http://hackage.haskell.org/package/array">array</a>, <a href="http://hackage.haskell.org/package/bytestring">bytestring</a>, and <a href="http://hackage.haskell.org/package/containers">containers</a> by using <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a>, or additionally with types from <a href="http://hackage.haskell.org/package/vector">vector</a>, <a href="http://hackage.haskell.org/package/text">text</a>, and <a href="http://hackage.haskell.org/package/unordered-containers">unordered-containers</a> by using <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a>.</p></div></div><div class="top"><p class="src"><a id="v:ix" class="def">ix</a> :: <a href="Lens-Micro-Internal.html#t:Ixed">Ixed</a> m => <a href="Lens-Micro-Internal.html#t:Index">Index</a> m -> <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> m (<a href="Lens-Micro-Internal.html#t:IxValue">IxValue</a> m) <a href="src/Lens-Micro-Internal.html#ix" class="link">Source</a> <a href="#v:ix" class="selflink">#</a></p><div class="doc"><p>This traversal lets you access (and update) an arbitrary element in a list, array, <code>Map</code>, etc. (If you want to insert or delete elements as well, look at <code><a href="Lens-Micro.html#v:at">at</a></code>.)</p><p>An example for lists:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[0..5] & ix 3 .~ 10
</code></strong>[0,1,2,10,4,5]
</pre><p>You can use it for getting, too:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[0..5] ^? ix 3
</code></strong>Just 3
</pre><p>Of course, the element may not be present (which means that you can use <code><a href="Lens-Micro.html#v:ix">ix</a></code> as a safe variant of (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/GHC-OldList.html#v:-33--33-">!!</a></code>)):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[0..5] ^? ix 10
</code></strong>Nothing
</pre><p>Another useful instance is the one for functions – it lets you modify their outputs for specific inputs. For instance, here's <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#v:maximum">maximum</a></code> that returns 0 when the list is empty (instead of throwing an exception):</p><pre>maximum0 = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Foldable.html#v:maximum">maximum</a></code> <code><a href="Lens-Micro.html#v:-38-">&</a></code> <code><a href="Lens-Micro.html#v:ix">ix</a></code> [] <code><a href="Lens-Micro.html#v:.-126-">.~</a></code> 0
</pre><p>The following instances are provided in this package:</p><pre><code><a href="Lens-Micro.html#v:ix">ix</a></code> :: <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Int.html#t:Int">Int</a></code> -> <code><a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a></code> [a] a
<code><a href="Lens-Micro.html#v:ix">ix</a></code> :: (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Eq.html#t:Eq">Eq</a></code> e) => e -> <code><a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a></code> (e -> a) a
</pre><p>You can also use <code><a href="Lens-Micro.html#v:ix">ix</a></code> with types from <a href="http://hackage.haskell.org/package/array">array</a>, <a href="http://hackage.haskell.org/package/bytestring">bytestring</a>, and <a href="http://hackage.haskell.org/package/containers">containers</a> by using <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a>, or additionally with types from <a href="http://hackage.haskell.org/package/vector">vector</a>, <a href="http://hackage.haskell.org/package/text">text</a>, and <a href="http://hackage.haskell.org/package/unordered-containers">unordered-containers</a> by using <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a>.</p></div></div><div class="top"><p class="src"><a id="v:_head" class="def">_head</a> :: <a href="Lens-Micro-Internal.html#t:Cons">Cons</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s a <a href="src/Lens-Micro.html#_head" class="link">Source</a> <a href="#v:_head" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_head">_head</a></code> traverses the 1st element of something (usually a list, but can also be a <code>Seq</code>, etc):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..5] ^? _head
</code></strong>Just 1
</pre><p>It can be used to modify too, as in this example where the 1st letter of a sentence is capitalised:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>"mary had a little lamb." & _head %~ toTitle
</code></strong>"Mary had a little lamb."
</pre><p>The reason it's a traversal and not a lens is that there's nothing to traverse when the list is empty:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[] ^? _head
</code></strong>Nothing
</pre><p>This package only lets you use <code><a href="Lens-Micro.html#v:_head">_head</a></code> on lists, but if you use <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a> you get instances for <code>ByteString</code> and <code>Seq</code>, and if you use <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a> you additionally get instances for <code>Text</code> and <code>Vector</code>.</p></div></div><div class="top"><p class="src"><a id="v:_tail" class="def">_tail</a> :: <a href="Lens-Micro-Internal.html#t:Cons">Cons</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s s <a href="src/Lens-Micro.html#_tail" class="link">Source</a> <a href="#v:_tail" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_tail">_tail</a></code> gives you access to the tail of a list (or <code>Seq</code>, etc):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..5] ^? _tail
</code></strong>Just [2,3,4,5]
</pre><p>You can modify the tail as well:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[4,1,2,3] & _tail %~ reverse
</code></strong>[4,3,2,1]
</pre><p>Since lists are monoids, you can use <code><a href="Lens-Micro.html#v:_tail">_tail</a></code> with plain (<code><a href="Lens-Micro.html#v:-94-.">^.</a></code>) (and then it'll return an empty list if you give it an empty list):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[1..5] ^. _tail
</code></strong>[2,3,4,5]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[] ^. _tail
</code></strong>[]
</pre><p>If you want to traverse each <em>element</em> of the tail, use <code><a href="Lens-Micro.html#v:_tail">_tail</a></code> with <code><a href="Lens-Micro.html#v:each">each</a></code>:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>"I HATE CAPS." & _tail.each %~ toLower
</code></strong>"I hate caps."
</pre><p>This package only lets you use <code><a href="Lens-Micro.html#v:_tail">_tail</a></code> on lists, but if you use <a href="http://hackage.haskell.org/package/microlens-ghc">microlens-ghc</a> you get instances for <code>ByteString</code> and <code>Seq</code>, and if you use <a href="http://hackage.haskell.org/package/microlens-platform">microlens-platform</a> you additionally get instances for <code>Text</code> and <code>Vector</code>.</p></div></div><div class="top"><p class="src"><a id="v:_init" class="def">_init</a> :: <a href="Lens-Micro-Internal.html#t:Snoc">Snoc</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s s <a href="src/Lens-Micro.html#_init" class="link">Source</a> <a href="#v:_init" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_init">_init</a></code> gives you access to all-but-the-last elements of the list:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>"Hello." ^. _init
</code></strong>"Hello"
</pre><p>See documentation for <code><a href="Lens-Micro.html#v:_tail">_tail</a></code>, as <code><a href="Lens-Micro.html#v:_init">_init</a></code> and <code><a href="Lens-Micro.html#v:_tail">_tail</a></code> are pretty similar.</p></div></div><div class="top"><p class="src"><a id="v:_last" class="def">_last</a> :: <a href="Lens-Micro-Internal.html#t:Snoc">Snoc</a> s s a a => <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> s a <a href="src/Lens-Micro.html#_last" class="link">Source</a> <a href="#v:_last" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_last">_last</a></code> gives you access to the last element of the list:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>"Hello." ^? _last
</code></strong>'.'
</pre><p>See documentation for <code><a href="Lens-Micro.html#v:_head">_head</a></code>, as <code><a href="Lens-Micro.html#v:_last">_last</a></code> and <code><a href="Lens-Micro.html#v:_head">_head</a></code> are pretty similar.</p></div></div><div class="top"><p class="src"><a id="v:mapAccumLOf" class="def">mapAccumLOf</a> :: <a href="Lens-Micro.html#t:LensLike">LensLike</a> (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) <a href="src/Lens-Micro.html#mapAccumLOf" class="link">Source</a> <a href="#v:mapAccumLOf" class="selflink">#</a></p><div class="doc"><p>This generalizes <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#v:mapAccumL">mapAccumL</a></code> to an arbitrary <code><a href="Lens-Micro.html#t:Traversal">Traversal</a></code>. (Note that it doesn't work on folds, only traversals.)</p><pre><code>mapAccumL</code> ≡ <code><a href="Lens-Micro.html#v:mapAccumLOf">mapAccumLOf</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Traversable.html#v:traverse">traverse</a></code>
</pre></div></div><h1 id="g:7">Prism: a traversal iterating over at most 1 element</h1><div class="doc"><p>Prisms are traversals that always target 0 or 1 values. Moreover, it's possible to <em>reverse</em> a prism, using it to construct a structure instead of peeking into it. Here's an example from the lens library:</p><pre>>>> over _Left (+1) (Left 2)
Left 3
>>> _Left # 5
Left 5
</pre><p>However, it's not possible for microlens to export prisms, because their type depends on <code><a href="http://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html#t:Choice">Choice</a></code> from <a href="http://hackage.haskell.org/package/profunctors">profunctors</a>. So, all prisms included here are traversals instead (and you can't reverse them).</p></div><div class="top"><p class="src"><a id="v:_Left" class="def">_Left</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a' b) a a' <a href="src/Lens-Micro.html#_Left" class="link">Source</a> <a href="#v:_Left" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_Left">_Left</a></code> targets the value contained in an <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code>, provided it's a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code>.</p><p>Gathering all <code>Left</code>s in a structure (like the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:lefts">lefts</a></code> function, but not necessarily just for lists):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>[Left 1, Right 'c', Left 3] ^.. each._Just
</code></strong>[1,3]
</pre><p>Checking whether an <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code> is a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code> (like <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:isLeft">isLeft</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>has _Left (Left 1)
</code></strong>True
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>has _Left (Right 1)
</code></strong>False
</pre><p>Extracting a value (if you're sure it's a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code>):</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Left 1 ^?! _Left
</code></strong>1
</pre><p>Mapping over all <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code>s:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>(each._Left %~ map toUpper) [Left "foo", Right "bar"]
</code></strong>[Left "FOO",Right "bar"]
</pre><p>Implementation:</p><pre><code><a href="Lens-Micro.html#v:_Left">_Left</a></code> f (Left a) = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Left">Left</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f a
<code><a href="Lens-Micro.html#v:_Left">_Left</a></code> _ (Right b) = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> (<code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Right">Right</a></code> b)
</pre></div></div><div class="top"><p class="src"><a id="v:_Right" class="def">_Right</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a> a b') b b' <a href="src/Lens-Micro.html#_Right" class="link">Source</a> <a href="#v:_Right" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_Right">_Right</a></code> targets the value contained in an <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#t:Either">Either</a></code>, provided it's a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Either.html#v:Right">Right</a></code>.</p><p>See documentation for <code><a href="Lens-Micro.html#v:_Left">_Left</a></code>.</p></div></div><div class="top"><p class="src"><a id="v:_Just" class="def">_Just</a> :: <a href="Lens-Micro.html#t:Traversal">Traversal</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a') a a' <a href="src/Lens-Micro.html#_Just" class="link">Source</a> <a href="#v:_Just" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_Just">_Just</a></code> targets the value contained in a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code>, provided it's a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code>.</p><p>See documentation for <code><a href="Lens-Micro.html#v:_Left">_Left</a></code> (as these 2 are pretty similar). In particular, it can be used to write these:</p><ul><li>Unsafely extracting a value from a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code>:</li></ul><pre> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:fromJust">fromJust</a></code> = (<code><a href="Lens-Micro.html#v:-94--63--33-">^?!</a></code> <code><a href="Lens-Micro.html#v:_Just">_Just</a></code>)
</pre><ul><li>Checking whether a value is a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code>:</li></ul><pre> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:isJust">isJust</a></code> = <code><a href="Lens-Micro.html#v:has">has</a></code> <code><a href="Lens-Micro.html#v:_Just">_Just</a></code>
</pre><ul><li>Converting a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> to a list (empty or consisting of a single element):</li></ul><pre> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:maybeToList">maybeToList</a></code> = (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code> <code><a href="Lens-Micro.html#v:_Just">_Just</a></code>)
</pre><ul><li>Gathering all <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Just">Just</a></code>s in a list:</li></ul><pre> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:catMaybes">catMaybes</a></code> = (<code><a href="Lens-Micro.html#v:-94-..">^..</a></code> <code><a href="Lens-Micro.html#v:each">each</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:.">.</a></code> <code><a href="Lens-Micro.html#v:_Just">_Just</a></code>)
</pre></div></div><div class="top"><p class="src"><a id="v:_Nothing" class="def">_Nothing</a> :: <a href="Lens-Micro.html#t:Traversal-39-">Traversal'</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a> a) () <a href="src/Lens-Micro.html#_Nothing" class="link">Source</a> <a href="#v:_Nothing" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#v:_Nothing">_Nothing</a></code> targets a <code>()</code> if the <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#t:Maybe">Maybe</a></code> is a <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code>, and doesn't target anything otherwise:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Just 1 ^.. _Nothing
</code></strong>[]
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>Nothing ^.. _Nothing
</code></strong>[()]
</pre><p>It's not particularly useful (unless you want to use <code><code><a href="Lens-Micro.html#v:has">has</a></code> <code><a href="Lens-Micro.html#v:_Nothing">_Nothing</a></code></code> as a replacement for <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:isNothing">isNothing</a></code>), and provided mainly for consistency.</p><p>Implementation:</p><pre><code><a href="Lens-Micro.html#v:_Nothing">_Nothing</a></code> f Nothing = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Function.html#v:const">const</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Maybe.html#v:Nothing">Nothing</a></code> <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Data-Functor.html#v:-60--36--62-"><$></a></code> f ()
<code><a href="Lens-Micro.html#v:_Nothing">_Nothing</a></code> _ j = <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.9.0.0/Control-Applicative.html#v:pure">pure</a></code> j
</pre></div></div><h1 id="g:8">Other types</h1><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:LensLike" class="def">LensLike</a> f s t a b = (a -> f b) -> s -> f t <a href="src/Lens-Micro-Type.html#LensLike" class="link">Source</a> <a href="#t:LensLike" class="selflink">#</a></p><div class="doc"><p><code><a href="Lens-Micro.html#t:LensLike">LensLike</a></code> is a type that is often used to make combinators as general as possible. For instance, take (<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>), which only requires the passed lens to be able to work with the <code>(,) a</code> functor (lenses and traversals can do that). The fully expanded type is as follows:</p><pre>(<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>) :: ((a -> (a, b)) -> s -> (a, t)) -> (a -> b) -> s -> (a, t)
</pre><p>With <code><a href="Lens-Micro.html#t:LensLike">LensLike</a></code>, the intent to use the <code>(,) a</code> functor can be made a bit clearer:</p><pre>(<code><a href="Lens-Micro.html#v:-60--60--37--126-"><<%~</a></code>) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t)
</pre></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:LensLike-39-" class="def">LensLike'</a> f s a = <a href="Lens-Micro.html#t:LensLike">LensLike</a> f s s a a <a href="src/Lens-Micro-Type.html#LensLike%27" class="link">Source</a> <a href="#t:LensLike-39-" class="selflink">#</a></p><div class="doc"><p>A type alias for monomorphic <code><a href="Lens-Micro.html#t:LensLike">LensLike</a></code>s.</p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.17.2</p></div></body></html>
|