Questions tagged [lenses]

In functional programming, a lens is a composable field accessor. Lenses allow nested data structures to be manipulated in a concise and side-effect-free way.

In functional programming, a lens is a composable field accessor. Lenses allow nested data structures to be manipulated in a concise and side-effect-free way.

Lenses define a set function and a get function. Given a value a of type A and a value b of type B, set b a returns a new value a' of type A with some field inside set to b. Given a value a of type A, get a returns the value b of type B contained in a.

To constitute a lens, get and set must follow a few straightforward laws:

  1. Given a and b, get (set b a) = b.

  2. Given a, b, and b', get (set b' (set b a)) = b'.

  3. Given a and b, set (get a) a = a.

Because lenses follow these laws, they are safe to compose. This makes them suitable for manipulating nested data structures in a concise way.

269 questions
178
votes
1 answer

lenses, fclabels, data-accessor - which library for structure access and mutation is better

There are at least three popular libraries for accessing and manipulating fields of records. The ones I know of are: data-accessor, fclabels and lenses. Personally I started with data-accessor and I'm using them now. However recently on…
Tener
  • 5,280
  • 4
  • 25
  • 44
172
votes
1 answer

Sneaking lenses and CPS past the value restriction

I'm encoding a form of van Laarhoven lenses in OCaml, but am having difficulty due to the value restriction. The relevant code is as follows: module Optic : sig type (-'s, +'t, +'a, -'b) t val lens : ('s -> 'a) -> ('s -> 'b -> 't) -> ('s, 't,…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
83
votes
2 answers

Functional lenses

Could someone explain functional lenses to me? It's a surprisingly difficult subject to google for and I haven't made any progress. All I know is that they provide similar get/set functionality than in OO.
Masse
  • 4,334
  • 3
  • 30
  • 41
38
votes
3 answers

What are lenses used/useful for?

I can't seem to find any explanation of what lenses are used for in practical examples. This short paragraph from the Hackage page is the closest I've found: This modules provides a convienient way to access and update the elements of a structure.…
Pubby
  • 51,882
  • 13
  • 139
  • 180
26
votes
2 answers

What are the advantages and disadvantages of using lenses?

Lenses don't seem to have any disadvantages while having significant advantages over standard Haskell: Is there any reason I shouldn't use lenses wherever possible? Are there performance considerations? Additionally, does template Haskell have any…
joshj
  • 473
  • 5
  • 11
22
votes
2 answers

Isn't it redundant for Control.Lens.Setter to wrap types in functors?

I'm watching the Control.Lens introduction video. It makes me wonder why is it needed for the Setter type to wrap things in functors. It's (roughly) defined like this: type Control.Lens.Setter s t a b = (Functor f) => (a -> f a) -> s -> f t Let's…
qwe
  • 766
  • 1
  • 5
  • 15
20
votes
2 answers

Ramda js: lens for deeply nested objects with nested arrays of objects

Using Ramda.js (and lenses), I want to modify the JavaScript object below to change "NAME:VERSION1" to "NAME:VERSION2" for the object that has ID= "/1/B/i". I want to use a lens because I want to just change one deeply nested value, but otherwise…
Greg Edwards
  • 598
  • 1
  • 5
  • 12
20
votes
1 answer

Lens / Prism with error handling

Let's say I have a pair of conversion functions string2int :: String -> Maybe Int int2string :: Int -> String I could represent these fairly easily using Optics. stringIntPrism :: Prism String Int However if I want to represent failure reason, I'd…
James Davies
  • 9,602
  • 5
  • 38
  • 42
20
votes
1 answer

Type variable would escape its scope

I'm trying to refactor my function by giving it a lens argument (from the xml-lens package). I'm missing something about type quantifiers. What is going on here? *Main> let z name = listToMaybe $ pom ^.. root ./ ell name . text *Main> :t z z :: Text…
sevo
  • 4,559
  • 1
  • 15
  • 31
20
votes
2 answers

Getting multiple results from map with "lens"

Having these imports: > import Control.Lens Control.Lens> import qualified Data.Map as Map and a map value defined as follows: Control.Lens Map> let m = Map.fromList [('a', 1), ('c', 3), ('b', 2)] I can get it's elements one by one like…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
18
votes
2 answers

Could someone explain the diagram about the `lens` library?

If you browse through Lens entry on hackage, Lens Github's repo, or even google about Lens, you will find a lot of partial references such as introductory tutorials/videos, examples, overviews and so on. Since I already know most of the basics, I am…
kqr
  • 14,791
  • 3
  • 41
  • 72
17
votes
2 answers

Combining lenses

Using a lens library I can apply a modification function to individual targets, like so: Prelude Control.Lens> (1, 'a', 2) & _1 %~ (*3) (3,'a',2) Prelude Control.Lens> (1, 'a', 2) & _3 %~ (*3) (1,'a',6) How can I combine those individual lenses (_1…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
17
votes
2 answers

traversal tree with Lens and Zippers

I'm learning the Lens package. I must say it's a rather challenging task. Can someone show me how to traverse a Tree with Zipper from Lens? In particular, how can I write a function that takes a list of roots and allows me to access the branches of…
Kai
  • 263
  • 1
  • 6
16
votes
1 answer

Is there a van Laarhoven representation of `Optional`

Many types of optics have a van Laarhoven representation. For example, a Lens of type Lens s t a b can be represented as: Functor f => (a -> f b) -> s -> f t Similarly a Traversal, can be represented in a similar way, swapping the Functor…
Joe
  • 1,479
  • 13
  • 22
15
votes
3 answers

How do I handle the Maybe result of at in Control.Lens.Indexed without a Monoid instance

I recently discovered the lens package on Hackage and have been trying to make use of it now in a small test project that might turn into a MUD/MUSH server one very distant day if I keep working on it. Here is a minimized version of my code…
1
2 3
17 18