SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
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'swe!
DSP SSP Media
広
告
出
稿
広
告
閲
覧
1. 広告リクエスト
2. bid request
3. 入札判断
4. bid response
5. 落札通知
6. 広告配信
afewsecs
100 ms or die!
※画像は http://jp.yamaha.com/products/network/downloads/tools/ より
Product: FSS
Web管理画面から
紙広告を配信!
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 structure
case 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 Lenses
case 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!
}

Contenu connexe

Similaire à Maverick sponsor LT

Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdfWrite a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
rozakashif85
 

Similaire à Maverick sponsor LT (20)

Tame cloud complexity with F# powered DSLs (build stuff)
Tame cloud complexity with F# powered DSLs (build stuff)Tame cloud complexity with F# powered DSLs (build stuff)
Tame cloud complexity with F# powered DSLs (build stuff)
 
Baseball Prediction Model on Tensorflow
Baseball Prediction Model on TensorflowBaseball Prediction Model on Tensorflow
Baseball Prediction Model on Tensorflow
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30
 
Tips and Tricks for Avoiding Common Query Pitfalls
Tips and Tricks for Avoiding Common Query PitfallsTips and Tricks for Avoiding Common Query Pitfalls
Tips and Tricks for Avoiding Common Query Pitfalls
 
A look at the CQL changes in 3.x (Benjamin Lerer, Datastax) | Cassandra Summi...
A look at the CQL changes in 3.x (Benjamin Lerer, Datastax) | Cassandra Summi...A look at the CQL changes in 3.x (Benjamin Lerer, Datastax) | Cassandra Summi...
A look at the CQL changes in 3.x (Benjamin Lerer, Datastax) | Cassandra Summi...
 
A look at the cql changes in 3.x
A look at the cql changes in 3.xA look at the cql changes in 3.x
A look at the cql changes in 3.x
 
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdfWrite a class (BasketballTeam) encapsulating the concept of a tea.pdf
Write a class (BasketballTeam) encapsulating the concept of a tea.pdf
 
Ropossum: A Game That Generates Itself
Ropossum: A Game That Generates ItselfRopossum: A Game That Generates Itself
Ropossum: A Game That Generates Itself
 
The Ring programming language version 1.5.4 book - Part 53 of 185
The Ring programming language version 1.5.4 book - Part 53 of 185The Ring programming language version 1.5.4 book - Part 53 of 185
The Ring programming language version 1.5.4 book - Part 53 of 185
 
The Ring programming language version 1.5.3 book - Part 57 of 184
The Ring programming language version 1.5.3 book - Part 57 of 184The Ring programming language version 1.5.3 book - Part 57 of 184
The Ring programming language version 1.5.3 book - Part 57 of 184
 
The Ring programming language version 1.5.3 book - Part 47 of 184
The Ring programming language version 1.5.3 book - Part 47 of 184The Ring programming language version 1.5.3 book - Part 47 of 184
The Ring programming language version 1.5.3 book - Part 47 of 184
 
League of Graphs
League of GraphsLeague of Graphs
League of Graphs
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88
 
Chainer ui v0.3 and imagereport
Chainer ui v0.3 and imagereportChainer ui v0.3 and imagereport
Chainer ui v0.3 and imagereport
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actors
 
03 objects
03 objects03 objects
03 objects
 
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal Editor
 
Chapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdfChapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdf
 
360|iDev
360|iDev360|iDev
360|iDev
 

Dernier

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Dernier (20)

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Maverick sponsor LT

  • 1. Lightning Talk by presented by Naoki Aoyama (@aoiroaoino) in ScalaMatsuri 2016
  • 2. > whoami • Naoki Aoyama • Twitter: @AoiroAoino • GitHub: @aoiroaoino • Scala Exp: about 4 years • committer
  • 3. Product: 広告主 Audience It'swe! DSP SSP Media 広 告 出 稿 広 告 閲 覧 1. 広告リクエスト 2. bid request 3. 入札判断 4. bid response 5. 落札通知 6. 広告配信 afewsecs 100 ms or die! ※画像は http://jp.yamaha.com/products/network/downloads/tools/ より
  • 5.
  • 6. try { Tour of Lens
 in 3 minutes
  • 7. 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)
  • 8. Nested data structure case 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))
  • 9. 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 = ) ) ) ) ) )
  • 10. 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
  • 12. What s the Lens? • something like a getter/setter such as Java • functional reference
  • 13. 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)
  • 14. What s the Lens? case class Lens[S, A](get: S => A, set: A => S => S)
  • 15. Let s define some Lenses case 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) )
  • 16. 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)
  • 17. 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.
  • 18. 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) ) }
  • 19. 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))
  • 20. (_bbb compose _ccc compose _ddd compose _eee compose _fff compose _ggg).set( )(aaa) Lens for the win!!
  • 21. Example in Maverick • We use Monocle in test codes.
  • 22. } finally { –Naoki Aoyama Lens is Awesome! }