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
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
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
10
votes
2 answers

Filtering Lists in Scala's Monocle

Given the following code: case class Person(name :String) case class Group(group :List[Person]) val personLens = GenLens[Person] val groupLens = GenLens[Group] how can i "filter" out certain Persons from the selection, NOT by index but by a…
Lazarus535
  • 1,158
  • 1
  • 8
  • 23
9
votes
1 answer

Combine collection of lenses

Monocle is a great library (and not the only one) which implements lenses pattern, which is great if we have to change one field in huge nested object. Like in example http://julien-truffaut.github.io/Monocle/ case class Street(number: Int, name:…
pkozlov
  • 746
  • 5
  • 17
9
votes
3 answers

How to find and modify field in nested case classes?

Defined some nested case classes with List fields: @Lenses("_") case class Version(version: Int, content: String) @Lenses("_") case class Doc(path: String, versions: List[Version]) @Lenses("_") case class Project(name: String, docs:…
Freewind
  • 193,756
  • 157
  • 432
  • 708
8
votes
1 answer

remove elements from a List of case class structure efficiently and elegantly

I have a nested case classes structure in a List for simplicity will use following as an example - case class Address(street: String, city: String, state: String, zipCode: Int) case class Person(firstName: String, lastName: String, addresses:…
Vikas Pandya
  • 1,998
  • 1
  • 15
  • 32
6
votes
0 answers

Is there a way to use circe-optics' JsonPath with strings, just as in jq CLI tool?

What I'd like to do, is having field descriptor defined as field1.field2[1].field3, access value two of json: { "field1": { "field2": [ { "field3": "one" }, { "field3": "two" } ] } } I know I can…
Bartek Andrzejczak
  • 1,292
  • 2
  • 14
  • 27
5
votes
1 answer

Modifying Map via Monocle

I wanted to try lenses and the Monocle library seemed (from my noobish perspective) good with all those fancy boilerplate-less @Lenses. Unfortunately I found out there are little to non learning materials for beginners (I know basics of FP in…
monnef
  • 3,903
  • 5
  • 30
  • 50
4
votes
1 answer

Add and delete List items

The university example explains how to add and delete items of a map: (departments composeLens at("Physics")).set(Some(physics))(uni) (departments composeLens at("History")).set(None)(uni) This does not work with Lists, though: (lecturers…
Tamriel
  • 1,337
  • 1
  • 9
  • 9
4
votes
1 answer

Are Monocle's Optionals the same as partial Lenses?

Monocle's optionals have the following access functions (for Optional[C,A]): getOption: C => Option[A] set: A => C => C This is at odds with the original definition of (partial) asymmetric data lenses. I would expect: getOption: C =>…
4
votes
2 answers

How to update Map using monocle

I'd like to try Monocle library. But i could not find help resources for base syntax. In short i need optics Map[K,V] -> A having optics V -> A, how could i define this? Suppose i have some import monocle.macros.GenLens case class DirState(opened:…
Odomontois
  • 15,918
  • 2
  • 36
  • 71
3
votes
1 answer

Functional Programming/Optic concept that takes a partial object and returns a "filled in" object using lenses and traversals?

(Edit I'm using monocle-ts, but if it's not possible with monocle-ts (since the author even says it's just a partial port of the original Monocle for Scala) but if there is something in another optics package for any language, I'm open to porting…
user1713450
  • 1,307
  • 7
  • 18
3
votes
1 answer

Update deeply nested case class with Options

I have a 3-level nested case class model with a bunch of options that represents some data in a database. It's essentially: case class User(settings: Option[Settings]) case class Settings(keys: Option[List[KeySet]]) case class KeySet(privateKey:…
coolboyjules
  • 2,300
  • 4
  • 22
  • 42
3
votes
1 answer

cost of equality when using monocle/scalaz lenses

I was reading about Diode, and it made me think about lenses in Monocle / Scalaz: If I (conditionally) modify deeply some part of a deeply nested data-structure using Monocle/Scalaz lens and want to compare if there was a change, is there a need to…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
3
votes
1 answer

How to use monocle to modify a nested map and another field in scala

I'm giving a try to monocle for the first time. Here is the case class : case class State(mem: Map[String, Int], pointer: Int) And the current modification, using standard scala, that I would like to do : def add1 = (s: State) => s.copy( mem =…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
1
2 3