Questions tagged [monocle-scala]

Monocle is a Lens library, or more generally an Optics library where Optics gather the concepts of Lens, Traversal, Optional, Prism and Iso. Monocle is strongly inspired by Haskell Lens.

Monocle is a Lens library, or more generally an Optics library where Optics gather the concepts of Lens, Traversal, Optional, Prism and Iso. Monocle is strongly inspired by Haskell Lens.

What does it mean?

Optics are a set of purely functional abstractions to manipulate (get, set, modify) immutable objects. Optics compose between each other and particularly shine with nested objects.

Why do I need this?

Scala already provides getters and setters for case classes but modifying nested object is verbose which makes code difficult to understand and reason about.

Github

32 questions
2
votes
0 answers

Monocle. Scala. How to work with recursive data?

Сonsider a simple case: case class Node(id: String, name: String, children: Seq[Node]) How can i make changes to any depth using Monocle?? I want to add a new Node to the list children if it isn't exist and sometimes change a name. BUT ANY DEPTH.…
user10926849
2
votes
1 answer

Map `State` via `Lens`

Is there some function with signature like lensMapState[S, T, A](lens : Lens[S, T]): State[T, A] => State[S, A] With semantics run modification of chosen part and get result One implementation could be def lensMapState[S, T, A](lens: Lens[S, T]):…
Odomontois
  • 15,918
  • 2
  • 36
  • 71
2
votes
1 answer

How can I use Monocle's built in law implementations to test my own lenses?

I noticed that Monocle has implementations of lens laws that are used to test the libraries internals. They seem to be nicely generalized and modularized. I tried to use them to test my own lenses, but I am lost in the jungle of dependencies. Has…
1
vote
1 answer

Modify sequence at index using Monocle

I can modify Map using focus and index just fine. Is there a similar functionality in Monocle to update a sequence at given index? import monocle.syntax.all._ case class C(member: Int) val map = Map(0 -> C(0), 1 -> C(1)) case class…
Suma
  • 33,181
  • 16
  • 123
  • 191
1
vote
0 answers

Is it possible to model update with constrains with Lens (or other optics)?

I'm working with an Web UI which requires validation on user inputs. When implementing an double thumb slider, and comes problem when using Lens with a 'constraint' update. When use lens.set, it is required to validate the new value, and if it is…
1
vote
0 answers

Update a single item in a nested case class

The university example explains how to update all items of a map: (allLecturers composeLens salary).modify(_ + 2)(uni) To set a single item, one can use this cumbersome code: (departments composeLens at("History")).set(Some(Department(30,…
Tamriel
  • 1,337
  • 1
  • 9
  • 9
1
vote
2 answers

Correct syntax for updating nested map using Monocle

I've seen the official example of updating a Map but I'm having trouble with the syntax. val pod: Lens[Event, Pod] = GenLens[Event](_.`object`) val metadata: Lens[Pod, Metadata] = GenLens[Pod](_.metadata) val labels: Lens[Metadata, Map[String,…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
1
vote
1 answer

Defining optional values in a path built with composed optics

I have a structure of nested case classes that I create with default values: case class Alpha(text: String = "content") case class Beta(alpha: Alpha = Alpha()) case class Gamma(beta: Option[Beta] = None) I would like to create the whole thing with…
qtwo
  • 497
  • 4
  • 10
1
vote
2 answers

Scala: how to upsert field value in Monocle

Given the JsonExample in the monocle project, I would like to create a lens where a set call will either replace a value in a key/value pair, or create the key/value pair if it doesnt already exist. However this seems to represented with either an…
J Pullar
  • 1,915
  • 2
  • 18
  • 30
0
votes
0 answers

Access parent case class from child using Monocle lens

I want to use optics in Scala to not go down a nested structure but rather traverse the other way around like going to Parent from Child. case class Parent(param1: Int) class Child(param2: Int) val parent = Parent(param1) val child =…
0
votes
1 answer

Merge each entry of a list nested inside a tuple with the other part of the tuple

I have a list (let's call it L1) of tuples, in this tuple _1 is some class with data and _2 is a list (let's call it L2) of some other classes with other data. For each entry of L1 (that is a list of tuples) I need to merge each entry of L2 with the…
Leonid Bor
  • 2,064
  • 6
  • 27
  • 47
0
votes
0 answers

how to add monocle library to my scala project?

i have a scala project using the scala plugin. I need to use this monocle library, (https://www.optics.dev/Monocle/) for a project, but I just have no idea how to add it to my project. For sbt projects, there should be a build.sbt file, where I…
n00bster
  • 379
  • 4
  • 12
0
votes
1 answer

How to create Monocle lenses with sequences?

I am trying to create a Monocle lens with nested case classes and sequences. import monocle.macros.GenLens import monocle.function.Each.each case class A(bs: Seq[B]) case class B(c: Int) val bs = GenLens[A](_.bs) val c = GenLens[B](_.c) val cs =…
pgrandjean
  • 676
  • 1
  • 9
  • 19
0
votes
1 answer

How to print a Monocle Lens as a property accessor style string

Using Monocle I can define a Lens to read a case class member without issue, val md5Lens = GenLens[Message](_.md5) This can used to compare the value of md5 between two objects and fail with an error message that includes the field name when…
Janek Bogucki
  • 5,033
  • 3
  • 30
  • 40
0
votes
1 answer

copy method and subtype polymorphism in Scala

I am trying to solve the following Scala compiler error below. case class CC[E](l:List[E]) trait D[E,L<:CC[E]]{ def f(l:L):L = l.copy(l=List()) // does not compile: "found CC[E], required: L" } In (pseudo)-Haskell (without sub-typing) this would…
jhegedus
  • 20,244
  • 16
  • 99
  • 167