Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala & Play Framework 勉強会

Scala & Play Framework 勉強会

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

spring_raining

November 22, 2014
Tweet

More Decks by spring_raining

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. Play Frameworkͱ͸
    • Ruby on Rails / Django like
    • ͨ͘͞Μͷಋೖࣄྫ
    • ˒5,405

    View full-size slide

  6. GETTING STARTED

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. ม਺
    var hoge: Int = 10
    var fuga = "100"
    println(hoge + fuga)
    hoge = 20
    fuga = 200
    →ίϯύΠϧΤϥʔ
    fuga͸StringܕͳͷͰ
    Intܕ͸୅ೖͰ͖ͳ͍

    View full-size slide

  16. ఆ਺
    val teisuu = "konnitiwa"
    println(teisuu)
    val ໊લ = ஋
    val ໊લ : ܕ = ஋
    ఆ਺͸࠶୅ೖͰ͖ͳ͍

    View full-size slide

  17. ഑ྻ
    val a = Array("yui", "yukari", "yuzuko")
    println(a(0))
    println(a.length)
    Array(ॳظ஋..)
    new Array(഑ྻ௕)
    new Array[ܕ](഑ྻ௕)

    View full-size slide

  18. ഑ྻ
    val a = Array("yui", "yukari", "yuzuko")
    println(a(0))
    println(a.length)
    ഑ྻͷཁૉ͸[ ]Ͱ͸ͳ͘
    ( )Ͱऔಘ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  21. if
    var foo: String = ""
    foo = if (1 == 1) "yui" else "yuzuko"
    val bar = if (false) "yukari"
    println(bar)
    if͕஋Λฦ͞ͳ͍৔߹…
    Unitͱ͍͏ΫϥεΛฦ͢
    (voidΈ͍ͨͳ΋ͷ)

    View full-size slide

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

    View full-size slide

  23. for (1)
    val cast = Array("moffle", "macaron", "tiramy")
    for (i <- cast) {
    println(i)
    } ίϨΫγϣϯ͔ΒཁૉΛ
    1ͭͣͭऔΓग़࣮ͯ͠ߦ
    (foreachΈ͍ͨͳ΍ͭ)

    View full-size slide

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

    View full-size slide

  25. for (2)
    for (i <- 0 until 5) {
    println(i)
    }
    ੔਺ until ੔਺
    Ұ୴RangeΫϥεͰ੔਺ͷ
    ίϨΫγϣϯΛ࡞Γ
    ͔ͦ͜Β஋ΛऔΓग़͢

    View full-size slide

  26. 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)

    View full-size slide

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

    View full-size slide

  28. while
    var i = 0
    var summer = ""
    while (i < 10) {
    summer += "hiji"
    i += 1
    }
    println(summer)
    break΍continue͸ʁ

    View full-size slide

  29. 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ͷ͍͢͝΍ͭ

    View full-size slide

  30. 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

    View full-size slide

  31. 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)
    }

    View full-size slide

  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)
    }
    →λϓϧ
    ഑ྻʹࣅͯΔ͚Ͳ
    ஋ͷมߋ͸Ͱ͖ͳ͍

    View full-size slide

  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)
    }
    _ ͸ϫΠϧυΧʔυ
    ԿͰ΋ΞϦ

    View full-size slide

  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)
    } Ϛονͨ͠஋͸औಘͰ͖Δ

    View full-size slide

  35. match (3)
    val someType: Any = "Yo"
    someType match {
    case _: Int =>
    println("seisuu")
    case _: String =>
    println("mojiretsu")
    case _ =>
    println("unknown")
    }
    ܕΛ൑ఆ͢Δ͜ͱ΋Մೳ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. Hello world
    object Hello {
    def main(args: Array[String]): Unit = {
    println("Hello, world!")
    }
    }
    HelloΦϒδΣΫτ

    View full-size slide

  42. FUNCTION
    CLASS
    OBJECT

    View full-size slide

  43. 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 ؔ਺໊(Ҿ਺໊: ܕ, .. ) = { .. }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. Function(3)
    def multiply(a: Int)(b: Int) = {
    a * b
    }
    val twice = multiply(2)
    !
    println(twice(3))
    twice͸(Int)=>Intܕͷؔ਺
    ΧϦʔԽͨؔ͠਺ʹ
    Ҿ਺Λ෦෼తʹ༩͑Δͱ
    ৽͍ؔ͠਺͕࡞ΕΔ

    View full-size slide

  49. 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 Ϋϥε໊(Ҿ਺ఆٛ) = { .. }

    View full-size slide

  50. 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 Ϋϥε໊
    ͰΠϯελϯεੜ੒

    View full-size slide

  51. 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)
    ܧঝɿݩ͋ΔΫϥε͔Β
    ৽͍͠ΫϥεΛ࡞Δ

    View full-size slide

  52. 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ͰͦΕҎ্ܧঝͤ͞ͳ͍

    View full-size slide

  53. 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Ͱγϯάϧτϯ
    ΦϒδΣΫτ͕࡞ΕΔ

    View full-size slide

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

    View full-size slide

  55. Object(2)
    class RabbitHouse {
    var member = List("Chino", "Rize", "Cocoa")
    }
    !
    object RabbitHouse {
    def apply() = new RabbitHouse
    }
    !
    val rh = RabbitHouse()
    println(rh.member)
    applyϝιου͸
    ݺͼग़͢ࡍ໊લΛলུͰ͖Δ

    View full-size slide

  56. ଞʹ΋͍Ζ͍Ζ͋Δ͚Ͳ
    ͻͱ·ͣऴΘΓ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  59. Web application framework?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  64. MVC
    Controller
    View
    Model

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  67. How it works?

    View full-size slide

  68. PlayͷϑΝΠϧߏ଄
    app/
    build.sbt
    conf/
    logs/
    project/
    public/
    target/
    test/
    →ΞϓϦͷιʔείʔυ
    →ϏϧυεΫϦϓτ
    →ઃఆϑΝΠϧ
    →ϩάϑΝΠϧ
    →sbtઃఆϑΝΠϧ
    →Ξηοτ(ը૾ϑΝΠϧͱ͔)
    →ࣗಈͰੜ੒͞ΕΔϑΝΠϧ
    →ςετͷιʔείʔυ

    View full-size slide

  69. sbt?
    ScalaͷϑΝΠϧΛϏϧυ͢Δπʔϧ
    ϥΠϒϥϦ؅ཧ΋ͯ͘͠ΕΔΒ͍͠
    (Α͘஌Βͳ͍)

    View full-size slide

  70. /conf/routes
    GET /
    controllers.Application.index
    !
    GET /assets/*file
    controllers.Assets.at(path="/public", file)
    ΞΫηε͖ͯͨ͠৔ॴʹΑͬͯ
    ͲͷϖʔδΛݟͤΔ͔ΛܾΊΔ

    View full-size slide

  71. /conf/routes
    GET /
    controllers.Application.index
    !
    GET /assets/*file
    controllers.Assets.at(path="/public", file)
    / ʹΞΫηε͖ͯͨ͠Β
    controllers.Application.indexΛ࣮ߦ

    View full-size slide

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

    View full-size slide

  73. /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()Λฦ͢

    View full-size slide

  74. /app/controller/Application.scala
    object Application extends Controller {
    def index = Action {
    Ok(views.html.index("Your new application is ready."))
    }
    }
    ϏϡʔͷindexϑΝΠϧΛ
    OKͷεςʔλεͰฦ͢

    View full-size slide

  75. /app/views/index.scala.html
    @(message: String)
    !
    @main("Welcome to Play") {
    !
    @play20.welcome(message)
    !
    } .htmlϑΝΠϧ͚ͩͲ
    ී௨ͷhtmlϑΝΠϧͰ͸ͳ͍

    View full-size slide

  76. Template Engine
    σʔλΛݩʹ.scala.htmlςϯϓϨʔτ͔Β
    ग़ྗ͢ΔWebϖʔδΛੜ੒͢Δ

    View full-size slide

  77. Template engine(1)


    @title


    @content


    ઌ಄ʹ@Λ͚ͭΔͱ
    ͦͷ෦෼͸Scalaͷίʔυ

    View full-size slide

  78. Template engine(1)


    @title


    @content


    @ม਺ Ͱ
    ม਺ͷத਎͕ग़ྗ

    View full-size slide

  79. Template engine(2)

    @for(u <- users) {
    @u.name : @u.age
    @if(u.age >= 20) {
    adult
    }
    }

    @for() { .. } ͱ͔
    @if() { .. } ͱ͔

    View full-size slide

  80. Template engine(3)
    @(title: String)(content: Html)



    @title


    @content


    ςϯϓϨʔτ͸ؔ਺ͷΑ͏ʹ
    Ҿ਺ΛऔΔ͜ͱ͕Ͱ͖Δ

    View full-size slide

  81. Template engine(3)
    @(title: String)(content: Html)



    @title


    @content


    ઌ಄ߦʹScalaͷؔ਺ͬΆ͘
    Ҿ਺ͷఆٛΛ͢Δ

    View full-size slide

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

    View full-size slide

  83. Template engine(4)
    @(message: String)
    !
    @main("Welcome to Play") {
    !
    @play20.welcome(message)
    !
    }
    Ҿ਺ͱͯ͠
    "Welcome to Play”ͱ
    play20.welcome(message)
    Λmainʹ༩͍͑ͯΔ

    View full-size slide

  84. ·ͱΊΔͱ
    Application.scala
    index.scala.html
    routes

    View full-size slide

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

    View full-size slide