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
7
votes
2 answers

How to compose lenses that return Maybe (Haskell)

Suppose I have a lens like at _ that needs some Maybe a: import Data.Map as M m = M.fromList [(1,(2,3))] --set 2nd element m ^. at 1 .~ Just (4,5) --gives fromList [(1,(4,5))] m ^. at 1 .~ Nothing --gives fromList () Now suppose I want to compose…
holdenlee
  • 969
  • 1
  • 8
  • 21
7
votes
1 answer

Applying functions that depend on multiple fields using the lens

Let A, B, C be types and there are two functions f :: (A , B) -> A and g :: (A , B) -> B. Consider following record type data Rec = Rec{_a :: A, _b :: B, _c :: C}. What would be the most elegant way to define function that maps (Rec a b c) to (Rec…
Katty J.
  • 686
  • 4
  • 11
7
votes
2 answers

Export only getter or setter from a module

Is there a way for me to only export specific getters xor setters from a module with a lens? For example, let's assume a data structure that has an invariant of being always >= 0, being modified only by incrementing it and being created only with…
Shoe
  • 74,840
  • 36
  • 166
  • 272
7
votes
3 answers

Using a lens twice

I'm struggling with using the lens library for a particular problem. I'm trying to pass an updated data structure a lens focussed on part of that updated structure to another function, g. I pass both the lens and the data structure because g needs…
dbeacham
  • 986
  • 2
  • 9
  • 16
7
votes
2 answers

Using Lens in Haskell to modify values

I find myself using this pattern often: do let oldHeaders = mail ^. headers put $ (headers .~ (insert header value oldHeaders)) mail which seems like the kind of thing Control.Lens should be able to do, but I guess I just haven't found the…
Drew
  • 12,578
  • 11
  • 58
  • 98
6
votes
1 answer

Is there a way to compose reified lenses?

I was trying to use the lens library to solve the following problem: Given the list version of a tree, make a tree. Example: Given: [1,2,3,4,5,6,7] I should make a tree: 1 2 3 4 5 6 7 My solution was to create nodes according to…
user821596
  • 77
  • 6
6
votes
2 answers

Why does Setter have Traversable constraint?

When you look at setting in lens-family-core package, you'll find that its type is Identical f => ((a -> b) -> s -> t) -> LensLike f s t a b, and Identical is defined as class (Traversable f, Applicative f) => Identical f. I understand it needs…
snak
  • 6,483
  • 3
  • 23
  • 33
6
votes
2 answers

Haskell - iso on newtype

If I have a newtype newtype Foo = Foo Int is there an automatic way to get an Iso' Foo Int? I saw I could use makeLenses ''Foo, but I don't know what is the name of the generated iso.
marcosh
  • 8,780
  • 5
  • 44
  • 74
6
votes
1 answer

Apply two Folds or Getters and only succeed when both succeed

Imagine I have the following list: lst :: [(Bool, Maybe Integer)] lst = [(True, Just 3), (True, Nothing), (False, Just 12)] Using the lens library, I want to extract the elements of the tuples, but I only want it to succeed when the second element…
Alexis King
  • 43,109
  • 15
  • 131
  • 205
6
votes
2 answers

Is there a direct way to combine setters for multiple record fields to a single setter?

import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''Foo Now if I want to modify both int fields, I can do barbaz :: Setter' Foo Int barbaz = sets $ \foo f -> foo & bar %~ f …
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
6
votes
2 answers

Use Haskell's lenses library to fmap a lens

In the code below, my question concerns the top-most function someFunc (everything below is just to provide a complete example). I use a record-syntax getter and fmap there. What's the lens way to implement someFunc? import …
ruben.moor
  • 1,876
  • 15
  • 27
6
votes
1 answer

What's the best way to represent a short bit string?

I want to represent a string of up to around 120 bits, and speed is critical. I need to be able to build a bitstring by repeated snoc operations, and then to consume it with repeated uncons operations. One idea is to steal the implementation of…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
6
votes
1 answer

resource that explains vocabulary used in Edward Kmett's lens package

I am trying to read the documentation in Edward Kmett's Lens package. I am not familiar with a lot of the terms used (profunctor, isomorphism, monomorphic, contravariant, bifunctor, etc...) What would be a good resource to go to learn some of this…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
6
votes
1 answer

Use a lens to replace a specific element of a (key,value) list

I want to use Kmett's lens library to access an element of a (key, value) list under a specific key. In other words, I would like to replace this code with something more idiomatic and perhaps shorter: type Headers = [ (ByteString, ByteString)…
dsign
  • 12,340
  • 6
  • 59
  • 82
6
votes
1 answer

Is this expected behavior of Template Haskell?

Can anyone tell why this code doesn't compile data A = A { _b :: B } makeLenses ''A type B = String with message Not in scope: type constructor or class B and this does: type B = String data A = A { _b :: B } makeLenses ''A Without…
Alexey Vagarenko
  • 1,206
  • 8
  • 15