Scala & Play Framework 勉強会

Scala & Play Framework 勉強会

2014年11月22日 OUCC Scala & Play Framework 勉強会のスライドです
author: @spring_raining

42ba906dc35ef460ffcb1b7c3307ef45?s=128

spring_raining

November 22, 2014
Tweet

Transcript

  1. OUCC Scala × Play Framework Hands-on @spring_raining

  2. એ఻ ҰॹʹWebΞϓϦ࡞Ζ͏ʂʂʂʂʂ̍ʂʂ →→→Reply to @spring_raining

  3. એ఻ OUCC Advent Calendarॻ͖·ͤΜ͔ ʂʂʂʂʂʁʁʁʂʂʂʂʁʁ →http://www.adventar.org/calendars/449

  4. Scalaͱ͸ • Javaͱಉ͡؀ڥͰಈ͘ • ؆ܿͳදه • ؔ਺ܕmeetsΦϒδΣΫτࢦ޲

  5. Play Frameworkͱ͸ • Ruby on Rails / Django like •

    ͨ͘͞Μͷಋೖࣄྫ • ˒5,405
  6. GETTING STARTED

  7. ʮJava SEʯͰݕࡧݕࡧ ʮJDKʯΛΠϯετʔϧ

  8. Θ͔Γʹ͍͘ ʮAccept License AgreementʯΛΫϦοΫ

  9. typesafe.com Typesafe Activator Λμ΢ϯϩʔυ & Πϯετʔϧ

  10. IDE͸͓޷ΈͰ IntelliJ IDEAΛ૝ఆͯ͠ਐΊ·͢

  11. μ΢ϯϩʔυ͕ऴΘΔ·Ͱ… Scalaͷجຊߏจ

  12. SYNTAX

  13. ม਺ var hoge: Int = 10 var fuga = "100"

    println(hoge + fuga) hoge = 20 fuga = 200
  14. ม਺ var hoge: Int = 10 var fuga = "100"

    println(hoge + fuga) hoge = 20 fuga = 200 ! var ໊લ = ஋ var ໊લ: ܕ = ஋ ܕ͸ࣗಈͰਪଌ͞ΕΔ
  15. ม਺ var hoge: Int = 10 var fuga = "100"

    println(hoge + fuga) hoge = 20 fuga = 200 →10110
  16. ม਺ var hoge: Int = 10 var fuga = "100"

    println(hoge + fuga) hoge = 20 fuga = 200 →ίϯύΠϧΤϥʔ fuga͸StringܕͳͷͰ Intܕ͸୅ೖͰ͖ͳ͍
  17. ఆ਺ val teisuu = "konnitiwa" println(teisuu) val ໊લ = ஋

    val ໊લ : ܕ = ஋ ఆ਺͸࠶୅ೖͰ͖ͳ͍
  18. ഑ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) Array(ॳظ஋..)

    new Array(഑ྻ௕) new Array[ܕ](഑ྻ௕)
  19. ഑ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) ഑ྻͷཁૉ͸[

    ]Ͱ͸ͳ͘ ( )Ͱऔಘ
  20. if var foo: String = "" foo = if (1

    == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if (৚݅ࣜ) ࣜ if (৚݅ࣜ) ࣜ else ࣜ if (৚݅ࣜ) { ࣜ.. } if (৚݅ࣜ) { ࣜ.. } else { ࣜ.. }
  21. if var foo: String = "" foo = if (1

    == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if͸ʮࣜʯͳͷͰ஋Λฦ͢ ฦ͞Εͨ஋͸ ม਺ʹ୅ೖ͞ΕΔ 
  22. if var foo: String = "" foo = if (1

    == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if͕஋Λฦ͞ͳ͍৔߹… Unitͱ͍͏ΫϥεΛฦ͢ (voidΈ͍ͨͳ΋ͷ)
  23. for (1) val cast = Array("moffle", "macaron", "tiramy") for (i

    <- cast) { println(i) } for (ม਺໊ <- ίϨΫγϣϯ) { ࣜ.. }
  24. for (1) val cast = Array("moffle", "macaron", "tiramy") for (i

    <- cast) { println(i) } ίϨΫγϣϯ͔ΒཁૉΛ 1ͭͣͭऔΓग़࣮ͯ͠ߦ (foreachΈ͍ͨͳ΍ͭ)
  25. for (2) for (i <- 0 until 5) { println(i)

    } Scalaʹ͸͍ΘΏΔ for (ʙ ; ʙ ; ʙ) ͸࢖͑ͳ͍
  26. for (2) for (i <- 0 until 5) { println(i)

    } ੔਺ until ੔਺ Ұ୴RangeΫϥεͰ੔਺ͷ ίϨΫγϣϯΛ࡞Γ ͔ͦ͜Β஋ΛऔΓग़͢
  27. 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)
  28. while var i = 0 var summer = "" while

    (i < 10) { summer += "hiji" i += 1 } println(summer) while (৚݅ࣜ) ࣜ while (৚݅ࣜ) { ࣜ.. }
  29. while var i = 0 var summer = "" while

    (i < 10) { summer += "hiji" i += 1 } println(summer) break΍continue͸ʁ
  30. 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ͷ͍͢͝΍ͭ
  31. 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
  32. 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) }
  33. 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) } →λϓϧ ഑ྻʹࣅͯΔ͚Ͳ ஋ͷมߋ͸Ͱ͖ͳ͍
  34. 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) } _ ͸ϫΠϧυΧʔυ ԿͰ΋ΞϦ
  35. 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) } Ϛονͨ͠஋͸औಘͰ͖Δ
  36. match (3) val someType: Any = "Yo" someType match {

    case _: Int => println("seisuu") case _: String => println("mojiretsu") case _ => println("unknown") } ܕΛ൑ఆ͢Δ͜ͱ΋Մೳ
  37. ͦΖͦΖμ΢ϯϩʔυ ऴΘΓͦ͏

  38. activator ui ىಈ͢Δͱϒϥ΢β্ཱ͕͕ͪΔ

  39. Hello Scala! ࡞੒ͨ͠ΒCode view & Open in IDE Λબ୒

  40. Hello world object Hello { def main(args: Array[String]): Unit =

    { println("Hello, world!") } }
  41. Hello world object Hello { def main(args: Array[String]): Unit =

    { println("Hello, world!") } } mainؔ਺
  42. Hello world object Hello { def main(args: Array[String]): Unit =

    { println("Hello, world!") } } HelloΦϒδΣΫτ
  43. ㊗️

  44. FUNCTION CLASS OBJECT

  45. 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 ؔ਺໊(Ҿ਺໊: ܕ, .. ) = { .. }
  46. Function(1) def pow (a: Int, b: Int): Int = {

    if (b <= 1) a else pow(a, b - 1) * a } ! println(pow(2, 3)) return͸লུՄ
  47. Function(2) val max = (a: Int, b: Int) => {

    if (a > b) a else b } ! println(max(2, 3)) ؔ਺໊(Ҿ਺໊: ܕ, .. ) => { .. }: ໭ܕ ؔ਺໊(Ҿ਺໊: ܕ, .. ) => { .. }
  48. Function(2) val max = (a: Int, b: Int) => {

    if (a > b) a else b } ! println(max(2, 3)) Scala͸ؔ਺ࣗମΛม਺ʹͨ͠Γ ฦ஋ʹͨ͠ΓͰ͖Δ =ؔ਺Ϧςϥϧ
  49. Function(3) def multiply(a: Int)(b: Int) = { a * b

    } val twice = multiply(2) ! println(twice(3)) 1ͭͷҾ਺Ϧετ͸ෳ਺ͷ Ҿ਺ʹ෼ׂ͢Δ͜ͱ͕Ͱ͖Δ =ΧϦʔԽ
  50. Function(3) def multiply(a: Int)(b: Int) = { a * b

    } val twice = multiply(2) ! println(twice(3)) twice͸(Int)=>Intܕͷؔ਺ ΧϦʔԽͨؔ͠਺ʹ Ҿ਺Λ෦෼తʹ༩͑Δͱ ৽͍ؔ͠਺͕࡞ΕΔ
  51. 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 Ϋϥε໊(Ҿ਺ఆٛ) = { .. }
  52. 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 Ϋϥε໊ ͰΠϯελϯεੜ੒
  53. 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) ܧঝɿݩ͋ΔΫϥε͔Β ৽͍͠ΫϥεΛ࡞Δ
  54. 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ͰͦΕҎ্ܧঝͤ͞ͳ͍
  55. 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Ͱγϯάϧτϯ ΦϒδΣΫτ͕࡞ΕΔ
  56. Object(1) object RabbitHouse { var member = List("Chino", "Rize") def

    invite(person: String) = { member = person :: member } } ! RabbitHouse.invite("Cocoa") println(RabbitHouse.member) object ΦϒδΣΫτ໊ = { .. } γϯάϧτϯΦϒδΣΫτ =Πϯελϯε͕1͔ͭ͠ ࡞Εͳ͍Ϋϥε
  57. Object(2) class RabbitHouse { var member = List("Chino", "Rize", "Cocoa")

    } ! object RabbitHouse { def apply() = new RabbitHouse } ! val rh = RabbitHouse() println(rh.member) applyϝιου͸ ݺͼग़͢ࡍ໊લΛলུͰ͖Δ
  58. ଞʹ΋͍Ζ͍Ζ͋Δ͚Ͳ ͻͱ·ͣऴΘΓ

  59. Hello Play!

  60. Play Scala Seed μ΢ϯϩʔυ࣌ؒΉͬͪΌ͔͔Δ

  61. μ΢ϯϩʔυ͕ऴΘΔ·Ͱ… WAFʹ͍ͭͯ

  62. Web application framework?

  63. WebΞϓϦͷجຊ Webϖʔδ ݟͤͯ HTMLͱ͔

  64. ΊΜͲ͍ Security Cashing URL mapping REST Database Web template Ajax

    Web API
  65. ͦ͜ͰWAF WebΞϓϦΛ੍࡞͢ΔࡍͷΊΜͲ͍෦෼͕ ͋Β͔͡Ί༻ҙ͞Ε͍ͯΔ

  66. MVC = Model View Controller WebΞϓϦͰΑ͘࢖ΘΕΔઃܭύλʔϯ Play FrameworkͰ΋࢖ΘΕ͍ͯΔ

  67. MVC Controller View Model

  68. ͦΖͦΖμ΢ϯϩʔυ ऴΘΓͦ͏

  69. ࠓ౓ͦ͜Hello Play μ΢ϯϩʔυͨ͠৔ॴͰactivator run localhost:9000ʹΞΫηε

  70. ㊗️

  71. How it works?

  72. PlayͷϑΝΠϧߏ଄ app/ build.sbt conf/ logs/ project/ public/ target/ test/ →ΞϓϦͷιʔείʔυ

    →ϏϧυεΫϦϓτ →ઃఆϑΝΠϧ →ϩάϑΝΠϧ →sbtઃఆϑΝΠϧ →Ξηοτ(ը૾ϑΝΠϧͱ͔) →ࣗಈͰੜ੒͞ΕΔϑΝΠϧ →ςετͷιʔείʔυ
  73. sbt? ScalaͷϑΝΠϧΛϏϧυ͢Δπʔϧ ϥΠϒϥϦ؅ཧ΋ͯ͘͠ΕΔΒ͍͠ (Α͘஌Βͳ͍)

  74. /conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) ΞΫηε͖ͯͨ͠৔ॴʹΑͬͯ

    ͲͷϖʔδΛݟͤΔ͔ΛܾΊΔ
  75. /conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) /

    ʹΞΫηε͖ͯͨ͠Β controllers.Application.indexΛ࣮ߦ
  76. /app/controller/Application.scala object Application extends Controller { def index = Action

    { Ok(views.html.index("Your new application is ready.")) } } ίϯτϩʔϥʔͷ಺༰͸ ControllerΛܧঝͨ͠ΦϒδΣΫτʹ ॻ͔Ε͍ͯΔ
  77. /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()Λฦ͢
  78. /app/controller/Application.scala object Application extends Controller { def index = Action

    { Ok(views.html.index("Your new application is ready.")) } } ϏϡʔͷindexϑΝΠϧΛ OKͷεςʔλεͰฦ͢
  79. /app/views/index.scala.html @(message: String) ! @main("Welcome to Play") { ! @play20.welcome(message)

    ! } .htmlϑΝΠϧ͚ͩͲ ී௨ͷhtmlϑΝΠϧͰ͸ͳ͍
  80. Template Engine σʔλΛݩʹ.scala.htmlςϯϓϨʔτ͔Β ग़ྗ͢ΔWebϖʔδΛੜ੒͢Δ

  81. Template engine(1) <html> <head> <title>@title</title> </head> <body> @content </body> </html>

    ઌ಄ʹ@Λ͚ͭΔͱ ͦͷ෦෼͸Scalaͷίʔυ
  82. Template engine(1) <html> <head> <title>@title</title> </head> <body> @content </body> </html>

    @ม਺ Ͱ ม਺ͷத਎͕ग़ྗ
  83. Template engine(2) <ul> @for(u <- users) { <li>@u.name : @u.age</li>

    @if(u.age >= 20) { <div>adult</div> } } </ul> @for() { .. } ͱ͔ @if() { .. } ͱ͔
  84. Template engine(3) @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title>

    </head> <body> @content </body> </html> ςϯϓϨʔτ͸ؔ਺ͷΑ͏ʹ Ҿ਺ΛऔΔ͜ͱ͕Ͱ͖Δ
  85. Template engine(3) @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title>

    </head> <body> @content </body> </html> ઌ಄ߦʹScalaͷؔ਺ͬΆ͘ Ҿ਺ͷఆٛΛ͢Δ
  86. Template engine(4) @(message: String) ! @main("Welcome to Play") { !

    @play20.welcome(message) ! } Ҿ਺Λఆٛ͢Δͱ ผϑΝΠϧ͔ΒςϯϓϨʔτͱ ಉ໊͡લͰؔ਺͕ݺ΂Δ
  87. Template engine(4) @(message: String) ! @main("Welcome to Play") { !

    @play20.welcome(message) ! } Ҿ਺ͱͯ͠ "Welcome to Play”ͱ play20.welcome(message) Λmainʹ༩͍͑ͯΔ
  88. ·ͱΊΔͱ Application.scala index.scala.html routes

  89. ͓ർΕ༷Ͱͨ͠ ൃද͸ऴΘΓͰ͢ ֤ࣗͰϑΝΠϧͷத਎Λม͑ͯ ͓΋͠ΖWebΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞