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

Learning Scalaの第1章〜第5章まで

Learning Scalaの第1章〜第5章まで

Learning Scalaの第1章〜第5章までの例文を順番にまとめました。
Scalaをこれから始める人向けに、難しいところに入る前に構文の書き方的な部分から見ていただければと思います。

Ryuichi Okada

July 10, 2016
Tweet

More Decks by Ryuichi Okada

Other Decks in Programming

Transcript

  1. About ME • ԬాཾҰ (@ryu1_okd) • גࣜձࣾάϥουΩϡʔϒ • SiTest •

    Node.js / LiveScript / AngularJS • Docker / CoreOS / fleet / etcd
  2. Learning Scala • Part1. CoreScala • 1: Getting Started with

    the Scala Language. • 2: Working with Data: Literals, Values, Variables, and Types. • 3: Expressions and Conditionals. • 4: Functions • 5: First-Class Functions.
  3. Values / Variables scala> val x = 5 x: Int

    = 5 scala> x = 10 <console>:11: error: reassignment to val x = 10 ^
  4. Values / Variables scala> var y = 5 y: Int

    = 5 scala> y = 10 y: Int = 10 scala> y = "Hello" <console>:11: error: type mismatch; found : String("Hello") required: Int y = "Hello" ^
  5. Expression scala> val x = 5 * 20; val amount

    = x + 10 x = 100 amount = 110 Block͸{}ͰғΉ scala> val amount = { | val x = 5 * 20 | x + 10 | } amount: Int = 110
  6. match/case otherwise ຢ͸ _ Λ࢖͏ scala> val status = 500

    scala> val message = status match{ | case 200 => "ok" | case 400 => println("ERROR 400"); "error" | case _ => { | println("ERROR 500") | "error" | } | } ERROR 500 message: String = error
  7. Matching Types with Pattern Variables ܕͰͷmatch/case scala> val x:Int =

    12180 x: Int = 12180 scala> val y:Any = x y: Any = 12180 scala> y match { | case x: String => s"'x'" | case x: Double => f"$x%.2f" | case x: Float => s"$x%.2f" | case x: Long => s"${x}l" | case x: Int => s"${x}i" | } res0: String = 12180i
  8. Loops scala> for (x <- 0 to 3) { println(s"loop

    $x")} loop 0 loop 1 loop 2 loop 3 scala> for (x <- 0 to 3) yield { s"loop $x"} res1: scala.collection.immutable.IndexedSeq[String] = Vector(loop 0, loop 1, loop 2, loop 3) scala> for (x <- 1 to 10 if x % 3 == 0) println(x) 3 6 9
  9. Nested Iterators scala> for { | x <- 1 to

    2 | y <- 1 to 3 | } | { println (s"($x, $y)") } (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3)
  10. Functions def <identifier> = <expression> def hi: String = "hi"

    def hi = "hi" scala> def multiplier(x:Int, y:Int): Int = { | x * y | } multiplier: (x: Int, y: Int)Int scala> multiplier(6, 7) res3: Int = 42
  11. Procedures scala> def log(d: Double): Unit = println(f"Got value $d%.2f")

    log: (d: Double)Unit scala> log(2.235353) Got value 2.24
  12. Calling Functions with Named Parameters scala> def greet(prefix: String, name:

    String) = s"$prefix $name" greet: (prefix: String, name: String)String scala> val greeting1 = greet("Ms", "Brown") greeting1: String = Ms Brown scala> val greeting2 = greet(name = "Brown", prefix = "Mr") greeting2: String = Mr Brown
  13. Parameters with Default Values scala> def greet(prefix: String = "

    ", name: String) = s"$prefix$name" greet: (prefix: String, name: String)String scala> val greeting1 = greet(name = "Paul") greeting1: String = " Paul" scala> val greeting2 = greet("Paul") <console>:11: error: not enough arguments for method greet: (prefix: String, name: String)String. Unspecified value parameter name. val greeting2 = greet("Paul") ^
  14. Type Parameters identityʹStringΛ౉͢ͱStringͰฦ͢ identityʹIntΛ౉͢ͱIntͰฦ͢ scala> def identity(a: Any): Any =

    a identity: (a: Any)Any scala> val s:String = identity("Hello") <console>:11: error: type mismatch; found : Any required: String val s:String = identity("Hello") ^
  15. Type Parameters scala> def identity[A](a: A): A = a identity:

    [A](a: A)A scala> val s: String = identity[String]("Hello") s: String = Hello scala> val i: Int = identity[Int](123) i: Int = 123 scala> val d: Double = identity(3.1415) d: Double = 3.1415 scala> val f = identity(3.1415f) f: Float = 3.1415
  16. Function Types and Values ([<type>, ...]) => <type> scala> def

    double(x: Int): Int = x * 2 double: (x: Int)Int scala> double(5) res3: Int = 10 scala> val myDouble: (Int) => Int = double myDouble: Int => Int = <function1> scala> myDouble(10) res4: Int = 20
  17. Higher-Order Functions ߴ֊ؔ਺ɹؔ਺ΛҾ਺ʹ࣋ͭؔ਺΍ؔ਺Λฦؔ͢਺ scala> def safeStringOp(s: String, f: String =>

    String) = { | if(s != null) f(s) else s | } safeStringOp: (s: String, f: String => String)String scala> def reverser(s: String) = s.reverse reverser: (s: String)String scala> safeStringOp(null, reverser) res5: String = null scala> safeStringOp("ABCDEFG", reverser) res6: String = GFEDCBA
  18. Partially Applied Function and Currying ෦෼ద༻ scala> def factorOf(x: Int,

    y: Int) = y % x == 0 factorOf: (x: Int, y: Int)Boolean scala> val multipleOf3 = factorOf(3, _: Int) multipleOf3: Int => Boolean = <function1> scala> val y = multipleOf3(78) y: Boolean = true
  19. Partially Applied Function and Currying ΧϦʔԽ scala> def factorOf(x: Int)(y:

    Int) = y % x == 0 factorOf: (x: Int)(y: Int)Boolean scala> val isEven = factorOf(2) _ isEven: Int => Boolean = <function1> scala> val z = isEven(32) z: Boolean = true
  20. Invoking Higher-Order Functions with Function Literal Blocks scala> def safeStringOp(s:

    String, f: String => String) = { | if (s != null) f(s) else s | } safeStringOp: (s: String, f: String => String)String scala> val uuid = java.util.UUID.randomUUID.toString uuid: String = 6c70d5ca-c7ce-4675-8671-e27ebd88d58d scala> val timedUUID = safeStringOp(uuid, { s => | val now = System.currentTimeMillis | val timed = s.take(24) + now | timed.toUpperCase | }) timedUUID: String = 6C70D5CA-C7CE-4675-8671-1468053661358