Questions tagged [haskell-lens]

A lenses library for Haskell.

An extensive library which provides families of lenses, isomorphisms, folds, traversals, getters and setters.

For more info visit:

454 questions
5
votes
0 answers

How to use lens and extensible-effects?

I would like to use lens and extensible-effects to work a simple example. Error messages say that type is ambiguous because of typeclass with parameter HasObj x and GHC cannot understand where pos come from, I guess. makeClassy is really useful to…
IruT
  • 323
  • 1
  • 8
5
votes
1 answer

How can I traverse different parts of data structure one after the other?

In Control.Lens.Traversal the beside function traverses both parts of a Bitraversable. The example given is >>> ("hello",["world","!!!"])^..beside id traverse ["hello","world","!!!"] Can I write a more explicit version of beside (let's call it…
phischu
  • 440
  • 2
  • 6
5
votes
2 answers

Control.Lens lens for a total map

I have a total map from type A to type B. import qualified Data.Map as M import Data.Maybe tmGet k m = fromJust $ M.lookup k m tmSet k v = M.insert k v I used Data.Map as an example implementation but it can be anything, e.g. an Array or even a…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
5
votes
2 answers

Extracting Values from JSON using lens-aeson

I just read the tutorial at https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms, and I have successfully written a query into a json bytestring. However, I am not getting the kind of result value I want. I'd like to do something along…
nomen
  • 3,626
  • 2
  • 23
  • 40
5
votes
1 answer

Haskell List and Control.Lens

I'm writing an AST library for a simple "dynamically typed" language. I have written my syntax tree and parser. Now I'm working on manipulating the AST, and I am interested in using the lens package for that purpose. Consider data Obj = Obj !(Map…
nomen
  • 3,626
  • 2
  • 23
  • 40
5
votes
2 answers

Searching for missing State Combinator for Lens

I currently have code that looks like this: do x <- use foo foo <~ runFoo x where foo is a Lens to a Foo field and runFoo :: MonadState m => Foo -> m Foo I think there should be a way to do this operation in one line, but I cannot find it. I…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
4
votes
1 answer

How to avoid the warning "Redundant constraint: Functor f" in the getter?

I have some record SomeRecord and fields like _user, _password. And I want to write a Getter for a "virtual" field, like identity which will look like :: identity:: Getter SomeRecord String identity = to $ \srec -> srec^.user <> ":"…
RandomB
  • 3,367
  • 19
  • 30
4
votes
0 answers

Simplification of `eitherPrism`

The Prism' here comes from optics-core not lens. Is there a way to simplify this implementation, or is this as simple as it gets? {- | Given two @Prism'@'s whose filepaths are distinct (ie., both @a@ and @b@ encode to distinct filepaths), return a…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
4
votes
1 answer

How to get rid of these apparently superfluous `undefined`s?

I'm using GHC 9.2.2 and playing with OverloadedRecordDot and generic-lens. As an experiment, I want to use the overloaded dot as a "frontend" to the generic-lens functionality (including type-changing update). I have these auxiliary definitions: {-#…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
4
votes
2 answers

Haskell lens : view doesn't reference like over and set?

First time using lens. set and over went easy enough and I thought it would be simple with view: Use the same scheme to reference the inner part, but don't supply a new value or function. But Noooo. tst3 below gives the error below the code. Anyone…
user49011
  • 523
  • 1
  • 3
  • 10
4
votes
3 answers

Lenses, the State monad, and Maps with known keys

here is a puzzle that I keep on bumping into and that, I believe, no previous SO question has been addressing: How can I best use the lens library to set or get values within a State monad managing a nested data structure that involves Maps when I…
copton
  • 338
  • 1
  • 9
4
votes
1 answer

Using lenses to mappend two fields of a record

I'm trying to get used to some basic lens features. I started with the following type and function, before trying to introduce lenses: import qualified Data.Set as S data Sets = Sets {pending, placed, open :: S.Set Int} interesting :: Sets ->…
amalloy
  • 89,153
  • 8
  • 140
  • 205
4
votes
1 answer

Updating a nested data structure using lenses

I'm currently trying to make parts of my code more concise using lenses. In particular, I have a HTTP Request where I want to replace the value of a header with the name Private-Header. I managed to write the function that updates the…
l7r7
  • 1,134
  • 1
  • 7
  • 23
4
votes
2 answers

Using effect of Traversable [] and Applicative Maybe in lens library

I have the following structure: y = [ fromList([("c", 1 ::Int)]), fromList([("c", 5)]), fromList([("d", 20)]) ] I can use this to update every "c": y & mapped . at "c" . mapped %~ (+ 1) -- [fromList [("c",2)], fromList [("c",6)], fromList…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
4
votes
0 answers

How to filter Array values using Data.Lens.Aeson before parsing them?

I have an incoming JSON which looks like: { "somekey": [ { "objectType": "typeA" , "key1": "val1" , "key2": "val2" } , { "objectType": "typeB" , "key3": "val3" , "key4": "val4" } ] While parsing, I want to deal…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60