Maverick sponsor LT

22
Lightning Talk by presented by Naoki Aoyama (@aoiroaoino) in ScalaMatsuri 2016

Transcript of Maverick sponsor LT

Page 1: Maverick sponsor LT

Lightning Talk by

presented by Naoki Aoyama (@aoiroaoino)

in ScalaMatsuri 2016

Page 2: Maverick sponsor LT

> whoami

• Naoki Aoyama

• Twitter: @AoiroAoino

• GitHub: @aoiroaoino

• Scala Exp: about 4 years

• committer

Page 3: Maverick sponsor LT

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/ より

Page 4: Maverick sponsor LT

Product: FSS

Web管理画面から 紙広告を配信!

Page 5: Maverick sponsor LT
Page 6: Maverick sponsor LT

try {

Tour of Lens in 3 minutes

Page 7: Maverick sponsor LT

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)

Page 8: Maverick sponsor LT

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

Page 9: Maverick sponsor LT

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

Page 10: Maverick sponsor LT

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

Page 11: Maverick sponsor LT

_人人人人_ > Lens < ‾Y^Y^Y‾

Page 12: Maverick sponsor LT

What’s the Lens?

• something like a getter/setter such as Java

• functional reference

Page 13: Maverick sponsor LT

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)

Page 14: Maverick sponsor LT

What’s the Lens?

case class Lens[S, A](get: S => A, set: A => S => S)

Page 15: Maverick sponsor LT

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

Page 16: Maverick sponsor LT

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)

Page 17: Maverick sponsor LT

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.

Page 18: Maverick sponsor LT

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

Page 19: Maverick sponsor LT

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

Page 20: Maverick sponsor LT

(_bbb compose _ccc compose _ddd compose _eee compose _fff compose _ggg).set( )(aaa)

Lens for the win!!

Page 21: Maverick sponsor LT

Example in Maverick

• We use Monocle in test codes.

Page 22: Maverick sponsor LT

} finally {

–Naoki Aoyama

“Lens is Awesome!”

}