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

Design code to avoid rewriting during maintenance

I have lens based code that expects a certain data structure: makeLenses ''Pool ... pool ^. objects ^? _head :: Maybe Object This code assumes that: objects :: Functor f => ([Object] -> f [Object]) -> Pool -> f Pool But I want to introduce new…
sevo
  • 4,559
  • 1
  • 15
  • 31
4
votes
3 answers

How would I use lens in Haskell to duplicate Python's enumerate?

Python's enumerate on lists can be written as zip [0..]. I looked at Control.Lens.Traversal and Control.Lens.Indexed, but I couldn't figure out how to use lenses to generalize this to any reasonable container (I hesitate to say "Traversable"). I'm…
Alex R
  • 2,201
  • 1
  • 19
  • 32
4
votes
1 answer

Type error with rank-n types and lenses

I have a simple polymorphic datatype Foo {-# LANGUAGE TemplateHaskell #-} import Control.Lens data Foo c = Foo { _bar :: c, _baz :: c, _quux :: c } makeLenses ''Foo The generated lenses are, of course,…
tjhance
  • 961
  • 1
  • 7
  • 14
4
votes
1 answer

How to define settable lens

I have a record type in my Haskell code to which I want to define a lens that can be used as a getter and as a setter. The code looks like this: data Players = Players { _white :: Player , _black :: Player …
Lemming
  • 4,085
  • 3
  • 23
  • 36
4
votes
2 answers

How to remove an item from a list by index using the lens library?

I can view the, say, 4th item in a list using a lens like this: preview (ix 3) myList Is there something that could replace "preview" in order to remove the fourth item from the list instead of viewing it? The return list should be the same as the…
Richard Parsons
  • 277
  • 1
  • 9
4
votes
1 answer

How do you fmap a Getter?

As discussed on reddit, you can't just lift a Lens' a b to Lens' (Maybe a) (Maybe b). But for the special case Getter a b, this is obviously possible, since it's isomorphic to a->b. But unlike with Iso, there appears to be no standard function to…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
4
votes
1 answer

How can I express `mapM` with `concat` using Lenses to concatenate results of an IO operation?

I'm trying to figure out a way how to combine traverseOf with >>= in such a way that would allow the following. TLDR; A simple example in plain Haskell would be something like this, but using lenses deep inside a data structure. λ> fmap concat $…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
4
votes
1 answer

Aeson Prisms with "free" references

Just read the brilliant "Lens/Aeson Traversals/Prisms" article and have a real-world application. Given the following anonymized JSON structure, how would I prism out a collection rather than a specific value? {"Locations" : [ {"id" : "2o8434",…
Brandon
  • 308
  • 1
  • 2
  • 10
4
votes
2 answers

Lenses: Composing backwards and (.) in Lens context

I have been reading this article and in one of their section it is stated: Lenses compose backwards. Can't we make (.) behave like functions? You're right, we could. We don't for various reasons, but the intuition is right. Lenses should combine…
Sibi
  • 47,472
  • 16
  • 95
  • 163
4
votes
2 answers

How does get function typecheck in lens

I have been reading this post to understand lens. They initially define a type synonym like this: type RefF a b = forall f. Functor f => (b -> f b) -> (a -> f a) Const is defined like this: newtype Const a b = Const { getConst :: a } How does the…
Sibi
  • 47,472
  • 16
  • 95
  • 163
4
votes
0 answers

How is using the lens library different than programming in an imperative language?

What advantages do we get by using the lens library over a language like C or Python? I get that state is still immutable with the lens library, but from a practical perspective, how is it any different? Examples would be very useful. EDIT: What I…
Emil
  • 2,098
  • 11
  • 25
4
votes
2 answers

How to check if map has a key using lens syntax?

How to check if map has a key using lens syntax? import qualified Map as Map let x = Map.member "bla" m How to write this using lenses?
Vagif Verdi
  • 4,816
  • 1
  • 26
  • 31
4
votes
2 answers

How can I use Lenses to perform read-only monadic operation over a sequence held in some state?

My data structure looks more or less like this (simplified for the purpose of the question) data GameObject = GameObject { _num :: Int } data Game = Game { _objects :: [GameObject] } I use makeLenses to generate the accessors for both of those.…
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
4
votes
1 answer

Is there some method to construct value for record with lenses without underscore identifiers?

For example I have the following record data Rec = Rec { _a :: Int , _b :: Int , _c :: Int } deriving (Show, Eq) makeLenses ''Rec and I see only 2 ways to constuct new values: Rec{_a=1,_b=2,_c=3} Rec 1 2 3 The second variant does…
Qrilka
  • 612
  • 1
  • 6
  • 19
4
votes
1 answer

How does one use an IndexedTraversal in Control.Lens to perform an index-aware action for each element?

I have an IndexedTraversal (from the Control.Lens package) and I would like to apply an index-aware monadic action to each element in it. Unfortunately, all of the convenient ways that I see of doing something like this --- such as ^! combined with…
Gregory Crosswhite
  • 1,457
  • 8
  • 17