maverick sponsor lt
TRANSCRIPT
Lightning Talk by
presented by Naoki Aoyama (@aoiroaoino)
in ScalaMatsuri 2016
> whoami
• Naoki Aoyama
• Twitter: @AoiroAoino
• GitHub: @aoiroaoino
• Scala Exp: about 4 years
• committer
Product:
広告主
Audience
It's w
e!
DSP SSP Media
広告出稿 広告閲覧
1. 広告リクエスト
2. bid request
3. 入札判断
4. bid response
5. 落札通知
6. 広告配信
a few secs
100 ms or die!
※画像は http://jp.yamaha.com/products/network/downloads/tools/ より
try {
Tour of Lens in 3 minutes
Simple data structure
case class Player(name: String, age: Int)
val player = Player("aoino", 25)
scala> player.name res0: String = aoino
scala> player.copy(age = 26) res1: Player = Player(aoino,26)
Nested data structurecase class Game(stageId: Int, player: Player)
val game = Game(999, player)
// get scala> game.player.name res6: String = aoino
// set scala> game.copy( | player = game.player.copy( | name = "Aoyama" | ) | ) res7: Game = Game(999,Player(Aoyama,25))
Copy method hell ...
aaa.copy ( bbb = aaa.bbb.copy ( ccc = aaa.bbb.ccc.copy ( ddd = aaa.bbb.ccc.ddd.copy ( eee = aaa.bbb.ccc.ddd.eee.copy ( fff = aaa.bbb.ccc.ddd.eee.fff.copy ( ggg = ) ) ) ) ) )
Motivation
• We need the simple syntax like updating mutable objects -> want to avoid the copy method hell
• We always want a "composability" and "reusability" -> compose of a getter/settter defined in the class is difficult
_人人人人_ > Lens < ‾Y^Y^Y‾
What’s the Lens?
• something like a getter/setter such as Java
• functional reference
What’s the Lens?
getter: S => A
e.g. player.name // => "aoino"
setter: A => S => S
e.g. player.copy(age = 26) // => Player("aoino",26)
What’s the Lens?
case class Lens[S, A](get: S => A, set: A => S => S)
Let’s define some Lensescase class Lens[S, A](get: S => A, set: A => S => S)
case class Player(name: String, age: Int)
val _name: Lens[Player, String] = Lens( _.name, str => player => player.copy(name = str) )
val _age: Lens[Player, Int] = Lens( _.age, num => player => player.copy(age = num) )
Give it a try
scala> _name.get(player) res2: String = aoino
scala> _name.set("Aoyama")(player) res3: Player = Player(Aoyama,25)
scala> _age.get(player) res4: Int = 25
scala> _age.set(26)(player) res5: Player = Player(aoino,26)
We need `composability`
// in Function f: A => B g: B => C
g compose f : A => C
// in Lens sa: Lens[S, A] ab: Lens[A, B]
sa compose ab : Lens[S, B]
Note: Typically, Lenses `compose` is reverse compared to function.
Let’s define `compose` method
case class Lens[S, A](get: S => A, set: A => S => S){
def compose[B](other: Lens[A, B]): Lens[S, B] = Lens( s => other.get(this.get(s)), b => s => this.set( other.set(b)(this.get(s)) )(s) ) }
Give it a try
val _player: Lens[Game, Player] = ...
scala> (_player compose _name).get(game) res8: String = aoino
scala> (_player compose _name).set("Aoyama")(game) res9: Game = Game(999,Player(Aoyama,25))
(_bbb compose _ccc compose _ddd compose _eee compose _fff compose _ggg).set( )(aaa)
Lens for the win!!
Example in Maverick
• We use Monocle in test codes.
} finally {
–Naoki Aoyama
“Lens is Awesome!”
}