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
870
JavaScriptでもディープラーニングってやつでなんとかして / Kyoto.js 15
spring_raining
2
2.1k
CSS組版の救世主 Vivliostyle / HTML5 Conference 2018
spring_raining
6
7.8k
印刷・出版のためのオンラインエディタ Viola / Viola - Online editor for printing and publishing
spring_raining
3
670
Printable Web
spring_raining
3
880
今こそCSS組版
spring_raining
5
2.4k
シン・サーバの形は。
spring_raining
0
310
Other Decks in Programming
See All in Programming
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
as(型アサーション)を書く前にできること
marokanatani
9
2.6k
EventSourcingの理想と現実
wenas
6
2.3k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Ethereum_.pdf
nekomatu
0
460
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
870
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
190
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Why Our Code Smells
bkeepers
PRO
334
57k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
BBQ
matthewcrist
85
9.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
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ΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞