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

Lenses with interdependent (simultaneous) updates

Lets say I have: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TupleSections #-} import Control.Lens data T a b = T { _foo :: a , _bar :: a -> b } makeLenses ''T a appears in both foo and bar, so updates have to be…
user2141650
  • 2,827
  • 1
  • 15
  • 23
8
votes
1 answer

Aeson and Lens with DeriveGeneric and makeLenses - names don't line up

Let's say I have a type Person import GHC.Generics import Data.Text import Data.Aeson import Control.Lens data Person = Person { _firstName :: Text, _lastName :: Text, _age :: Int } deriving (Show, Generic) And I want to…
James Davies
  • 9,602
  • 5
  • 38
  • 42
8
votes
3 answers

What is the difference between ix and element in the Lens library of Haskell

In Haskell's lens library, ix and element both take an Int an can be used e.g. to read or write a list element at some index, like this ghci> [1..10] ^? ix 4 Just 5 ghci> [1..10] & ix 4 .~ 1 [1,2,3,4,1,6,7,8,9,10] and similarly: ghci> [1..10] ^?…
Stephan
  • 746
  • 5
  • 14
8
votes
2 answers

With the Haskell lens library, how do I treat getters as `first class'?

I've noticed I'm commonly building functions that get values using lenses, apply some function to the values and return the result. For example, to sum the elements of a pair \pair -> (pair ^. _1) + (pair ^. _2) I feel like there should be some…
8
votes
1 answer

Relationship between forward and backward map in Isomorphism (Lens package)

Why does/Should nothing constrain s to be isomorphic to t, and b to be isomorphic to a in an Isomorphism of type Iso s t a b? I understand we have a forward mapping s -> a, and a backward mapping b -> t, but why is there no relationship imposed on…
user3585010
  • 135
  • 4
8
votes
1 answer

How can I use `over` from Control.Lens but perform a monadic action and collect the results?

The problem is pretty simple. I have a structure that looks something like this data Foo = Foo [Bar] data Bar = Boo | Moo Item Int data Item = Item String Int and I have a lens for changing the contents of the Items inside the data structure, such…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
8
votes
3 answers

How to combine lenses in "parallel"

I'm new to the excelent Control.Lens and I'm trying to combine 2 lens in "parallel" (not in sequence) like I would do with `Control.Arrow.&&&). If I take the example from the lens documentation: `data Foo a = Foo { _baz :: Int, _bar :: Int, a } I…
mb14
  • 22,276
  • 7
  • 60
  • 102
8
votes
2 answers

Generating lenses for a "lens" library with a custom name processor instead of the default "underscore"-based one

The standard makeLenses implementation generates lenses for all the fields of a record which begin with underscore. I very much dislike the idea of having to introduce such an awkward naming convention to my records for many reasons. What I want to…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
7
votes
1 answer

Are there rank N lenses?

Suppose I have a simple GADT. data Expr a where Int' :: Integer -> Expr Int Fun :: Text -> Expr a (:*) :: Expr (a -> b) -> Expr a -> Expr b Now, I can define the following traversal over it: transform :: Applicative f => (forall b. Expr b ->…
grepcake
  • 3,960
  • 3
  • 16
  • 26
7
votes
1 answer

Haskell Identity Lens

I'm wondering if there is an identity lens in Haskell. A lens identity such that if I had a type data MyType = MyType { _myField :: Int }, then I can do myType ^. identity . myField .~ 2 . There seemed to be one in lens-1.1.1, but I can't find one…
7
votes
1 answer

Why does this lens function require a type signature?

I'm writing a function which uses the lenses library, but curiously, the code doesn't compile when I remove the type annotation. {-# LANGUAGE TemplateHaskell, Rank2Types #-} import Control.Lens import Control.Monad.State import Data.List…
Joe the Person
  • 4,470
  • 3
  • 22
  • 32
7
votes
1 answer

Is there a “dual” to zooming?

zoom allows us to use a state action that only uses some state variables, in a context where more variables are actually defined. {-# LANGUAGE TemplateHaskell #-} import Control.Lens import Control.Monad.Trans.State import…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
7
votes
1 answer

How to use a list of lens?

Can I use a list of Traversal? The following code: f::[Int] -> [[Int]] f l = [l & i .~ 1 | i<-[ix 0], (l^? i) == Just 0] produces an error: • Couldn't match type ‘Const (Data.Monoid.First Int) [Int]’ with ‘Identity [Int]’ …
Alexei Kopylov
  • 280
  • 1
  • 5
7
votes
1 answer

What are simple definitions for Control.Lens.Traversal's partsOf, holesOf and singular?

I'm trying to learn more about the lens library. I already understand the lenses in the lens-family package and their derivation and also grasp the two type parameter versions of Store, Pretext and Bazaar, but I'm having trouble understanding…
Johannes Riecken
  • 2,301
  • 16
  • 17
7
votes
1 answer

How can I use overloaded record fields with lenses?

It's possible to mix classes with lenses to simulate overloaded record fields, up to a point. See, for example, makeFields in Control.Lens.TH. I'm trying to figure out if there's a nice way to reuse the same name as a lens for some types and a…
dfeuer
  • 48,079
  • 5
  • 63
  • 167