Skip to content
tphyahoo edited this page Aug 6, 2015 · 152 revisions

Operators

Combinator(s) w/ Result Stateful w/ Result Notes
Control.Lens (Getting)
view / ^. use View single target, or fold multiple monoidal targets.

view (to snd) ("hello","world") == "world" -- convert prelude.snd function into lens

view _2 ("hello","world") == "world" -- same thing

view _2 ("hello","world","yeeha") == "world" -- but also works on triples!

("hello","world") ^. _2 == "world"

[Sum 3, Sum 4] ^. each == Sum 7

use works just like view except in a MonadState instead of a MonadReader.
views uses View/fold after applying a function.

views _2 (map toUpper) ("hello","world") == "WORLD"

views each (Sum . succ . getSum) [Sum 1, Sum 2] == Sum 5

Control.Lens (Setting)
set / .~ <.~ assign / .= <.= Replace target(s) with value.

set _2 "pluto" ("hello","world") == ("hello","pluto")

set each 0 [1,2] == [0,0]

set (setting Control.Arrow.second) 3 (1,2) == (1,3)

( set (upon snd) 3 $ (1,2) ) == (1,3) -- warning, here be dragons, see docu for upon

  <a href="http://ekmett.github.com/lens/Control-Lens-Lens.html#v:-60--60-.-126-">
    <code> &lt;&lt;.~</code> </a> and
  <a href="http://ekmett.github.com/lens/Control-Lens-Lens.html#v:-60--60-.-61-">
    <code> &lt;&lt;.= </code></a>
  return the old value.
over / %~ <%~ %= <%= Replace target(s) by applying function.

over _2 (map toUpper) ("hello","world")

over each (map toUpper) ["hello","world"]

<<%~ and <<%= return the old value
id / %%~ / traverseOf %%= Update target(s) with an Applicative or auxiliary result

( (%%~) each $ \x -> if even x then Just . succ $ x else Nothing ) [2,4,6]

( (%%~) _2 $ \x -> if even x then Just . succ $ x else Nothing ) (2,4,6)

traverseOf can replace (%%~) above but is less general. traverseOf applies to Over type, whereas (%%~) applies to Optical type. Over p is equivalent to Optical p (->)

Control.Lens.Fold / Control.Lens.Prism
review Choose a branch of a sum type

( review _Right $ 1 ) == Right 1

preview / ^? / firstOf Attempt to extract branch of a sum type.

( preview _Right . review _Right $ 1 ) == Just 1

works with view if a monoid...

( view _Right . Right $ [1,2,3] ) == [1,2,3]

( view _Right . Left $ [1,2,3] ) == ()

previews preview after applying a function

( previews _Right (+1) . Right $ 1 ) == Just 2

toListOf / ^.. Return a list of the target(s)

toListOf traverse ('a','b') == "b"

toListOf each ('a','b') == "ab"

Control.Lens.Action (has everything here been removed from lens package??)
perform, performs ^! Perform monadic action(s)
Control.Lens (Getting Indexed)
iview / ^@. iuse View the index and value of an IndexedGetter or IndexedLens.
let mapHW = Data.Map.fromList [(1,"hello"),(2,"world")]
mapHW ^@. iat 2 == (2,Just "world")
iviews iuses View a function of the index and value of an IndexedGetter into the current environment.
Control.Lens (Setting Indexed)
iset / .@~ .@= Set target(s) with access to the index.
iover / %@~ <%@~ %@= <%@= Update target(s) with access to the index.
withIndex, itraverseOf, %%@~ %%@= Update target(s) with an Applicative or auxiliary result with access to the index.
Control.Lens (math)
+~ <+~ += <+= Add to target(s)
-~ <-~ -= <-= Subtract from target(s)
*~ <*~ *= <*= Multiply target(s)
//~ <//~ //= <//= Divide target(s)
^~ <^~ ^= <^= Raise target(s) to a non-negative Integral power
^^~ <^^~ ^^= <^^= Raise target(s) to an Integral power
**~ <**~ **= <**= Raise target(s) to an arbitrary power
Control.Lens (logic)
||~ <||~ ||= <||= Logically or target(s)
&&~ <&&~ &&= <&&= Logically and target(s)
<>~ <<>~ <>= <<>= mappend to the target monoidal value(s)
Data.Bits.Lens
.|.~ <.|.~ .|.= <.|.= Bitwise or target(s)
.&.~ <.&.~ .&.= <.&.= Bitwise and target(s)
System.FilePath.Lens
</>~ <</>~ </>= <</>= Append a relative path to a FilePath
<.>~ <<.>~ <.>= <<.>= Append a file extension to a FilePath
Clone this wiki locally