lens

Convenient ad-hoc lens Syntax on top of Monocle

License

License

GroupId

GroupId

ai.x
ArtifactId

ArtifactId

lens_2.12
Last Version

Last Version

2.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

lens
Convenient ad-hoc lens Syntax on top of Monocle
Project URL

Project URL

http://github.com/xdotai/lens
Project Organization

Project Organization

x.ai
Source Code Management

Source Code Management

https://github.com/xdotai/lens

Download lens_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/ai.x/lens_2.12/ -->
<dependency>
    <groupId>ai.x</groupId>
    <artifactId>lens_2.12</artifactId>
    <version>2.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/ai.x/lens_2.12/
implementation 'ai.x:lens_2.12:2.0.1'
// https://jarcasting.com/artifacts/ai.x/lens_2.12/
implementation ("ai.x:lens_2.12:2.0.1")
'ai.x:lens_2.12:jar:2.0.1'
<dependency org="ai.x" name="lens_2.12" rev="2.0.1">
  <artifact name="lens_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='ai.x', module='lens_2.12', version='2.0.1')
)
libraryDependencies += "ai.x" % "lens_2.12" % "2.0.1"
[ai.x/lens_2.12 "2.0.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.8
com.github.julien-truffaut : monocle-core_2.12 jar 1.5.0
com.github.julien-truffaut : monocle-generic_2.12 jar 1.5.0
com.github.julien-truffaut : monocle-macro_2.12 jar 1.5.0
org.scalactic : scalactic_2.12 jar 3.0.5

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.5

Project Modules

There are no modules declared in this project.

lens

Join the chat at https://gitter.im/xdotai/lens

User-friendly lens syntax for mortals. Update values inside of nested case class instances without .copy .

Based on Monocle. Also see https://github.com/julien-truffaut/Monocle/pull/208 and https://github.com/julien-truffaut/Monocle/pull/204 .

Use with caution. .copy and likewise .lens can make code hard to read. They are conceptually creating patched versions of existing objects. It can be hard for readers to reason about the patching, similar to how it is hard to reason about mutation. If you can write your code in a way that does not need patching objects, prefer that. If you can't, .lens is a nice alternative to .copy.

SBT Dependency

Scala 2.11

"ai.x" %% "lens" % "1.0.0"

Scala 2.12

"ai.x" %% "lens" % "2.0.0"

Usage

case class C( d: Int )
case class B( c: C )
case class A( b: B )
val a = A(B(C(3)))

Easy updates using .lens

import ai.x.lens.ImplicitBoundLens
A(B(C(5))) == a.lens(_.b.c.d).set( 5 )
A(B(C(6))) == a.lens(_.b.c.d).modify( _ + 3 )

Verbose updates using .copy

A(B(C(5))) == a.copy(
  b = a.b.copy(
    c = a.b.c.copy(
      d = 5
)))

A(B(C(6))) == a.copy(
  b = a.b.copy(
    c = a.b.c.copy(
      d = a.b.c.d + 3
)))

Related Work

ai.x.lens is very similar to quicklens, which implements more features to slightly reduce code size. But this also mean more learning and remembering. It's fair to make the trade-off in either direction. Check out quicklens if you want more features. Use ai.x.lens if you want something simpler.

ai.x

x.ai

x.ai is a personal assistant who schedule meetings for you

Versions

Version
2.0.1
2.0.0