maverick sponsor lt

Download Maverick sponsor LT

Post on 26-Jan-2017

1.092 views

Category:

Engineering

0 download

Embed Size (px)

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/

    http://jp.yamaha.com/products/network/downloads/tools/

  • Product: FSS

    Web

    https://www.mvrck.co.jp/news/archives/news_20150522_1.html

  • 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

  • Whats the Lens

    something like a getter/setter such as Java

    functional reference

  • Whats the Lens

    getter: S => A

    e.g. player.name // => "aoino"

    setter: A => S => S

    e.g. player.copy(age = 26) // => Player("aoino",26)

  • Whats the Lens

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

  • Lets 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.

  • Lets 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!

    }