monocle-scala

Lens / Prism with error handling

狂风中的少年 提交于 2020-01-01 01:18:07
问题 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 need to keep these as two separate functions. string2int :: String -> Validation [ParseError] Int int2string :: Int -> String` For this simple example Maybe is perfectly fine, since we can always assume that a failure is a parse failure, thus we don

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

你说的曾经没有我的故事 提交于 2019-12-22 10:38:37
问题 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 = s.mem.updated("a", s.mem("a") + 1), pointer = s.pointer + 1 ) And here is my implementation with monocle val mem = GenLens[State](_.mem) val pointer = GenLens[State](_.pointer) val add2 = (mem composeLens at("a")).modify(_.map(_ + 1)) andThen pointer

Correct syntax for updating nested map using Monocle

独自空忆成欢 提交于 2019-12-11 07:14:23
问题 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, String]] = GenLens[Metadata](_.labels) I want to update a key "app" in the labels Map . But I can't get the following to compile: (labels.composeOptional(index("app"))).set("whatever")(someLabels) In fact, this answer by one of the authors of Monacle

Filtering Lists in Scala's Monocle

拟墨画扇 提交于 2019-12-10 02:34:43
问题 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 specific property of Person , like: val trav :Traversal[Group, Person] = (groupLens(_.group) composeTraversal filterWith((x :Person) => /*expression of type Boolean here */)) I only found the filterIndex function, which does only include elements from the

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

坚强是说给别人听的谎言 提交于 2019-12-05 18:55:04
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 = s.mem.updated("a", s.mem("a") + 1), pointer = s.pointer + 1 ) And here is my implementation with monocle val mem = GenLens[State](_.mem) val pointer = GenLens[State](_.pointer) val add2 = (mem composeLens at("a")).modify(_.map(_ + 1)) andThen pointer.modify(_ + 1) Unfortunately, the code is not cleaner… Is there a more concise way ? Can we generate all

Filtering Lists in Scala's Monocle

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:58:07
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 specific property of Person , like: val trav :Traversal[Group, Person] = (groupLens(_.group) composeTraversal filterWith((x :Person) => /*expression of type Boolean here */)) I only found the filterIndex function, which does only include elements from the list based on index, but this is not what i want. filterIndex takes a function of type: (Int => Boolean

Lens / Prism with error handling

大城市里の小女人 提交于 2019-12-03 11:00:19
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 need to keep these as two separate functions. string2int :: String -> Validation [ParseError] Int int2string :: Int -> String` For this simple example Maybe is perfectly fine, since we can always assume that a failure is a parse failure, thus we don't actually have to encode this using an Either or Validation type. However imagine, in addition to my