Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
880
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
気をつけたい!Desktop対応で陥りやすい罠とその対策
goto_tsl
0
200
コンテンツの主権を守るため(?)、高機能画像CDNからAWS自前対応に乗り換えた話
lengthtail
1
120
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
940
Seamless Flutter Native Integration: FFI & Pigeon - Dreamwalker (JaichangPark / 박제창) @FlutterKaigi2024
itsmedreamwalker
0
120
ソフトウェアの振る舞いに着目し 複雑な要件の開発に立ち向かう
rickyban
0
820
社内活動の取り組み紹介 ~ スリーシェイクでこんな取り組みしてます ~
bells17
0
390
Welcome JSConf.jp 2024
yosuke_furukawa
PRO
0
3.1k
イマのCSSでできる インタラクション最前線 + CSS最新情報
clockmaker
5
3.8k
MoQとか勉強会#2 発表資料
yuki_uchida
2
630
.NET Conf 2024の振り返り
tomokusaba
0
200
Missing parts when designing and implementing Android UI
ericksli
0
390
物流システムにおけるリファクタリングとアーキテクチャの再構築 〜依存関係とモジュール分割の重要性〜
deeprain
1
730
Featured
See All Featured
Statistics for Hackers
jakevdp
796
220k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
For a Future-Friendly Web
brad_frost
175
9.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
43
9.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
27
2.1k
Thoughts on Productivity
jonyablonski
67
4.3k
Gamification - CAS2011
davidbonilla
80
5.1k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Code Review Best Practice
trishagee
64
17k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
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ΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞