Questions tagged [lenses]

In functional programming, a lens is a composable field accessor. Lenses allow nested data structures to be manipulated in a concise and side-effect-free way.

In functional programming, a lens is a composable field accessor. Lenses allow nested data structures to be manipulated in a concise and side-effect-free way.

Lenses define a set function and a get function. Given a value a of type A and a value b of type B, set b a returns a new value a' of type A with some field inside set to b. Given a value a of type A, get a returns the value b of type B contained in a.

To constitute a lens, get and set must follow a few straightforward laws:

  1. Given a and b, get (set b a) = b.

  2. Given a, b, and b', get (set b' (set b a)) = b'.

  3. Given a and b, set (get a) a = a.

Because lenses follow these laws, they are safe to compose. This makes them suitable for manipulating nested data structures in a concise way.

269 questions
10
votes
1 answer

How do I combine lenses and functors?

I'm trying to get used to the lens library for Haskell, and find myself struggling at some simple problems. For instance, let's say (for convenience) that at and _1 have the following types (this is how I understand them, at least): at :: Ord k => k…
wen
  • 3,782
  • 9
  • 34
  • 54
10
votes
3 answers

Filtering composite structures with Lens

I have a [(a, Maybe b)], and want to obtain a [(a, b)], with all pairs where the second element was Nothing filtered out. Is there a concise way to describe this operation using lens?
Narvius
  • 180
  • 7
10
votes
2 answers

Why would my datatype need to an instance of Monoid to use this lens?

I'm using the code below on a record that has a field '_scene' of type SceneGraph. I've created lenses for it using makeLenses. inputGame :: Input -> Game -> Game inputGame i g = flip execState g $ do let es = g ^. userInput . events sg…
schellsan
  • 2,164
  • 1
  • 22
  • 32
10
votes
2 answers

How does Haskell's lens package handle fields that are also keywords?

How does lens handle the case where a de-sugared field is a keyword? I seem to remember reading that something special is done, but I can't remember where I read it or what the name of the "lensed" accessor would end up as. Consider the…
Ben Ford
  • 2,087
  • 21
  • 26
10
votes
3 answers

Data.Lens or Control.Lens

Possible Duplicate: lenses, fclabels, data-accessor - which library for structure access and mutation is better I'm going to use and learn a Lens package on my next Haskell project. I had almost decided on the Data.Lens package when I found this…
MFlamer
  • 2,340
  • 2
  • 18
  • 25
10
votes
2 answers

Scalaz Lens Composition

Really simple question here. After watching an excellent introduction to lenses: http://www.youtube.com/watch?v=efv0SQNde5Q I thought I might attempt one of the simple examples covered in the talk: import…
9
votes
1 answer

Is there a van Laarhoven optic based on the Monad typeclass?

As I understand it, each van Laarhoven optic type can be defined by a constraint on a type constructor: type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s ->…
9
votes
1 answer

What's the reason behind the name Market in Control.Lens?

Edward Kmett's optics library; Control.Lens defines a large number of types. Most of these have relatively self explanatory names, like Traversal and Fold. It also defines some types with less obvious names, like Bazaar From the Bazaar…
Joe
  • 1,479
  • 13
  • 22
9
votes
3 answers

How to find and modify field in nested case classes?

Defined some nested case classes with List fields: @Lenses("_") case class Version(version: Int, content: String) @Lenses("_") case class Doc(path: String, versions: List[Version]) @Lenses("_") case class Project(name: String, docs:…
Freewind
  • 193,756
  • 157
  • 432
  • 708
9
votes
1 answer

indexing list with Control.Lens requires Monoid constraint

The following code doesn't compile: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data MyType = MyType Int data Outer = Outer { _inners :: [ Inner ] } data Inner = Inner { _val :: MyType } $(makeLenses ''Outer) $(makeLenses ''Inner) i1 =…
ajp
  • 1,723
  • 14
  • 22
9
votes
2 answers

Composing partial getters using the lens library

I am using the lens package and and keep thinking there must be an easy solution to the following problem. Say I have some map (or any At instance) and a lens on its value type, ie aMap :: Map Int a aLens :: Simple Lens a b I want a getter g ::…
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
9
votes
4 answers

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldables as well on ByteStrings and Texts. Generalizing it to Foldable is simple. But how to include…
Petr
  • 62,528
  • 13
  • 153
  • 317
8
votes
0 answers

Is there a Lens/Optic that can use to dig deeply into multidimensional maps?

I've had a bit of difficulty using Lenses with Maps. I have maps that look like this Map String (Map String Int). These are multidimensional arrays, and I usually set them up with known dimensions. I often want to read and update elements of these…
8
votes
2 answers

Why doesn't a prism set function return an Option/Maybe

In functional optics, a well-behaved prism (called a partial lens in scala, I believe) is supposed to have a set function of type 'subpart -> 'parent -> 'parent, where if the prism "succeeds" and is structurally compatible with the 'parent argument…
Nathan Wilson
  • 629
  • 6
  • 11
8
votes
1 answer

remove elements from a List of case class structure efficiently and elegantly

I have a nested case classes structure in a List for simplicity will use following as an example - case class Address(street: String, city: String, state: String, zipCode: Int) case class Person(firstName: String, lastName: String, addresses:…
Vikas Pandya
  • 1,998
  • 1
  • 15
  • 32
1 2
3
17 18