SlideShare a Scribd company logo
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!
}

More Related Content

Similar to Maverick sponsor LT

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)
Yan Cui
 
Baseball Prediction Model on Tensorflow
Baseball Prediction Model on TensorflowBaseball Prediction Model on Tensorflow
Baseball Prediction Model on Tensorflow
Jay Ryu
 
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
Mahmoud Samir Fayed
 
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
Mahmoud Samir Fayed
 
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
MongoDB
 
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
Benjamin Lerer
 
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...
DataStax
 
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
 
Ropossum: A Game That Generates Itself
Ropossum: A Game That Generates ItselfRopossum: A Game That Generates Itself
Ropossum: A Game That Generates Itself
Mohammad Shaker
 
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
Mahmoud Samir Fayed
 
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
Mahmoud Samir Fayed
 
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
Mahmoud Samir Fayed
 
League of Graphs
League of GraphsLeague of Graphs
League of Graphs
Dmitry Vrublevsky
 
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
Mahmoud Samir Fayed
 
Chainer ui v0.3 and imagereport
Chainer ui v0.3 and imagereportChainer ui v0.3 and imagereport
Chainer ui v0.3 and imagereport
Preferred Networks
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actors
zucaritask
 
03 objects
03 objects03 objects
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal Editor
Nick Pruehs
 
Chapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdfChapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdf
MekiyaShigute1
 
360|iDev
360|iDev360|iDev
360|iDev
Aijaz Ansari
 

Similar to 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
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
 
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...
 
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
 

Recently uploaded

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
mahaffeycheryld
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 

Recently uploaded (20)

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 

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