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
0
votes
1 answer

What is the correct way to compose Ramda lenses?

I'm having trouble wrapping my head around this pipeline. Given an array of students, I want to map over them, adding a new property (icon) based on the value of an existing property (grade). I've broken the problem down into smaller parts by…
Sean Lindo
  • 1,387
  • 16
  • 33
0
votes
1 answer

How to eliminate the boilerplate of wrapping and unwrapping using lenses

tl;dr: is it possible to use any of the lens family of abstractions to wrap/unwrap any arbitrary newtype (that provides an instance for such abstractions)? I'll motivate my question by a simple example, based on a true story. Suppose I define the…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
0
votes
1 answer

Haskell: use or uses in Getter

In Control.Lens we have Getter that can access the nested structure. Getter has use and uses, but it's not clear to me how they work. So it'd be great if someone can provide some simple examples that use or uses is utilised. Why do I need to know…
4xx
  • 165
  • 8
0
votes
1 answer

Using a Lens on a non-case class extending something with a constructor in Scala

I am probably thinking about this the wrong way, but I am having trouble in Scala to use lenses on classes extending something with a constructor. class A(c: Config) extends B(c) { val x: String = doSomeProcessing(c, y) // y comes from B } I am…
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
0
votes
0 answers

How to build up modifications and apply them at once using lenses in Scala?

I have designed a case class that looks superficially like this: case class Moo(foos: Seq[Foo], bar: Bar) { require(foos.length == bar.baz.length) ... } This type will be consumed by a Java program, so I have created a convenient MooBuilder to…
Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
0
votes
1 answer

How to reduce boilerplate with monocle in scala

I've refactored my code for day 12 of advent of code by using monocle, a lens library in scala. Is it possible to improve this code : type Register = String type Mem = Map[String, Int] @Lenses case class State(mem: Mem, pointer: Int) def…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
0
votes
2 answers

Composable Lensing

I have the following Haskell types import Control.Lens import Data.Map.Lens import qualified Data.Map as M type RepoStats = M.Map String (M.Map String RepoStat) data RepoStat = RepoStat { _full_name :: String, _bug_count :: Maybe Int, …
Steve
  • 1,596
  • 1
  • 10
  • 21
0
votes
1 answer

How can I lift an fclabels Lens to a Monad?

I'm trying to update some old code using fclabels, from v0.4 to v2.0 (lts-3.17), that lifts a label/lens to a monad. The old code is: {-# LANGUAGE TypeOperators #-} import Data.Record.Label import Control.Monad (liftM, liftM2) liftMLabel :: Monad m…
S i
  • 3
  • 3
0
votes
1 answer

Haskell: Changing an element at a given index in a ByteString

I'm trying to change a character at indices x and y in a matrix made of ByteStrings to a different character. Initially, I used [[Char]] to represent the matrix, so I was able to use .~ from Control.Lens.Setter to change the value, but this doesn't…
the spectre
  • 350
  • 4
  • 11
0
votes
2 answers

Array indexing lens out of array and index lenses

This is a simpler version of Using lens for array indexing if both array and index are in State as I have resolved some issues. I'm not sure if I should delete the original or edit it in place. Given {-# Language TemplateHaskell #-} {-# Language…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
0
votes
2 answers

How to combine the results of two simple lenses into one

Suppose I have a data type defined as below: data Register = Register { _reg_h :: Word8 , _reg_l :: Word8 } makeLenses ''Register Now if I want to define a lens that focus from a Register to a…
shouya
  • 2,863
  • 1
  • 24
  • 45
0
votes
1 answer

scala shapeless lenses not working

In my project I encountered a situation where I need to perform nested update on immutable object which is an instance of case class. Firstly I just wanted to use copy function provided by the case classes but then I stumbled upon lenses. I looked…
Andna
  • 6,539
  • 13
  • 71
  • 120
-1
votes
1 answer

Traversing and adding elements to a Data.Tree using Lenses in Haskell

I'm starting to use lenses and until now I've been unable to use them in a concrete part of a codebase I'm writing. My objective is to update a rose tree structure such as the one in Data.Tree by adding a new node inside one of the existing ones. To…
Jesuspc
  • 1,664
  • 10
  • 25
-1
votes
1 answer

Lenses support in Play framework

Doesn't the Play framework have any lenses support included by default? Something like Monocole or those with Scalaz. Maybe there are recommended alternative approaches (along with their rationale) that I'm not aware of. Scenario: I have an…
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
1 2 3
17
18