lenses

Inserting into a list at a specific location using lenses

梦想与她 提交于 2021-02-07 06:42:57
问题 I'm trying to perform a manipulation of a nested data structure containing lists of elements. After mucking around with various approaches I finally settled on lenses as the best way to go about doing this. They work perfectly for finding and modifying specific elements of the structure, but so far I'm stumped on how to add new elements. From what I've read, I can't technically use a Traversal as it violates the Traversal laws to insert a new element into a list, and that's assuming I could

Using Lenses on Scala Regular Classes

。_饼干妹妹 提交于 2020-01-21 12:39:15
问题 Most popular JSON libraries for Scala have the ability to serialize and deserialize to case classes. Unfortunately, until Scala 2.11 is released, there is a restriction on the number of parameters a case class can have (22 maximum). As a workaround to go over this limit, it is possible to use regular classes instead. (for example: How can I deserialize from JSON with Scala using *non-case* classes?). However, this loses the benefits of case classes. For example, there is no automatically

Using Lenses on Scala Regular Classes

拜拜、爱过 提交于 2020-01-21 12:38:21
问题 Most popular JSON libraries for Scala have the ability to serialize and deserialize to case classes. Unfortunately, until Scala 2.11 is released, there is a restriction on the number of parameters a case class can have (22 maximum). As a workaround to go over this limit, it is possible to use regular classes instead. (for example: How can I deserialize from JSON with Scala using *non-case* classes?). However, this loses the benefits of case classes. For example, there is no automatically

makeLenses for GADTs (Haskell)

巧了我就是萌 提交于 2020-01-11 08:29:09
问题 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? 回答1: 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

Jquery Image Lens - Kinetic JS image id

佐手、 提交于 2020-01-03 01:46:30
问题 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

indexing list with Control.Lens requires Monoid constraint

若如初见. 提交于 2020-01-01 09:19:36
问题 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

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

Deriving nested shapeless lenses using only a type

我们两清 提交于 2019-12-31 02:45:35
问题 I'm trying to come up with something similar to Classy Lenses to use with cats-mtl. For this, I want to be able to construct a Lens based on provided types only. I found no way to do it using operations provided in shapeless, so I'm writing a new one. import shapeless._ class Classy[O[_, _], S, A](val get: O[S, A]) object Classy { def apply[O[_, _], S, A](implicit ev: Classy[O, S, A]): Classy[O, S, A] = ev implicit def rootLens[S]: Classy[Lens, S, S] = new Classy(OpticDefns.id[S]) implicit