lenses

Combining Getters into a Fold

半城伤御伤魂 提交于 2019-12-11 09:20:20
问题 In the spirit of the following questions: Getting multiple results from map with “lens” Combining lenses I am now looking for a way to combine multiple Getters into a single Fold, so that something like the following: ('a','b','c','d') ^.. (_1 <> _2 <> _3) would result in this: ['a', 'b', 'c'] But the code above actually fails with the following message: No instance for (Monoid (Accessor (Endo [Char]) (Char, Char, Char, Char))) arising from a use of `<>' So how do I achieve this? Is this

Add or modify values in a shapeless HMap

徘徊边缘 提交于 2019-12-11 01:56:05
问题 Does anyone know how I might add or modify values in a shapeless HMap? The only functions I see on the HMap definition are: get + (which looks like it's creating a new map and adding the (k,v) tuple) - (same as above) My suspicion is that I will need to use lens? 回答1: shapeless.HMap is immutable. It's a wrapper (with type-level enhancements) of scala.collection.immutable.Map . + adds or modifies a pair (returning new HMap). Lenses also create a copy. Immutability/persistence is typical for FP

How to move from external function to functional lens

别说谁变了你拦得住时间么 提交于 2019-12-11 01:45:20
问题 On my journey to start getting better with functional programming, I discovered, with the help of a member of the SO family, what lens. I even made some research on it with the links down below to understand more about them. https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/basic-lensing http://fluffynukeit.com/how-functional-programming-lenses-work/ https://medium.com/@dtipson/functional-lenses-d1aba9e52254#.27yw4gnwk With all that knowledge, I thought I could

Haskell: use or uses in Getter

妖精的绣舞 提交于 2019-12-10 11:55:47
问题 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 it? because I'm reading some implementation in Haskell and "uses" and "use" were used in them. In particualr it says: inRange <- uses fsCurrentCoinRangeUpperBound (coinIndex <=) If the above code is just for comparing (<=) two values, then why do we

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

What are the advantages and disadvantages of using lenses?

不羁的心 提交于 2019-12-09 05:02:29
问题 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? 回答1: 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

Jquery Image Lens - Kinetic JS image id

人走茶凉 提交于 2019-12-08 16:44:33
I am trying to use jquery image lens on Kinetic JS canvas http://jsfiddle.net/user373721/7f8qM/15/ . The challenge I have is how to find the id of the image in the canvas, I tried: myImage.onload = function () { var yoda = new Kinetic.Image({ x: 0, y: 0, image: myImage, width: 400, height: 400, id: 'thumb' }); layer.add(yoda); layer.draw(); }; myImage.src = 'http://www.html5canvastutorials.com/demos/assets/yoda.jpg'; $('#thumb').imageLens({ lensSize: 200 }); Had no luck, I would appreciate your suggestions, thanks in advance. It is not posible to use kineticjs and imageLens as you are trying.

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

廉价感情. 提交于 2019-12-06 18:42:16
问题 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

How to get classy lenses with overloaded field names?

扶醉桌前 提交于 2019-12-06 02:14:13
I'm trying to build lenses for records which have the same field names. Along with that, I'm trying to "wrap/extend" these base records and want the same field names to work for the wrapped/extended records (which, I believe, classy lenses do). How do I get the following to work: -- Data types for context of the code snippet below data Download = Download { userId :: UserId ,gid :: Gid ,logId :: LogId ,parentId :: Maybe DownloadId ,createdAt :: UTCTime ,updatedAt :: UTCTime } data File = File { downloadId :: DownloadId ,fpath :: String ,len :: Int ,createdAt :: UTCTime ,updatedAt :: UTCTime }

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

核能气质少年 提交于 2019-12-06 01:39:44
问题 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.