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

What is the purpose of united lens?

While reading through the Lens over Tea #1 article, I found the united lens. My implementation is united :: Lens' a () united f v = const v <$> f () which is pretty much the same as the implementation in the Lens library. What leaves me completely…
Lubomír Sedlář
  • 1,580
  • 2
  • 17
  • 26
6
votes
1 answer

Modifying the target of a Lens conditionally

I have a function that produces an updated board from an input and a board, if the move is permitted by the rules of the game: move :: Input -> Board -> Maybe Board The board is wrapped in the GameState type with some additional data: type…
vtan
  • 75
  • 4
6
votes
1 answer

zippers: mapping over last breadcrumb

I've bumped into an issue using zippers and lens. Consider following example: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeOperators #-} import Control.Lens import Control.Zipper data A = AA { _aa :: A } | AB { _ab :: B } …
remdezx
  • 2,939
  • 28
  • 49
6
votes
1 answer

stepping into zipper with `to` lens

I'm struggling with lens and zippers. Consider below code run in ghci > import Control.Lens > import Control.Zipper > > :t within (ix 1) $ zipper ([1,2,3] :: [Int]) > within (ix 1) $ zipper ([1,2,3] :: [Int]) :: Control.Monad.MonadPlus m => m…
remdezx
  • 2,939
  • 28
  • 49
6
votes
0 answers

Idiomatic way to share lens field definitions among modules

If I have two modules that both use Control.Lens.TH' makeFields to generate fields from a record, and a record in each of the different modules has the same field name, what's the best way of ensuring that the two modules are using the same…
Fraser
  • 1,521
  • 2
  • 14
  • 23
6
votes
1 answer

Aeson and Lenses with error handling

I'm very new to the magic of lenses, so I'm having some trouble with this. With reference to: https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms a JSON object can be traversed in the following way: val ^? nth 0 . key "someObject" .…
iamnat
  • 4,056
  • 1
  • 23
  • 36
6
votes
1 answer

Implementing polymorphic 'deep' function for traversals and folds

I'm using lens together with xml-lens. I'd like to make the following function more polymorphic, so that it also works for Folds and not only Traversals: -- | Traverse a plated structure recursively, trying to match a fold at each level. Don't…
bennofs
  • 11,873
  • 1
  • 38
  • 62
6
votes
3 answers

Composing lenses with `at` and `ix`

Let's say I have some fairly simple data type Person with a couple of fields, and a type that holds a collection of Persons. data Person = Person { _name :: String, _age :: Int } data ProgramState = PS { _dict :: IntMap Person } makeLenses…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
6
votes
1 answer

How to zoom in acid-state?

data Foo = Foo { _bar :: Map String Integer } deriving (Eq, Ord, Read, Show, Data, Typeable) $(deriveSafeCopy 0 'base 'Foo) $(makeLenses ''Foo) Given the above code I am under the impression that it should be possible to do this: addEntry ::…
fho
  • 6,787
  • 26
  • 71
6
votes
1 answer

How to work around inability to use lenses with existential types?

I am using Edward Kmett's lens library for the first time, and finding it rather nice, but I ran into a snag... The question at [1] explains that existential quantifiers disrupt makeLenses. I really rather want to use an existential with lenses in…
Irene Knapp
  • 145
  • 8
6
votes
1 answer

How is anamorphism related to lens?

How is the Lens, the record accessor, e.g. http://hackage.haskell.org/packages/archive/lens/3.9.0.2/doc/html/Control-Lens-Type.html#t:Lens related to anamorphism?…
nushio
  • 753
  • 3
  • 12
5
votes
1 answer

How to get sublist's head using lens?

I have a value: my :: [(A, Either B [C])] and I want to get [(A, C)] from it using lens. The result items are items from my that: the second item in tuples is Right [C] the list in this Right [C] has at least 1 item, so the result get the first…
RandomB
  • 3,367
  • 19
  • 30
5
votes
2 answers

How to use lens to access a record field behind a sum type

I am trying to access a nested record using lenses and prisms in Haskell: import Data.Text (Text) import Control.Lens.TH data State = State { _stDone :: Bool , _stStep :: StateStep } data StateStep = StatePause | StateRun …
ruben.moor
  • 1,876
  • 15
  • 27
5
votes
1 answer

Is there a standard combinator packing the result of a lens in functor?

While trying to apply a function with multiple layers of functors to a member of a host data structure, as in the following example: update :: (SomeProductField -> Maybe (Int, SomeProductField)) -> (SomeProduct -> Maybe (Int, SomeProduct)) I…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
5
votes
1 answer

Returning ‘Nothing’ if an indexed list traversal doesn’t match

I’m new to lens and trying to use it to compose many small modifications to a nested structure, which may fail and possibly return additional results: element -> Maybe element element -> Maybe (result, element) How do I modify an inner structure by…
Jon Purdy
  • 53,300
  • 8
  • 96
  • 166