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
10
votes
1 answer

Isomorphisms between 3 and more types using lens

Inspired by a question on polymorphic function between ADTs I'm trying to create isomorphisms between multiple (not just 2) types, so that every time I need an isomorphic but not the same type, I can sprinkle my code with some convert. Suppose I…
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
10
votes
2 answers

How to make the product of two lenses?

If I have two lenses: foo :: Lens' X Foo bar :: Lens' X Bar Is there a way to construct a product lens: foobar :: Lens' X (Foo, Bar) foobar = ... foo bar or is it impossible?
phadej
  • 11,947
  • 41
  • 78
10
votes
3 answers

How to avoid default return value when accessing a non-existent field with lenses?

I love Lens library and I love how it works, but sometimes it introduces so many problems, that I regret I ever started using it. Lets look at this simple example: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data Data = A { _x :: String,…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
10
votes
2 answers

Lenses and Monomorphism Restriction

I have been working out on the examples given out in this article for creating Lenses. I created Lens as stated in the article and the following is my code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens type Degrees = Double type Latitude =…
Sibi
  • 47,472
  • 16
  • 95
  • 163
10
votes
1 answer

How do I combine lenses and functors?

I'm trying to get used to the lens library for Haskell, and find myself struggling at some simple problems. For instance, let's say (for convenience) that at and _1 have the following types (this is how I understand them, at least): at :: Ord k => k…
wen
  • 3,782
  • 9
  • 34
  • 54
10
votes
1 answer

Getting Maybe from lens with default value

A lot of lens getters return Maybe values. And i often need to replace them with some default. Say map lookup but with default. fromMaybe "" $ Map.fromList [(1,"Foo")] ^? at 1 Can this be written with lens syntax? Maybe something close to…
Vagif Verdi
  • 4,816
  • 1
  • 26
  • 31
10
votes
2 answers

Why would my datatype need to an instance of Monoid to use this lens?

I'm using the code below on a record that has a field '_scene' of type SceneGraph. I've created lenses for it using makeLenses. inputGame :: Input -> Game -> Game inputGame i g = flip execState g $ do let es = g ^. userInput . events sg…
schellsan
  • 2,164
  • 1
  • 22
  • 32
9
votes
1 answer

What's the reason behind the name Market in Control.Lens?

Edward Kmett's optics library; Control.Lens defines a large number of types. Most of these have relatively self explanatory names, like Traversal and Fold. It also defines some types with less obvious names, like Bazaar From the Bazaar…
Joe
  • 1,479
  • 13
  • 22
9
votes
3 answers

How can I write a lens for a sum type

I have a type like this: data Problem = ProblemFoo Foo | ProblemBar Bar | ProblemBaz Baz Foo, Bar and Baz all have a lens for their names: fooName :: Lens' Foo String barName :: Lens' Bar String bazName :: Lens' Baz String Now I'd like to…
Paul Johnson
  • 17,438
  • 3
  • 42
  • 59
9
votes
2 answers

Traversal over last element of 'filtered'

I'm looking to filter a traversal, then select the last element to use with over. e.g. something like this (but which will actually compile): [1,2,3,4] & traverse . filtered even . _last +~ 10 > [1,2,3,14] Any ideas? P.S. I'm aware that filtered…
Chris Penner
  • 1,881
  • 11
  • 15
9
votes
3 answers

Zipping Traversals

I have a Traversable with holes in it -- imagine this binary tree: / \ / \ Nothing Just 1 / \ Nothing Just 3 I also have a list of values to fill the holes with -- [2, 4] -- resulting in: / …
Joel Burget
  • 1,328
  • 8
  • 17
9
votes
1 answer

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

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…
jsdw
  • 5,424
  • 4
  • 25
  • 29
9
votes
1 answer

How to set the range of the axis in the Haskell Chart library

In haskell-chart, how do you change the range of an axis? It seems like it has something to do with lenses and viewports, but as a beginner in Haskell, I'm having a hard time reading the…
Craig
  • 255
  • 1
  • 6
9
votes
1 answer

Haskell - Lenses, use of 'to' function

I have the following code. I'd like to be able to modify the active player's life when given a game state. I came up with an activePlayer lens, but when I try and use it in combination with the -= operator I receive the following error: > over…
Dwilson
  • 1,239
  • 9
  • 18
9
votes
1 answer

indexing list with Control.Lens requires Monoid constraint

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 =…
ajp
  • 1,723
  • 14
  • 22
1 2
3
30 31