Lens infix operators
An overview of what the different parts of lens-4.0 infix operators stand for.
I'll be calling anything that is used to look at data a "lens", e.g.
Traversal. When a more specific term is required, I'll be using the
correct data type (which in particular starts witha capital letter).
(Once this list is properly polished, it might be worth a pull request.)
@: Provide access to the index of the data field looked at.
("hello","world","!") ^@. _2 is
(1,"world"), since "world" is
the entry at the second (
_2) entry of the zero-indexed tuple,
hence the retrieved index
#: Takes an
ALensparameter instead of whatever else the normal operator does. (
#alone is a synonym for
review, which builds a data structure out of a lens.)
^: General "view data" operator.
.: Single value.
(1, "hello") ^. _2 views the second element,
..: Multiple values as a list.
"Hello, World!" ^.. folded . filtered isLower keeps only lower
case letters, result:
?: Return the first value of a lookup, or
Nothingif there is none.
[1..10] ^? folded picks the first element of the list, resulting
 ^? folded on the other hand is
?!: Unsafe version of
?: crashes when there is no value.
[1..10] ^?! folded is
 ^?! folded is a runtime
!: Perform a monadic action with the data.
["Hello","World!"] ^! folded . act putStrLn prints the list, one
element at a time.
!for folds, so that multiple actions can be performed in one go.
[getLine, getLine] ^!! folded . acts reads two lines, and returns
the results together in a list.
?: perform all actions, but return only the first result (safely).
[getLine, getLine] ^!? folded . acts reads two lines, and returns
Just <first line>.
~: General "set value" operator.
=: Same as
~, but set an implicit
.: Specify the new value directly.
(_2 .~ "Mrs. Robinson") ("Hello", "World") [greets Mrs. Robinson.]
Justthe specified value, i.e.
.with an added
Just. This mostly seems to be there for symmetry with the getter-
(_2 ?~ "kidding") ("Hello", "World") greets someone and takes it
back immediately. Safe in Haskell, not so safe otherwise.
%: Modify, chosen as a pun for other languages use
%as the modulo operator.
(traversed %@~ replicate) "Hello" replaces each character with an
index-time replication of itself, yielding
<: Also return the new value of the modified field. Useful to check on what was actually modified deep inside a structure.
(traversed <%~ Sum . length) (words "let it be") maps every list
entry to its length, and also the monoidal summary of all
modifications. Stripping the
Sum constructors, the result is
<, but returns the old value instead of the new one.
(traversed <<%~ length) (words "let it be") collects the
unmodified values before applying the function, yielding
The following symbols are all used as shortcuts to do something specific with a
<op>~ x generally applies
(<op> x) to the fields pointed at, so for
&&~ x applies
(&& x), and
- Standard operations
- Standard arithmetic:
/was not taken because
- Specialized operations (located in *.Lens submodules):
- File paths:
<.>- Bitwise arithmetic:
%) a field inside a structure, and also returns the new value of that field (
<). When applied to a
Traversal, it modifies all targeted values, and also returns a "monoidal summary" (e.g. a list) of all the modified values.
<<>~is first and foremost in the library because in a language that has robot monkey operators
(:)and Kleisli fish
>=>, a rat operator must not be missing.
<>) a value to a field and returns the new value along with the modified structure (the first
&: Like ordinary
$, but flipped. Useful for chaining lens operations, as they typically read left-to-right.
(["hello", "world"], 42) & _1.mapped <>~ "!" & _2 +~ 10 first
appends "!" to all list elements, and then adds 10 to the second
tuple component, resulting in
(["hello!", "world!"], 52).
|>: Prepend (cons) and append (snoc). Be careful to specify the arguments in the right order or you'll be greeted with a giant error message.
0 <| [1,2,3] evaluates to
[1,2,3] |> 4 to
??: Most useful when applied to the function Functor, which allows seeing
??as a placeholder for the first of a two-argument function.
f ?? x is equivalent to
\y -> f y x.
<.: Composition of indexed functions.
<~: Execute a
MonadStateaction, and store the result in the state fields pointed at by the lens. Named to resemble with the
<-symbol, which assigns the result of a monadic action to an ordinary name.
_2 <~ fmap (uncurry (++)) get } will execute the action on the
right hand side; the result is the concatenation of the two tuple
elements of the state. This is then set as the second field of the