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

traversal tree with Lens and Zippers

I'm learning the Lens package. I must say it's a rather challenging task. Can someone show me how to traverse a Tree with Zipper from Lens? In particular, how can I write a function that takes a list of roots and allows me to access the branches of…
Kai
  • 263
  • 1
  • 6
16
votes
3 answers

What's the difference between makeLenses and makeFields?

Pretty self-explanatory. I know that makeClassy should create typeclasses, but I see no difference between the two. PS. Bonus points for explaining the default behaviour of both.
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
16
votes
1 answer

How to modify using a monadic function with lenses?

I needed a lens function that works like over, but with monadic operations: overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) While this function is easy to define (it's actually just an identity modulo WrappedMonad), I wonder are such…
Petr
  • 62,528
  • 13
  • 153
  • 317
15
votes
1 answer

Why does “failing” (from lens) produce invalid traversals?

From the documentation: Try the first Traversal (or Fold), falling back on the second Traversal (or Fold) if it returns no entries. This is only a valid Traversal if the second Traversal is disjoint from the result of the first or returns exactly…
Emily
  • 2,577
  • 18
  • 38
15
votes
3 answers

How do I handle the Maybe result of at in Control.Lens.Indexed without a Monoid instance

I recently discovered the lens package on Hackage and have been trying to make use of it now in a small test project that might turn into a MUD/MUSH server one very distant day if I keep working on it. Here is a minimized version of my code…
14
votes
3 answers

Simulating interacting stateful objects in Haskell

I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable…
N. Virgo
  • 7,970
  • 11
  • 44
  • 65
14
votes
1 answer

Reconciling lens usage with database access

I've been playing around with lenses recently, and finding them very pleasant for their intended usage - digging into complex data structures. But one of the areas that I'd most appreciate them is in database access (specifically sqlite, but I think…
14
votes
1 answer

Haskell: Template Haskell and the scope

This code is compiled fine: data None = None { _f :: Int } type Simpl = Env type Env = Int However, I got an error with this code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data None = None { _f :: Int } type Simpl = Env makeLenses…
IruT
  • 323
  • 1
  • 8
13
votes
2 answers

Is there a Haskell lens function for "zipping" same-length tuples?

I would like to be able to combine two tuples of the same length using a function, similar to the zipWith function from base. For example, for the case of length 3 tuples: zipTupleWith f (a0,a1,a2) (b0,b1,b2) = (f a0 b0, f a1 b1, f a2 b2) Though I…
Oli
  • 2,507
  • 1
  • 11
  • 23
13
votes
2 answers

Using lens to add key and value to a nested Map

I am struggling to figure out an issue with manipulating JSON with Aeson lenses. My task is as simple as to add a key to a nested object in JSON. I was able to change the existing keyby means of: > :set -XOverloadedStrings > import Control.Lens >…
SkyWriter
  • 1,454
  • 10
  • 17
13
votes
3 answers

Can I make a Lens with a Monad constraint?

Context: This question is specifically in reference to Control.Lens (version 3.9.1 at the time of this writing) I've been using the lens library and it is very nice to be able to read and write to a piece (or pieces for traversals) of a structure. I…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
12
votes
1 answer

Is there a shortcut for this in Lens?

Is there a shortcut for this in the lens library? \x -> liftM (^. x) get Maybe it's a silly question, but it feels like a basic enough construction that there should be a shortcut for it.
Emil
  • 2,098
  • 11
  • 25
11
votes
1 answer

Haskell, Lenses, Getters, and Setters

I'm having trouble understanding all the nuances of the lens library in Haskell. Suppose I have the following lens activePlayer :: Lens' Game Player activePlayer = lens get set where get (Game {_players = (index, seq) }) = S.index seq…
Dwilson
  • 1,239
  • 9
  • 18
11
votes
2 answers

"Illegal polymorphic or qualified type" in Control.Lens

I'm working with Control.Lens. The actual function I'm writing is rather complex, but for the purpose of this question, I've boiled it down to a minimal failing example: import Control.Lens exampleFunc :: Lens s t a b -> String exampleFunc _ =…
rlkw1024
  • 6,455
  • 1
  • 36
  • 65
10
votes
1 answer

What is the dual of a prism or an affine traversal?

A prism is an optic for focusing into coproduct types, while affine traversal is a kind of optic which can focus at 0 of 1 element, i.e. AffineTraversal s t a b is isomorphic to (s -> Maybe a, (s, b) -> t). As far as I know, we get an affine…
Kristóf Marussy
  • 1,202
  • 8
  • 18
1
2
3
30 31