Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scala & Play Framework 勉強会
Search
spring_raining
November 22, 2014
Programming
0
400
Scala & Play Framework 勉強会
2014年11月22日 OUCC Scala & Play Framework 勉強会のスライドです
author: @spring_raining
spring_raining
November 22, 2014
Tweet
Share
More Decks by spring_raining
See All by spring_raining
CSS Variable をもっと活用する / Kyoto.js 18
spring_raining
4
1.7k
Vivliostyle CLIで広がるCSS組版のエコシステム / Vivliostyle user & developer meetup 2020 autumn
spring_raining
0
2.5k
Markdownの複雑化と締め切りのはざまで / Vivliostyle meetup
spring_raining
0
890
JavaScriptでもディープラーニングってやつでなんとかして / Kyoto.js 15
spring_raining
2
2.1k
CSS組版の救世主 Vivliostyle / HTML5 Conference 2018
spring_raining
6
7.9k
印刷・出版のためのオンラインエディタ Viola / Viola - Online editor for printing and publishing
spring_raining
3
670
Printable Web
spring_raining
3
890
今こそCSS組版
spring_raining
5
2.4k
シン・サーバの形は。
spring_raining
0
310
Other Decks in Programming
See All in Programming
良いユニットテストを書こう
mototakatsu
8
3k
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
150
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
650
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
790
Zoneless Testing
rainerhahnekamp
0
120
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
300
快速入門可觀測性
blueswen
0
400
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
170
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
230
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
8
1.8k
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Git: the NoSQL Database
bkeepers
PRO
427
64k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Producing Creativity
orderedlist
PRO
342
39k
Become a Pro
speakerdeck
PRO
26
5k
Speed Design
sergeychernyshev
25
670
The World Runs on Bad Software
bkeepers
PRO
66
11k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
The Invisible Side of Design
smashingmag
298
50k
Transcript
OUCC Scala × Play Framework Hands-on @spring_raining
એ ҰॹʹWebΞϓϦ࡞Ζ͏ʂʂʂʂʂ̍ʂʂ →→→Reply to @spring_raining
એ OUCC Advent Calendarॻ͖·ͤΜ͔ ʂʂʂʂʂʁʁʁʂʂʂʂʁʁ →http://www.adventar.org/calendars/449
Scalaͱ • Javaͱಉ͡ڥͰಈ͘ • ؆ܿͳදه • ؔܕmeetsΦϒδΣΫτࢦ
Play Frameworkͱ • Ruby on Rails / Django like •
ͨ͘͞Μͷಋೖࣄྫ • ˒5,405
GETTING STARTED
ʮJava SEʯͰݕࡧݕࡧ ʮJDKʯΛΠϯετʔϧ
Θ͔Γʹ͍͘ ʮAccept License AgreementʯΛΫϦοΫ
typesafe.com Typesafe Activator Λμϯϩʔυ & Πϯετʔϧ
IDE͓ΈͰ IntelliJ IDEAΛఆͯ͠ਐΊ·͢
μϯϩʔυ͕ऴΘΔ·Ͱ… Scalaͷجຊߏจ
SYNTAX
ม var hoge: Int = 10 var fuga = "100"
println(hoge + fuga) hoge = 20 fuga = 200
ม var hoge: Int = 10 var fuga = "100"
println(hoge + fuga) hoge = 20 fuga = 200 ! var ໊લ = var ໊લ: ܕ = ܕࣗಈͰਪଌ͞ΕΔ
ม var hoge: Int = 10 var fuga = "100"
println(hoge + fuga) hoge = 20 fuga = 200 →10110
ม var hoge: Int = 10 var fuga = "100"
println(hoge + fuga) hoge = 20 fuga = 200 →ίϯύΠϧΤϥʔ fugaStringܕͳͷͰ IntܕೖͰ͖ͳ͍
ఆ val teisuu = "konnitiwa" println(teisuu) val ໊લ =
val ໊લ : ܕ = ఆ࠶ೖͰ͖ͳ͍
ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) Array(ॳظ..)
new Array(ྻ) new Array[ܕ](ྻ)
ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) ྻͷཁૉ[
]Ͱͳ͘ ( )Ͱऔಘ
if var foo: String = "" foo = if (1
== 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if (݅ࣜ) ࣜ if (݅ࣜ) ࣜ else ࣜ if (݅ࣜ) { ࣜ.. } if (݅ࣜ) { ࣜ.. } else { ࣜ.. }
if var foo: String = "" foo = if (1
== 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) ifʮࣜʯͳͷͰΛฦ͢ ฦ͞Εͨ มʹೖ͞ΕΔ
if var foo: String = "" foo = if (1
== 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if͕Λฦ͞ͳ͍߹… Unitͱ͍͏ΫϥεΛฦ͢ (voidΈ͍ͨͳͷ)
for (1) val cast = Array("moffle", "macaron", "tiramy") for (i
<- cast) { println(i) } for (ม໊ <- ίϨΫγϣϯ) { ࣜ.. }
for (1) val cast = Array("moffle", "macaron", "tiramy") for (i
<- cast) { println(i) } ίϨΫγϣϯ͔ΒཁૉΛ 1ͭͣͭऔΓग़࣮ͯ͠ߦ (foreachΈ͍ͨͳͭ)
for (2) for (i <- 0 until 5) { println(i)
} Scalaʹ͍ΘΏΔ for (ʙ ; ʙ ; ʙ) ͑ͳ͍
for (2) for (i <- 0 until 5) { println(i)
} until Ұ୴RangeΫϥεͰͷ ίϨΫγϣϯΛ࡞Γ ͔ͦ͜ΒΛऔΓग़͢
for (3) 0 until 5 ! 0 to 5 !
0 to 5 by 2 ! 5 to 0 by -1 →Range(0, 1, 2, 3, 4) →Range(0, 1, 2, 3, 4, 5) →Range(0, 2, 4) →Range(5, 4, 3, 2, 1, 0)
while var i = 0 var summer = "" while
(i < 10) { summer += "hiji" i += 1 } println(summer) while (݅ࣜ) ࣜ while (݅ࣜ) { ࣜ.. }
while var i = 0 var summer = "" while
(i < 10) { summer += "hiji" i += 1 } println(summer) breakcontinueʁ
match (1) val somebody = "miyako" somebody match { case
"yuno" => println(144.3) case "miyako" => println(165) case "nori" | "nazuna" => println("?") case _ => println("unknown") } ม match { case ύλʔϯ => ॲཧ } match =switchͷ͍ͭ͢͝
match (1) val somebody = "miyako" somebody match { case
"yuno" => println(144.3) case "miyako" => println(165) case "nori" | "nazuna" => println("?") case _ => println("unknown") } →default →nori ·ͨnazuna
match (2) val animal = ("tanuki", "itachi", "araiguma") animal match
{ case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) }
match (2) val animal = ("tanuki", "itachi", "araiguma") animal match
{ case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } →λϓϧ ྻʹࣅͯΔ͚Ͳ ͷมߋͰ͖ͳ͍
match (2) val animal = ("tanuki", "itachi", "araiguma") animal match
{ case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } _ ϫΠϧυΧʔυ ԿͰΞϦ
match (2) val animal = ("tanuki", "itachi", "araiguma") animal match
{ case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } Ϛονͨ͠औಘͰ͖Δ
match (3) val someType: Any = "Yo" someType match {
case _: Int => println("seisuu") case _: String => println("mojiretsu") case _ => println("unknown") } ܕΛఆ͢Δ͜ͱՄೳ
ͦΖͦΖμϯϩʔυ ऴΘΓͦ͏
activator ui ىಈ͢Δͱϒϥβ্ཱ͕͕ͪΔ
Hello Scala! ࡞ͨ͠ΒCode view & Open in IDE Λબ
Hello world object Hello { def main(args: Array[String]): Unit =
{ println("Hello, world!") } }
Hello world object Hello { def main(args: Array[String]): Unit =
{ println("Hello, world!") } } mainؔ
Hello world object Hello { def main(args: Array[String]): Unit =
{ println("Hello, world!") } } HelloΦϒδΣΫτ
㊗️
FUNCTION CLASS OBJECT
Function(1) def pow (a: Int, b: Int): Int = {
if (b <= 1) a else pow(a, b - 1) * a } ! println(pow(2, 3)) def ໊ؔ(Ҿ໊: ܕ, .. ): ܕ = { .. } def ໊ؔ(Ҿ໊: ܕ, .. ) = { .. }
Function(1) def pow (a: Int, b: Int): Int = {
if (b <= 1) a else pow(a, b - 1) * a } ! println(pow(2, 3)) returnলུՄ
Function(2) val max = (a: Int, b: Int) => {
if (a > b) a else b } ! println(max(2, 3)) ໊ؔ(Ҿ໊: ܕ, .. ) => { .. }: ܕ ໊ؔ(Ҿ໊: ܕ, .. ) => { .. }
Function(2) val max = (a: Int, b: Int) => {
if (a > b) a else b } ! println(max(2, 3)) ScalaؔࣗମΛมʹͨ͠Γ ฦʹͨ͠ΓͰ͖Δ =ؔϦςϥϧ
Function(3) def multiply(a: Int)(b: Int) = { a * b
} val twice = multiply(2) ! println(twice(3)) 1ͭͷҾϦετෳͷ Ҿʹׂ͢Δ͜ͱ͕Ͱ͖Δ =ΧϦʔԽ
Function(3) def multiply(a: Int)(b: Int) = { a * b
} val twice = multiply(2) ! println(twice(3)) twice(Int)=>Intܕͷؔ ΧϦʔԽͨؔ͠ʹ ҾΛ෦తʹ༩͑Δͱ ৽͍͕ؔ͠࡞ΕΔ
Class(1) class Ship(n: String) { val name = n val
level = 1 val equipments = new Array[String](4) } ! val takao = new Ship("Takao") println(takao.level) class Ϋϥε໊ = { .. } class Ϋϥε໊(Ҿఆٛ) = { .. }
Class(1) class Ship(n: String) { val name = n val
level = 1 val equipments = new Array[String](4) } ! val takao = new Ship("Takao") println(takao.level) new Ϋϥε໊ ͰΠϯελϯεੜ
Class(2) class Ship(n: String) { val name = n }
! final class Battleship(n: String) extends Ship(n) { def explain = "[Battleship] " + name } ! val kirishima = new Battleship("Kirishima") println(kirishima explain) ܧঝɿݩ͋ΔΫϥε͔Β ৽͍͠ΫϥεΛ࡞Δ
Class(2) class Ship(n: String) { val name = n }
! final class Battleship(n: String) extends Ship(n) { def explain = "[Battleship] " + name } ! val kirishima = new Battleship("Kirishima") println(kirishima explain) extendͰܧঝ finalͰͦΕҎ্ܧঝͤ͞ͳ͍
Object(1) object RabbitHouse { var member = List("Chino", "Rize") def
invite(person: String) = { member = person :: member } } ! RabbitHouse.invite("Cocoa") println(RabbitHouse.member) Scalaʹstaticϝιου ແ͍ΘΓʹ objectͰγϯάϧτϯ ΦϒδΣΫτ͕࡞ΕΔ
Object(1) object RabbitHouse { var member = List("Chino", "Rize") def
invite(person: String) = { member = person :: member } } ! RabbitHouse.invite("Cocoa") println(RabbitHouse.member) object ΦϒδΣΫτ໊ = { .. } γϯάϧτϯΦϒδΣΫτ =Πϯελϯε͕1͔ͭ͠ ࡞Εͳ͍Ϋϥε
Object(2) class RabbitHouse { var member = List("Chino", "Rize", "Cocoa")
} ! object RabbitHouse { def apply() = new RabbitHouse } ! val rh = RabbitHouse() println(rh.member) applyϝιου ݺͼग़͢ࡍ໊લΛলུͰ͖Δ
ଞʹ͍Ζ͍Ζ͋Δ͚Ͳ ͻͱ·ͣऴΘΓ
Hello Play!
Play Scala Seed μϯϩʔυ࣌ؒΉͬͪΌ͔͔Δ
μϯϩʔυ͕ऴΘΔ·Ͱ… WAFʹ͍ͭͯ
Web application framework?
WebΞϓϦͷجຊ Webϖʔδ ݟͤͯ HTMLͱ͔
ΊΜͲ͍ Security Cashing URL mapping REST Database Web template Ajax
Web API
ͦ͜ͰWAF WebΞϓϦΛ੍࡞͢ΔࡍͷΊΜͲ͍෦͕ ͋Β͔͡Ί༻ҙ͞Ε͍ͯΔ
MVC = Model View Controller WebΞϓϦͰΑ͘ΘΕΔઃܭύλʔϯ Play FrameworkͰΘΕ͍ͯΔ
MVC Controller View Model
ͦΖͦΖμϯϩʔυ ऴΘΓͦ͏
ࠓͦ͜Hello Play μϯϩʔυͨ͠ॴͰactivator run localhost:9000ʹΞΫηε
㊗️
How it works?
PlayͷϑΝΠϧߏ app/ build.sbt conf/ logs/ project/ public/ target/ test/ →ΞϓϦͷιʔείʔυ
→ϏϧυεΫϦϓτ →ઃఆϑΝΠϧ →ϩάϑΝΠϧ →sbtઃఆϑΝΠϧ →Ξηοτ(ը૾ϑΝΠϧͱ͔) →ࣗಈͰੜ͞ΕΔϑΝΠϧ →ςετͷιʔείʔυ
sbt? ScalaͷϑΝΠϧΛϏϧυ͢Δπʔϧ ϥΠϒϥϦཧͯ͘͠ΕΔΒ͍͠ (Α͘Βͳ͍)
/conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) ΞΫηε͖ͯͨ͠ॴʹΑͬͯ
ͲͷϖʔδΛݟͤΔ͔ΛܾΊΔ
/conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) /
ʹΞΫηε͖ͯͨ͠Β controllers.Application.indexΛ࣮ߦ
/app/controller/Application.scala object Application extends Controller { def index = Action
{ Ok(views.html.index("Your new application is ready.")) } } ίϯτϩʔϥʔͷ༰ ControllerΛܧঝͨ͠ΦϒδΣΫτʹ ॻ͔Ε͍ͯΔ
/app/controller/Application.scala object Application extends Controller { def index = Action
{ Ok(views.html.index("Your new application is ready.")) } } → def index = Action.apply(Ok(views.html.index("..."))) index͕࣮ߦ͞ΕͨΒ Action.apply()Λฦ͢
/app/controller/Application.scala object Application extends Controller { def index = Action
{ Ok(views.html.index("Your new application is ready.")) } } ϏϡʔͷindexϑΝΠϧΛ OKͷεςʔλεͰฦ͢
/app/views/index.scala.html @(message: String) ! @main("Welcome to Play") { ! @play20.welcome(message)
! } .htmlϑΝΠϧ͚ͩͲ ී௨ͷhtmlϑΝΠϧͰͳ͍
Template Engine σʔλΛݩʹ.scala.htmlςϯϓϨʔτ͔Β ग़ྗ͢ΔWebϖʔδΛੜ͢Δ
Template engine(1) <html> <head> <title>@title</title> </head> <body> @content </body> </html>
ઌ಄ʹ@Λ͚ͭΔͱ ͦͷ෦Scalaͷίʔυ
Template engine(1) <html> <head> <title>@title</title> </head> <body> @content </body> </html>
@ม Ͱ มͷத͕ग़ྗ
Template engine(2) <ul> @for(u <- users) { <li>@u.name : @u.age</li>
@if(u.age >= 20) { <div>adult</div> } } </ul> @for() { .. } ͱ͔ @if() { .. } ͱ͔
Template engine(3) @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title>
</head> <body> @content </body> </html> ςϯϓϨʔτؔͷΑ͏ʹ ҾΛऔΔ͜ͱ͕Ͱ͖Δ
Template engine(3) @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title>
</head> <body> @content </body> </html> ઌ಄ߦʹScalaͷؔͬΆ͘ ҾͷఆٛΛ͢Δ
Template engine(4) @(message: String) ! @main("Welcome to Play") { !
@play20.welcome(message) ! } ҾΛఆٛ͢Δͱ ผϑΝΠϧ͔ΒςϯϓϨʔτͱ ಉ໊͡લͰ͕ؔݺΔ
Template engine(4) @(message: String) ! @main("Welcome to Play") { !
@play20.welcome(message) ! } Ҿͱͯ͠ "Welcome to Play”ͱ play20.welcome(message) Λmainʹ༩͍͑ͯΔ
·ͱΊΔͱ Application.scala index.scala.html routes
͓ർΕ༷Ͱͨ͠ ൃදऴΘΓͰ͢ ֤ࣗͰϑΝΠϧͷதΛม͑ͯ ͓͠ΖWebΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞