lenses

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

How can I use Control.Lens to update the ith element of a list?

亡梦爱人 提交于 2019-12-05 01:25:12
I have some datatypes along the line of data Outer = Outer { _list :: [ Inner ] } data Inner = Inner { _bool :: Bool } using Control.Lens, I can access the _bool of the ith Inner (inside a 'State Outer' monad) like this boolValue <- gets (^. list . to (!! i) . inner) I would like to also be able to update this value with something like list ^. (to (!! i)) ^. inner %= True However (by my understanding), the 'to' function only creates a getter, not a true lens that can be used as either getter or setter. So, how can I convert (!! i) into a lens that will allow me to update this field? You can't*

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

倖福魔咒の 提交于 2019-12-04 23:18:33
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 illustrating the problem I am facing right now with the at lenses used to access Key/Value containers (Data.Map.Strict in my case) {-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving, TemplateHaskell #-} module World where import Control.Applicative ((<$>),(<*>), pure) import Control.Lens import Data.Map.Strict (Map) import qualified Data.Map.Strict

Scalaz: how to compose a map lens with a value lens?

本小妞迷上赌 提交于 2019-12-04 06:21:17
There's an example of a Scalaz map lens here : Dan Burton calls it containsKey , and it's inspired by the Edward Kmett talk. There is also something called mapVPLens in Scalaz 7 which is useful for modifying values in a map. My question is: if I have a lens for modifying type V , and a lens for a Map[K,V] , how can I compose them? I've been searching for a while for a good simple example, but there's still a dearth of examples in Scalaz. I'm interested in both Scalaz 6 and Scalaz 7 solutions. If the lens you're trying to compose with the map lens is a partial lens, you can just use compose :

indexing list with Control.Lens requires Monoid constraint

允我心安 提交于 2019-12-04 04:25:38
The following code doesn't compile: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data MyType = MyType Int data Outer = Outer { _inners :: [ Inner ] } data Inner = Inner { _val :: MyType } $(makeLenses ''Outer) $(makeLenses ''Inner) i1 = Inner (MyType 1) i2 = Inner (MyType 2) o = Outer [i1, i2] x = o ^. inners . ix 0 . val giving this error Toy.hs:17:23: No instance for (Data.Monoid.Monoid MyType) arising from a use of `ix' Possible fix: add an instance declaration for (Data.Monoid.Monoid MyType) In the first argument of `(.)', namely `ix 0' In the second argument of `(.)', namely `ix 0

What's the difference between a lens and a partial lens?

我是研究僧i 提交于 2019-12-03 12:04:32
问题 A "lens" and a "partial lens" seem rather similar in name and in concept. How do they differ? In what circumstances do I need to use one or the other? Tagging Scala and Haskell, but I'd welcome explanations related to any functional language that has a lens library. 回答1: To describe partial lenses—which I will henceforth call, according to the Haskell lens nomenclature, prisms (excepting that they're not! See the comment by Ørjan)—I'd like to begin by taking a different look at lenses

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

What are the advantages and disadvantages of using lenses?

孤者浪人 提交于 2019-12-03 04:30:35
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 significant overhead? Lenses form an alternative to using direct closures over data constructors. Lenses therefore have approximately the same caveats as using functions and data constructors directly. Some of the cons because of this: Every time you modify a lens, you might potentially cause a lot of objects to be (re)created. For example, if you

makeLenses for GADTs (Haskell)

只愿长相守 提交于 2019-12-01 16:41:44
Is there an equivalent of makeLenses for GADTs? If I have a simple GADT like: data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b Is there a way to generate lenses automatically by passing in a constructor and a list of field names? I don't think it can be done automatically, but writing some lenses by hand isn't that hard in this particular case: {-# LANGUAGE GADTs #-} import Control.Lens data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b field1 :: Lens' (D a b) a field1 f (D x y) = fmap (\x' -> D x' y) (f x) field2 :: Lens' (D a b) b field2 f (D x y) = fmap (\y' -> D x y') (f y)