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
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

Lens package with algebraic types

I was coding with with the lens package. Everything was going fine until I tried to access a certain field on an algebraic type: import Control.Lens data Type = A { _a :: Char } | B makeLenses ''Type test1 = _a (A 'a') test2 = (A 'a') ^. a No…
David McHealy
  • 2,471
  • 18
  • 34
14
votes
2 answers

Avoiding repetition using lenses whilst deep-copying into Map values

I have an immutable data structure where I have nested values in Maps, like so: case class TradingDay(syms: Map[String, SymDay] = Map.empty) case class SymDay(sym: String, traders: Map[String, TraderSymDay] = Map.empty) case class…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
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
2 answers

Why do we need Control.Lens.Reified?

Why do we need Control.Lens.Reified? Is there some reason I can't place a Lens directly into a container? What does reify mean anyway?
massysett
  • 1,100
  • 6
  • 13
13
votes
3 answers

What's the difference between a lens and a partial lens?

A "lens" and a "partial lens" seem rather similar in name and in concept. How do they differ? In what circumstances do I need to use one or the other? Tagging Scala and Haskell, but I'd welcome explanations related to any functional language that…
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
13
votes
3 answers

Merge two case classes in Scala, but with deeply nested types, without lens boilerplate

Similar to this case class question but with a twist: I have a case class which has some deeply nested case classes as properties. As a simple example, case class Foo(fooPropA:Option[String], fooPropB:Option[Int]) case class Bar(barPropA:String,…
mhamrah
  • 9,038
  • 4
  • 24
  • 22
13
votes
2 answers

How can I use Control.Lens to update the ith element of a list?

I have some datatypes along the line of data Outer = Outer { _list :: [ Inner ] } data Inner = Inner { _bool :: Bool } using Control.Lens, I can access the _bool of the ith Inner (inside a 'State Outer' monad) like this boolValue <- gets (^. list .…
ajp
  • 1,723
  • 14
  • 22
12
votes
3 answers

Haskell lenses: how to make view play nicely with traverse?

I am trying to learn about lenses by implementing it in Haskell. I have implemented the view combinator as follows: {-# LANGUAGE RankNTypes #-} import Control.Applicative import Data.Traversable type Lens s a = Functor f => (a -> f a) -> s -> f…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
11
votes
1 answer

How Haskell's "composing lenses using function composition" with that weird order of arguments could be implemented?

I've been reading A wreq tutorial: A lens provides a way to focus on a portion of a Haskell value. For example, the Response type has a responseStatus lens, which focuses on the status information returned by the server. ghci> r ^.…
11
votes
1 answer

Shapeless: generic lens parameterized by case class or field

Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to…
ponythewhite
  • 617
  • 3
  • 8
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
10
votes
2 answers

Filtering Lists in Scala's Monocle

Given the following code: case class Person(name :String) case class Group(group :List[Person]) val personLens = GenLens[Person] val groupLens = GenLens[Group] how can i "filter" out certain Persons from the selection, NOT by index but by a…
Lazarus535
  • 1,158
  • 1
  • 8
  • 23
1
2
3
17 18