Lenses: A Functional Imperative [1/5]
Why Stack Overflow recommends it
Stack Overflow users link it as the standard introduction to lenses when answering questions about updating nested immutable data in Scala and Haskell.
Recommended by 8 people across 7 different months, 2011 to 2013.
What it is
Part 1 of a five-part recording of Edward Kmett's 2011 talk at the Boston Area Scala Enthusiasts meetup. It introduces lenses as composable, purely functional references and shows how they can recreate imperative conventions such as field update in an immutable setting. Examples are in Scala, using Scalaz.
What people said
Recommends paradigmatic · 2011-07 · Stack Overflow · on “Using Scala Case Classes as De-facto Maps”
Using a case class is less flexible than a map, since you can only assign/access pre-defined fields. You will need to build a full case class hierarchy beforehand. On the other hand, case class offer kind of "compile-time validations", because all the types are explicitly defined (in contrast to a Map[String,Any] ), and you cannot assign/access a non specified field by mistake. Case classes should also be faster, because you don't need to traverse the map hashtable to find what you are looking for. The "verbosity" problem comes from the immutable aspect of case classes, but you'll have exactly the same problem with immutable maps. The solution seems to be Lenses . There is a very nice talk h
Recommends hammar · 2011-11 · Stack Overflow · on “Functional lenses”
There is a nice introduction to lenses [this video] by Edward Kmett on YouTube. The examples are in Scala, but it shouldn't be too hard to follow.
Recommends aleator · 2011-12 · Stack Overflow · on “update list enclosed in tuple”
Have you considered using lenses [this video] ? If your purpose is to make updating the tuple easy and transparent for the programmer, they fit the bill very nicely. The following uses the excellent fclabels package: import Data.Label import Control.Category import Prelude hiding ((.)) test = ("192.168.1.1", 3455, (1234566, msgs)) msgs = ["aaa", "bbbb", "ccccc"] append x = modify messages (x :) main = do let test' = append "first" test print test' print (get messages test') second2 = lens (\(a,b) -> b) (\b (a,_) -> (a,b)) third3 = lens (\(a,b,c) -> c) (\c (a,b,_) -> (a,b,c)) messages = second2 . third3 The second2 and third3 can either be generic functions that you define once for all the tuples
Recommends Dan Burton · 2012-05 · Stack Overflow · on “What are lenses used/useful for?”
You might enjoy watching Edward Kmett's Lenses: A Functional Imperative [this video] talk. It's presented in Scala, but the translation to the usefulness of lenses in Haskell should be clear.
Recommends billymillions · 2012-08 · Stack Overflow · on “Scalaz Lens Composition”
Really simple question here. After watching an excellent introduction to lenses: [this video] I thought I might attempt one of the simple examples covered in the talk: import scalaz.Lens._ fst.andThen(snd).set(((1,2),3),9) this was followed by this error error: type mismatch; found : scalaz.Lens[(Nothing, Nothing),Nothing] required: scalaz.Lens[(Nothing, Nothing),C] Note: Nothing <: C, but class Lens is invariant in type B. You may wish to define B as +B instead. (SLS 4.5) fst.andThen(snd).set(((1,2),3)) ^ Any ideas as to how to make this work?
Recommends ron · 2012-09 · Stack Overflow · on “Finding my way through Scalaz”
Some of the videos I found useful: Scalaz overview by Nick Partridge [LINK] and by Jason Zaugg [LINK] Practical Scalaz by Chris Marshall [LINK] Lenses by Edward Kmett [this video] (5 parts) Reader monad by Tony Morris [LINK] and by Rúnar Bjarnason [LINK] State monad by Michael Pilquist [LINK] Monad transformers by Jordan West [LINK] Most of these have great slides, if you are hardcore then read them without the video. Also learn [LINK] to read Haskell [LINK] type signatures and browse the Haskell typeclassopedia [LINK] .
Recommends Henry Story · 2012-09 · Stack Overflow · on “Is it possible to elude the Scala's messy ways of handling tuples as input parameters with Scalaz?”
If you want to use scalaz to do this, then you are probably looking to do this with Lenses. See the course Learning scalaz: Day 11 [LINK] , and also the lecture by Edward Kmett Lenses a Functional Imperative [this video] captured on video. On page 42 of the pdf slides [LINK] Edward explains the use of Lenses with Maps to change them purely functionally.
Recommends ron · 2012-09 · Stack Overflow · on “Good scalaz introduction”
Videos: Scalaz overview by Nick Partridge [LINK] and by Jason Zaugg [LINK] Practical Scalaz by Chris Marshall [LINK] Lenses by Edward Kmett [this video] (5 parts) Reader monad by Tony Morris [LINK] and by Rúnar Bjarnason [LINK] State monad by Michael Pilquist [LINK] Monad transformers by Jordan West [LINK] (Copied from my answer of a closed duplicate).
Recommends Raahul Kumar · 2013-04 · Stack Overflow · on “example uses scalaz.Lens's modf, modp and xmap”
Unfortunately, Scalaz7 lens examples are a WIP. You need to ask this question of the Scalaz Google Group [LINK] . Before you ask, try these examples here and watch Emmett's videos. Using lenses with Scalaz 7 [LINK] Emmett's videos on Lenses [this video] Look at the source code again. What can you puzzle out from this? def xmapbA[X, A >: A2 <: A1](b: Bijection[A, X]): LensFamily[X, X, B1, B2] = xmapA(b to _)(b from _) def xmapB[X1, X2](f: B1 => X1)(g: X2 => B2): LensFamily[A1, A2, X1, X2] = lensFamily(a => run(a).xmap(f)(g)) def xmapbB[X, B >: B1 <: B2](b: Bijection[B, X]): LensFamily[A1, A2, X, X] = xmapB(b to _)(b from _) /** Modify the value viewed through the lens, returning a functor `X` full
Cites Edward Kmett · 2011-06 · Stack Overflow · on “Class set method in Haskell using State-Monad”
It takes a bit of permuting to transform imperative code into a purely functional context. A setter mutates an object. We're not allowed to do that directly in Haskell because of laziness and purity. Perhaps, if we transcribe the State monad to another language it'll be more apparent. Your code is in C++, but because I at least want garbage collection I'll use java here. Since Java never got around to defining anonymous functions, first, we'll define an interface for pure functions. public interface Function<A,B> { B apply(A a); } Then we can make up a pure immutable pair type. public final class Pair<A,B> { private final A a; private final B b; public Pair(A a, B b) { this.a = a; this.b = b