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

Lens modifying with failure

In Control.Lens.Lens, there is a function modifying :: MonadState s m => ASetter s s a b -> (a -> b) -> m () which allows the value under a lens on the MonadState state to be transformed by a pure function (a -> b). However, we may want to allow…
pat
  • 12,587
  • 1
  • 23
  • 52
4
votes
1 answer

Can I build something like a lens when my getter and setter return `Either`?

In brief My getter and setter could both fail, with messages describing how. Therefore they return Either String, which means I can't make lenses out of them in the normal way. In detail Consider these types: import qualified Data.Vector as V data…
Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40
4
votes
0 answers

Why are indexed lenses defined the way they are?

There are some very helpful explanations in this reddit thread of how indexed lenses are defined, but I'd like to know the details. So the main point is that indexed lenses are defined such that they can be treated as non-indexed ones too. These two…
effectfully
  • 12,325
  • 2
  • 17
  • 40
4
votes
2 answers

Index a Traversal with a Lens

I have a lens pointing to a json document, e.g. doc ^? ((key "body").values) Now I would like to index the values in body with the key "key", because the json looks like {"body": [{"key": 23, "data": [{"foo": 1}, {"foo": 2}]}]} So I'm looking for…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
4
votes
1 answer

Using lenses for accessing data inside functors

I find lenses very useful for accessing deeply nested data, but frequently "containers" like MVar or TVars throw off some of the nice properties of lenses. For example: data SomeStruct = SomeStruct { _b :: Int …
xal
  • 127
  • 1
  • 5
4
votes
3 answers

Polymorphic lens without template haskell

I am trying to create a polymorphic lens decleration (without template haskell) for multiple types. module Sample where import Control.Lens data A = A {_value:: Int} data B = B {_value:: Int} data C = C {_value:: String} value = lens _value (\i x ->…
yilmazhuseyin
  • 6,442
  • 4
  • 34
  • 38
4
votes
2 answers

Pass a lens into a function

I'd like to perform a sort based on a specific value within a record. As such I was thinking of passing a lens into a lensSort function but I've been unable to make it work. Ideally I could do something like this lensSort :: HasLens a => Lens' a b…
eddiec
  • 7,608
  • 5
  • 34
  • 36
4
votes
1 answer

Space leak / Bug in interaction of Control.Lens.Plated and Bound

Using the latest version of Bound and Control.Lens.Plated, the following transform call causes the programm to loop infinitely and happily munches away at my RAM. Terminating the program funnily causes the correct result to be printed, although I…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
4
votes
1 answer

Type families can't return a RankN type -- workarounds or alternatives?

I'm playing with an extensible record library, and I'm wanting to write a function field that can operate as either a Lens or a Traversal based on whether or not the Symbol key is in the list of keys. The type family is given: type family…
ephrion
  • 2,687
  • 1
  • 14
  • 17
4
votes
2 answers

How to zoom a monad transformer?

thanks again for the help! I'm making extensive use of the E. Kmett's Lens library, to avoid X/Y problems I'll explain a bit of context. I'm working on an extensible text editor and want to provide extension writers with a monad DSL, an Alteration…
Chris Penner
  • 1,881
  • 11
  • 15
4
votes
1 answer

When should I use MonadState lens combinators?

I understand that a MonadState s m gives me ability to get and update an s. I do not however understand how this state is related to the state used in lens combinators like assign. Especially when lens can operate on multiple targets. Also, there…
sevo
  • 4,559
  • 1
  • 15
  • 31
4
votes
0 answers

What is the lens equivalent of bitraverse?

Is there a way to create a lens that could do a similar thing to this function? λ> :t \f -> bitraverse f f \f -> bitraverse f f :: (Applicative f, Bitraversable t) => (b -> f d) -> t b b -> f (t d d) The specific type I'm trying to satisfy…
SimonF
  • 804
  • 1
  • 7
  • 14
4
votes
2 answers

Prism for converting to a Bounded Integral

I needed a Prism for converting an Integral a => a to an (Integral b, Bounded b) => b, ensuring that the a actually fits into the type of b. My current definition (below) requires the use of ScopedTypeVariables and is quite verbose. I would like to…
frasertweedale
  • 5,424
  • 3
  • 26
  • 38
4
votes
2 answers

Getting value with a Lens s t a b

I would like write a function which turns a function (a -> b) into a function (s -> t) with the help of a Lens s t a b (edit: I realised that this function already exists with Setter s t a b and is called over or %~, but it doesn't answer the…
Guillaume Chérel
  • 1,478
  • 8
  • 17
4
votes
1 answer

How can I use (^? ix 0) in an independent Getter?

Sorry for the poorly worded title, but I don't even know how to ask it properly. How can I turn this? instPublicIP :: Instance -> Maybe Text instPublicIP inst = inst ^. insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP .…
Joe Hillenbrand
  • 845
  • 9
  • 26