Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ゼロから始めるScalaプロジェクト

0 views

Published on

初心者向けScalaプロジェクト作成を行うハンズオン資料です. サンプルプログラムは (https://github.com/amaya382/scala-hands-on) を参照してください. (@Scala関西Summit 2017)

Published in: Software
  • Be the first to comment

ゼロから始めるScalaプロジェクト

  1. 1.           2
  2. 2.                 3
  3. 3.                     4
  4. 4.     scala   sbt about     5
  5. 5.    sbt new scala/hello-world.g8        cd sbt-example sbt     sbt run 6
  6. 6.    run    console  println("Hello")   Main.main(null)   :paste  :quit 7
  7. 7.     8
  8. 8. 9
  9. 9. sbt-example/ ├ build.sbt ├ project/ │ ├ build.properties │ └ plugins.sbt ├ src/ │ ├ main/ │ │ └ scala/ │ │ └ Main.scala │ └ test/ │ └ scala/ │ └ MainSpec.scala └ target/ 10
  10. 10.         11
  11. 11.                         reload 12
  12. 12.    libraryDependencies ++= Seq( "org.scala-sbt" %% "io" % "1.0.0", "org.json4s" %% "json4s-jackson" % "3.5.2", "org.scalatest" %% "scalatest" % "3.0.1" % Test)    reload  console  import sbt.io.IO   13
  13. 13.                         14
  14. 14.              def fizzBuzz(n: Int): Unit = ???     15 object Main extends App { ??? } object Main { def main(…): Unit = { ??? } }
  15. 15. def fizzBuzz(n: Int): Unit = for {i <- 1 to n} { i match { case x if x % 15 == 0 => println("FizzBuzz") case x if x % 3 == 0 => println("Fizz") case x if x % 5 == 0 => println("Buzz") case x => println(x) } } fizzBuzz(15) 16
  16. 16.                   17 val pw = new java.io.PrintWriter("sample.json") pw.print(scala.io.Source.fromURL("h ttps://goo.gl/KbGKi7").mkString) pw.flush(); pw.close()
  17. 17.    def readFile(fileName: String) : String = ???    import sbt.io.IO  IO.read  java.io.File 18
  18. 18. import sbt.io.IO import java.io.File def readFile(fileName: String): String = IO.read(new File(fileName)) 19
  19. 19.      def parseIntArrayJSON(jsonString: String) : List[Int] = ???    import org.json4s._  import org.json4s.jackson.JsonMethods  JsonMethods.parse      toInt 20
  20. 20. import org.json4s._ import org.json4s.jackson.JsonMethods._ def parseIntArrayJSON(jsonString: String) : List[Int] = { val json = parse(jsonString) for { JArray(array) <- json JInt(x) <- array } yield x.toInt } 21
  21. 21.    def toFizzBuzz(ints: List[Int]) : List[String] = ???     22
  22. 22. def toFizzBuzz(ints: List[Int]) : List[String] = for {i <- ints} yield i match { case x if x % 15 == 0 => "FizzBuzz" case x if x % 3 == 0 => "Fizz" case x if x % 5 == 0 => "Buzz" case x => x.toString } 23
  23. 23. def toFizzBuzz(ints: List[Int]) : List[String] = ints.map(i => i match { case x if x % 15 == 0 => "FizzBuzz" case x if x % 3 == 0 => "Fizz" case x if x % 5 == 0 => "Buzz" case x => x.toString }) 24
  24. 24. def toFizzBuzz(ints: List[Int]) : List[String] = ints.map(i => i match { case x if x % 15 == 0 => "FizzBuzz" case x if x % 3 == 0 => "Fizz" case x if x % 5 == 0 => "Buzz" case x => x.toString }) 25
  25. 25. def toFizzBuzz(ints: Seq[Int]): Seq[String] = ints.map { case x if x % 15 == 0 => "FizzBuzz" case x if x % 3 == 0 => "Fizz" case x if x % 5 == 0 => "Buzz" case x => x.toString } 26
  26. 26.    def toJSONFormat(list: List[String]) : String = ???    import org.json4s.JsonDSL._  render/compact 27
  27. 27. import org.json4s.JsonDSL._ import org.json4s.jackson.JsonMethods._ def toJSONFormat(list: List[String]): String = compact(render(list)) 28
  28. 28.    def writeFile(fileName: String, contents: String): Unit = ???    import sbt.io.IO  IO.write  java.io.File 29
  29. 29. import sbt.io.IO def writeFile(fileName: String, contents: String): Unit = IO.write(new File(fileName), contents) 30
  30. 30.              import org.scalatest._  class MainSpec extends FlatSpec { ... }  "foo" should "bar" in { ??? }  assert 31
  31. 31. class MainSpec extends FlatSpec {! "FizzBuzzed sample.json" should! "have the same elements as answer.json" in {! val sampleString = IO.read(new File("sample.json"))! val sampleJSON = JsonMethods.parse(sampleString)! val sample = for {! JArray(arr) <- sampleJSON! JInt(x) <- arr! } yield x.toInt! val fizzBuzzedSample = Main.toFizzBuzz(sample)! val answerString = IO.read(new File("answer.json"))! val answerJSON = JsonMethods.parse(answerString)! val answer = for {! JArray(arr) <- answerJSON! JString(x) <- arr! } yield x! assert(! fizzBuzzedSample.sameElements(answer))! }! } import java.io.File! import org.json4s._! import org.json4s.jackson.JsonMethods! import org.scalatest._! import sbt.io.IO! 32
  32. 32.      addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.9.0")  sbt pack           33

×