Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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. Play Frameworkͱ͸ • Ruby on Rails / Django like •

    ͨ͘͞Μͷಋೖࣄྫ • ˒5,405
  2. ม਺ var hoge: Int = 10 var fuga = "100"

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

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

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

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

    val ໊લ : ܕ = ஋ ఆ਺͸࠶୅ೖͰ͖ͳ͍
  7. if var foo: String = "" foo = if (1

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

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

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

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

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

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

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

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

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

    case _: Int => println("seisuu") case _: String => println("mojiretsu") case _ => println("unknown") } ܕΛ൑ఆ͢Δ͜ͱ΋Մೳ
  24. Hello world object Hello { def main(args: Array[String]): Unit =

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

    { println("Hello, world!") } } HelloΦϒδΣΫτ
  26. 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 ؔ਺໊(Ҿ਺໊: ܕ, .. ) = { .. }
  27. Function(1) def pow (a: Int, b: Int): Int = {

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

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

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

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

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

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

    } ! object RabbitHouse { def apply() = new RabbitHouse } ! val rh = RabbitHouse() println(rh.member) applyϝιου͸ ݺͼग़͢ࡍ໊લΛলུͰ͖Δ
  39. PlayͷϑΝΠϧߏ଄ app/ build.sbt conf/ logs/ project/ public/ target/ test/ →ΞϓϦͷιʔείʔυ

    →ϏϧυεΫϦϓτ →ઃఆϑΝΠϧ →ϩάϑΝΠϧ →sbtઃఆϑΝΠϧ →Ξηοτ(ը૾ϑΝΠϧͱ͔) →ࣗಈͰੜ੒͞ΕΔϑΝΠϧ →ςετͷιʔείʔυ
  40. /app/controller/Application.scala object Application extends Controller { def index = Action

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

    { Ok(views.html.index("Your new application is ready.")) } } ϏϡʔͷindexϑΝΠϧΛ OKͷεςʔλεͰฦ͢
  43. Template engine(2) <ul> @for(u <- users) { <li>@u.name : @u.age</li>

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

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

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

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

    @play20.welcome(message) ! } Ҿ਺ͱͯ͠ "Welcome to Play”ͱ play20.welcome(message) Λmainʹ༩͍͑ͯΔ