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

Lenses over Comonads or Representable

Here's a more specific variant of this question: Mutate only focus of Store Comonad?, for the benefit of not asking more than one question at once. Are there any lenses compatible with Control.Lens which allow me to interact with the focus of a…
Chris Penner
  • 1,881
  • 11
  • 15
5
votes
3 answers

How to compose "Maybe" lenses?

If I have lenses for a nested record, where each lens returns a Maybe, how can I get them to compose, so that if anything in the "traversal" returns a Nothing the final result is a Nothing? data Client = Client { clientProperties :: Maybe…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
5
votes
0 answers

Understanding confusing

I've been thinking about whether there's some method that could be added to Traversable to make it compose more cheaply in the presence of expensive functors. The inspiration was Control.Lens.Traversal.confusing, which uses a special Applicative to…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
5
votes
1 answer

Analog of `<<%~` not requiring Monoid for Traversal

I need a function like <<%~ which would act with Traversals in similar fashion to ^?, like this: (< (a -> b) -> s -> (Maybe a, t) > ix 0 < ix 1 < ix 2…
modular
  • 1,099
  • 9
  • 22
5
votes
1 answer

Creating lenses with DuplicateRecordFields

How would you go about generating lenses with the new DuplicateRecordFields enabled? I've already tried using makeLenses, but nothing was generated at all. I also tried importing my constructors with a qualifier to prevent name clashes between…
SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25
5
votes
1 answer

What in lens should I use to build a read-only getter by index?

I have a type whose internal details are hidden. I want to provide some kind of lens that can read elements from said type at particular indexes, but not modify them. An Ixed instance for my type doesn't seem to do what I want, as it explicitly…
Koz Ross
  • 3,040
  • 2
  • 24
  • 44
5
votes
3 answers

What is the story behind a Getter?

I stumbled upon Getter definition having both Functor and Contravariant constraint on f. It's not surprising that "getter" can't do much with the "contained part" but this signature looks like a Phantom in a "van Laarhoven" setting of (a -> f a) ->…
sevo
  • 4,559
  • 1
  • 15
  • 31
5
votes
3 answers

Make Lenses (TH) with the Same Field Name using makeClassy

This question is regarding Edward A. Kmett's lens package (version 4.13) I have a number of different data types all of which have a field that denotes the maximum number of elements contained (a business rule subject to run time change, not a…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
5
votes
1 answer

Control.Lens: Traversal isomorphism to toListOf and over

I watched Simon Peyton Jones' talk about Control.Lens, and he showed that Lens and LensR as defined here are isomorphic: type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t data LensR s t a b = LensR { viewR :: s -> a, setR …
RhubarbAndC
  • 494
  • 2
  • 13
5
votes
2 answers

Monadic alteration to tuple

I am looking for a function with type similar to: Monad m => (a, b) -> (b -> m c) -> m (a, c) It appears to me as some combination of bind (>>=) and a lens operation. I am aware that I can solve this with a pattern match after a bind, but my gut…
Luke Cycon
  • 648
  • 4
  • 12
5
votes
1 answer

Difficulty with zoom and free monads

I am mucking around with free monads and lens, using the free monad to create my own version of the IO monad: data MyIO next = LogMsg String next | GetInput (String -> next) deriving (Functor) I am stacking this on top of a state monad…
Pubby
  • 51,882
  • 13
  • 139
  • 180
5
votes
1 answer

Lens: zooming newtype

I'm interested in getting a zooming functionality for my monad transformer stack which is defined the following way: newtype Awesome a = Awesome (StateT AwesomeState (ExceptT B.ByteString IO) a) deriving (Functor, Applicative, Monad ,…
ksaveljev
  • 550
  • 2
  • 16
5
votes
1 answer

Point-free lens creation does not type check

In the function test, I traverse over a list, generate lenses from it's members, and then print some data. This works when I use a pointful call style. It fails to typecheck when I make it point-free. Why is this the case, and how can I solve this…
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
5
votes
0 answers

Serializing a Setter

My program has a client and a server component that need to communicate. They each have a state that they mutate. The mutation is done explicitly in the following way: There a relation class Diff a b where commit :: a -> b -> a That says that…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
5
votes
2 answers

How to use IORef with lenses?

Wondering how best to combine the Control.Lens package with IORefs. Specifically I'd like to be able to use atomicModifyIORef with lenses so that i can supply a function of type a -> (a, b) and return a value from the operation. Code snippet: let…
Scott
  • 4,070
  • 3
  • 21
  • 16