Lens is an abstraction from functional programming which helps to deal with a problem of updating complex immutable nested objects. This talk contains different usage of Lenses like QuickLens, Sauron, Scalaz.Lens.
4. What is Lens ?What is Lens ?
A Lens is an abstraction from functional programming
which helps to deal with a problem of updating complex
immutable nested objects.
Let's start with an example.
case class Turtle(
xcor: Double,
ycor: Double,
heading: Double)
5. If we want to mutate the value of the heading then.
In the Old Scala Days(2.7):
If we want to mutate the value of the heading then.
In the Old Scala Days(2.7):
case class Turtle(...) {
def right(...): Turtle =
Turtle(xcor,ycor,heading + delta)}
Scala 2.8 comes to the rescue:
case class Turtle(...) {
def right(...): Turtle =
copy(heading = heading + delta)
15. n levels deep
// imperative
a.b.c.d.e += 1
// functional
a.copy(
b = a.b.copy(
c = a.b.c.copy(
d = a.b.c.d.copy(
e = a.b.c.d.e +
1))))
What if we go N levels deep:
16. case class Program(
...
breeds: ListMap[String, Breed] =
ListMap(),
linkBreeds: ListMap[String, Breed] =
ListMap(),
...
)
Production Code:
17. Production Code:
// if we had lenses this wouldn't get so repetitious
// - ST 7/15/12
if (isLinkBreed)
program.copy(linkBreeds =
orderPreservingUpdate(
program.linkBreeds,
program.linkBreeds(breedName).copy(
owns = newOwns)))
else
program.copy(breeds =
orderPreservingUpdate(
program.breeds,
program.breeds(breedName).copy(
owns = newOwns)))
https://github.com/NetLogo/NetLogo/blob/15700ba6bcab813c5be8b49b8fa00c2204883b08/
headless/src/main/org/nlogo/compiler/StructureParser.scala#L266-L276
18. We Can Fix it:
omit needless repetition!
avoid nested copy()
19. The need of the lens arises when there is too much nesting.
20. If we want to increase userRating in this model then we
will have to write such a code:
If we want to increase userRating in this model then we
will have to write such a code:
And we have to write the code below to confirm all of the
addresses in BillInfo.
And we have to write the code below to confirm all of the
addresses in BillInfo.
If we want to increase userRating in this model then we
will have to write such a code:
If we want to increase userRating in this model then we
will have to write such a code:
21. If we increase a level of nesting in our structures then we
will considerably increase amount of a code like this. In
such cases lens give a cleaner way to make changes in
nested structures.
Using <<quicklens>> we can do it much simpler:
If we increase a level of nesting in our structures then we
will considerably increase amount of a code like this. In
such cases lens give a cleaner way to make changes in
nested structures.
Using <<quicklens>> we can do it much simpler:
23. There are several implementations in scala:
scalaz.Lens
Quicklens
Sauron
There are several implementations in scala:
scalaz.Lens
Quicklens
Sauron
Available Implementations
24. If we want to use scalaz.Lens at first we should
define lens:
If we want to use scalaz.Lens at first we should
define lens:
scalaz.Lens:
The first type parameter is needed to set in which class(MainClass)
we will change value and the second type parameter defines the
class(FieldClass) of the field which we will change with the lens.
We should also send two functions to lensu(...) method. The first
function defines how to change MainClass using a new value. The
second function is used to get value of the field which we want to
change.
25. Quicklens:
Use of scalaz.Lens is quite difficult.It is quite hard and we will
reduce amount of the code only if we have very complex
nesting and implement enough lens to compose them.
Quicklens has support of chain modifications which can be
helpful if you want to change several fields at the same time
26. It is also possible to create reusable lens as well as
in scalaz.Lens
It is also possible to create reusable lens as well as
in scalaz.Lens
QuickLens:
27. QuickLens:
And the lens composition is also possible:And the lens composition is also possible:
28. Sauron:Sauron:
It is said on the main page of Sauron repo it has been inspired
by quicklens but it has much simpler implementation and less
number of features. And also has additional dependency on
"org.scalamacros" % "paradise" % "2.1.0-M5"
31. Results:
➔ scalaz.Lens - If you already have scalaz in a project
and you are not bothered to write some code in order
to define lens
➔ Quicklens - Easy to use and powerful enough to deal
with the described problem
➔ Sauron - Very similar to Quicklens and has a less size
but also has less functionality
many web application want to increase application throughput, responsivenesswhere one task can make progress without waiting for all others to completewhere more than one task can make progress at same time.Concurrent program can be executed on single core machine via time slicYou may execute concurrent program in parallelOverall you play with threads