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
44
votes
3 answers

What are Prisms?

I'm trying to achieve a deeper understanding of lens library, so I play around with the types it offers. I have already had some experience with lenses, and know how powerful and convenient they are. So I moved on to Prisms, and I'm a bit lost. It…
Michail
  • 1,843
  • 1
  • 17
  • 21
31
votes
1 answer

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

All I know is that one works and the other doesn't. Context: I have one data structure F which contains a Data.Map.Map k S to another data structure S. My goal was to build a Lens that given an F and k would describe a field in S. The difficulty is…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
30
votes
4 answers

What are the differences between lenses and zippers?

This is an example of using a zipper in Haskell: data Tree a = Fork (Tree a) (Tree a) | Leaf a data Cxt a = Top | L (Cxt a) (Tree a) | R (Tree a) (Cxt a) type Loc a = (Tree a, Cxt a) left :: Loc a -> Loc a left (Fork l r, c) = (l, L c r) right ::…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
25
votes
1 answer

Why class constraint in type synonym needs RankNTypes

This compiles fine: type List a = [a] But when I introduce a class constraint, the compiler asks for RankNTypes to be included: type List2 a = Num a => [a] After including that extension, it compiles fine. Why is that extension required for…
Sibi
  • 47,472
  • 16
  • 95
  • 163
22
votes
2 answers

Isn't it redundant for Control.Lens.Setter to wrap types in functors?

I'm watching the Control.Lens introduction video. It makes me wonder why is it needed for the Setter type to wrap things in functors. It's (roughly) defined like this: type Control.Lens.Setter s t a b = (Functor f) => (a -> f a) -> s -> f t Let's…
qwe
  • 766
  • 1
  • 5
  • 15
21
votes
1 answer

How to combine lenses (not compose)

In haskell without lenses I can do things like : data Item = Item { quantity :: Double, price ::Double } cost :: Item -> Double cost = (*) <$> quantity <*> price If I use lenses instead how can I do the equivalent ? The best I can do is cost = to…
mb14
  • 22,276
  • 7
  • 60
  • 102
21
votes
2 answers

Using a Lens to read multiple fields

Given the types data Prisoner = P { _name :: String , _rank :: Int , _cereal :: Cereal } data Cereal = C { _number :: Int , _percentDailyValue :: Map String Float …
rampion
  • 87,131
  • 49
  • 199
  • 315
20
votes
4 answers

Is it possible to get all contexts of a Traversable lazily?

lens offers holesOf, which is a somewhat more general and powerful version of this hypothetical function: holesList :: Traversable t => t a -> [(a, a -> t a)] Given a container, holesList produces a list of elements of the container along…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
20
votes
1 answer

Lens / Prism with error handling

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…
James Davies
  • 9,602
  • 5
  • 38
  • 42
20
votes
2 answers

Getting multiple results from map with "lens"

Having these imports: > import Control.Lens Control.Lens> import qualified Data.Map as Map and a map value defined as follows: Control.Lens Map> let m = Map.fromList [('a', 1), ('c', 3), ('b', 2)] I can get it's elements one by one like…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
19
votes
3 answers

Generic LensLike like mapped and traverse

Suppose I wanted to create an "optic" for the contents of MaybeT m a: maybeTContents = _Wrapped .something. _Just Is there such a something? maybeTContents would for example be a Traversal when m is [], but only a Setter when m is (->) Int. Example…
yairchu
  • 23,680
  • 7
  • 69
  • 109
18
votes
2 answers

Could someone explain the diagram about the `lens` library?

If you browse through Lens entry on hackage, Lens Github's repo, or even google about Lens, you will find a lot of partial references such as introductory tutorials/videos, examples, overviews and so on. Since I already know most of the basics, I am…
kqr
  • 14,791
  • 3
  • 41
  • 72
18
votes
4 answers

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

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…
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
18
votes
1 answer

How do I make lenses from a record in GHCi

I want to play around with the Lens library a bit. I've loaded it into GHCi and created a record data type with the appropriate underscores: > data Foo a = Foo {_arg1 :: Int, _arg2 :: [a]} I would like to make the lenses for Foo using the…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
17
votes
2 answers

Combining lenses

Using a lens library I can apply a modification function to individual targets, like so: Prelude Control.Lens> (1, 'a', 2) & _1 %~ (*3) (3,'a',2) Prelude Control.Lens> (1, 'a', 2) & _3 %~ (*3) (1,'a',6) How can I combine those individual lenses (_1…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
1
2 3
30 31