lens

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

Combo lenses and prisms for sums of products?

北城以北 提交于 2019-12-04 17:00:43
If I have a record type, I can do pretty much anything I want to it with lenses. If I have a sum type, I can do pretty much whatever I want to it with prisms. But if I have a sum that includes a record, makeFields doesn't give me lenses into the fields (of course), but only traversals for them. declarePrisms seems a bit more promising. According to the documentation, declarePrisms [d| data Exp = Lit Int | Var String | Lambda{ bound::String, body::Exp } |] will create data Exp = Lit Int | Var String | Lambda { bound::String, body::Exp } _Lit :: Prism' Exp Int _Var :: Prism' Exp String _Lambda :

Haskell use first level lenses to create complex lens

删除回忆录丶 提交于 2019-12-04 11:43:23
Let's say, I have an object with two fields: data Example = Example { _position :: Int , _storage :: [Int]} how do I construct a lens that focuses on position element inside storage ? Also, will it be possible to restrict position values being modified via lenses to a range based on storage size? It seems like alongside could be used somehow, since Example is isomorphic to a tuple, but I can't comprehend the way to do that. I'm not sure how to phrase the question, so I was unable to find much relevant info. Edit: I misunderstood the problem, original answer follows at the end. I don't know any

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

When manipulating immutable datastructures, what's the difference between Clojure's assoc-in and Haskell's lenses?

穿精又带淫゛_ 提交于 2019-12-04 00:20:47
I need to manipulate and modify deeply nested immutable collections (maps and lists), and I'd like to better understand the different approaches. These two libraries solve more or less the same problem, right? How are they different, what types of problem is one approach more suitable for over the other? Clojure's assoc-in Haskell's lens Clojure's assoc-in lets you specify a path through a nested data struture using integers and keywords and introduce a new value at that path. It has partners dissoc-in , get-in , and update-in which remove elements, get them without removal, or modify them

Is there a shorthand way to update a specific struct field in racket?

浪子不回头ぞ 提交于 2019-12-03 23:49:17
Suppose I have a struct with many fields: (struct my-struct (f1 f2 f3 f4)) If I am to return a new struct with f2 updated, I have to rephrase every other fields: (define s (my-struct 1 2 3 4)) (my-struct (my-struct-f1 s) (do-something-on (my-struct-f2 s)) (my-struct-f3 s) (my-struct-f4 s)) Which is redundant and would be a source of bugs if I update the number of the fields or changed their orders. I really wonder if there's a such way I can update a specific field for a struct like: (my-struct-f2-update (my-struct 1 2 3 4) (lambda (f2) (* f2 2))) ;; => (my-struct 1 4 3 4) Or I can just set

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

Haskell: Reusing FromJSON instances with lenses, lens-aeson, and nested JSON

只愿长相守 提交于 2019-12-03 09:43:00
问题 I have been playing with Aeson and the lens package (lens-aeson, migrated from the core lens package), and have been sruggling to get them to work together. As a toy example, I have a type: data Colour = Yellow | Green | Blue and the FromJSON instance: instance FromJSON Colour where parseJSON (String s) = return $ case s of "blue" -> Blue "green" -> Green _ -> Yellow parseJSON _ = mzero So far so good. Now, say I have some nested JSON data come in that I want to extract just this out of: {

Ramda js: lens for deeply nested objects with nested arrays of objects

試著忘記壹切 提交于 2019-12-03 05:46:43
问题 Using Ramda.js (and lenses), I want to modify the JavaScript object below to change "NAME:VERSION1" to "NAME:VERSION2" for the object that has ID= "/1/B/i". I want to use a lens because I want to just change one deeply nested value, but otherwise retain the entire structure unchanged. I don't want to use lensIndex because I never know what order the arrays will be in, so instead, I want to "find" the object in an array by looking for its "id" fields. Can I do this with lenses, or should I do

Fisheye/Wide-Angle lens Calibration in OpenCV

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know the default OpenCV Calibration systems model a Pinhole camera, but I'm working with a system using extremely wide FOV lens (187-degrees). If there any existing way to do this in OpenCV, or to work with just wide lenses? Or will I have to rewrite all the calibration/undistort for my system? 回答1: Seems there's no good OpenCV way to do this. I wound up using OCamLib to do the actual calibration, then writing my own "undistortPoints" function (using Scaramuzza's algorithms) to undistort 2D image points into 3D unit vectors (rather than 2D