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
900
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
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1k
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
170
HTML/CSS超絶浅い説明
yuki0329
0
190
Swiftコンパイラ超入門+async関数の仕組み
shiz
0
170
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.1k
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
120
はてなにおけるfujiwara-wareの活用やecspressoのCI/CD構成 / Fujiwara Tech Conference 2025
cohalz
2
2.7k
QA環境で誰でも自由自在に現在時刻を操って検証できるようにした話
kalibora
1
140
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
150
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
280
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
How to train your dragon (web standard)
notwaldorf
89
5.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Designing for Performance
lara
604
68k
RailsConf 2023
tenderlove
29
970
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
192
16k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Automating Front-end Workflow
addyosmani
1366
200k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
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ΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞