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
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
200
Discord Bot with AI -for English learners-
xin9le
1
120
CSC305 Lecture 26
javiergs
PRO
0
140
Refactor your code - refactor yourself
xosofox
1
260
Featured
See All Featured
Fireside Chat
paigeccino
34
3.1k
Mobile First: as difficult as doing things right
swwweet
222
9k
Designing for Performance
lara
604
68k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
A better future with KSS
kneath
238
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Six Lessons from altMBA
skipperchong
27
3.5k
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ΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞