Created
September 30, 2013 07:53
-
-
Save f81/6760593 to your computer and use it in GitHub Desktop.
第11章:Scalaの「Traversable」リファレンス ref: http://qiita.com/f81@github/items/62ad3c56ce6be271ef35
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).foreach(print(_)) | |
123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) ++ Seq(4, 5) | |
res1: Seq[Int] = List(1, 2, 3, 4, 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 1) toSet | |
res10: scala.collection.immutable.Set[Int] = Set(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 1) toStream | |
res11: scala.collection.immutable.Stream[Int] = Stream(1, ?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> var array = Array(4, 5, 6, 7, 8) | |
array: Array[Int] = Array(4, 5, 6, 7, 8) | |
scala> Seq(1, 2, 3) copyToArray (array) | |
scala> array | |
res12: Array[Int] = Array(1, 2, 3, 7, 8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> var buffer = scala.collection.mutable.Buffer(4, 5, 6, 7, 8) | |
buffer: scala.collection.mutable.Buffer[Int] = ArrayBuffer(4, 5, 6, 7, 8) | |
scala> Seq(1, 2, 3) copyToBuffer (buffer) | |
scala> buffer | |
res13: scala.collection.mutable.Buffer[Int] = ArrayBuffer(4, 5, 6, 7, 8, 1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) isEmpty | |
res14: Boolean = false | |
scala> Seq() isEmpty | |
res15: Boolean = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) nonEmpty | |
res16: Boolean = true | |
scala> Seq() nonEmpty | |
res17: Boolean = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) size | |
res18: Int = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) find (_ > 1) | |
res19: Option[Int] = Some(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) head | |
res20: Int = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) headOption | |
res21: Option[Int] = Some(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq("a", "b", "c") map (_.toUpperCase()) | |
res2: Seq[java.lang.String] = List(A, B, C) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) last | |
res22: Int = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) lastOption | |
res23: Option[Int] = Some(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) init | |
res24: Seq[Int] = List(1, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) take(2) | |
res25: Seq[Int] = List(1, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) drop(2) | |
res26: Seq[Int] = List(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) filter (_ % 2 == 1) | |
res27: Seq[Int] = List(1, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) filterNot(_ % 2 == 1) | |
res28: Seq[Int] = List(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) slice(2, 4) | |
res29: Seq[Int] = List(3, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) tail | |
res30: Seq[Int] = List(2, 3, 4, 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) count (_ % 2 == 0) | |
res31: Int = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq("red", "blue", "yellow") flatMap (_.toList) | |
res3: Seq[Char] = List(r, e, d, b, l, u, e, y, e, l, l, o, w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) exists (_ > 4) | |
res32: Boolean = true | |
scala> Seq(1, 2, 3, 4, 5) exists (_ > 5) | |
res33: Boolean = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) groupBy(_ > 3) | |
res34: scala.collection.immutable.Map[Boolean,Seq[Int]] = Map(false -> List(1, 2, 3), true -> List(4, 5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) partition(_ > 3) | |
res35: (Seq[Int], Seq[Int]) = (List(4, 5),List(1, 2, 3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) span(_ % 2 == 1) | |
res36: (Seq[Int], Seq[Int]) = (List(1),List(2, 3, 4, 5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3, 4, 5) splitAt (3) | |
res37: (Seq[Int], Seq[Int]) = (List(1, 2, 3),List(4, 5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).foldLeft(10)(_ - _) | |
res38: Int = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).foldRight(10)(_ - _) | |
res39: Int = -8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).reduceLeft(_ - _) | |
res40: Int = -4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).reduceRight(_ - _) | |
res41: Int = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).sum | |
res42: Int = 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) collect { case (i) if (i % 2 == 1) => i + 1 } | |
res4: Seq[Int] = List(2, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3).product | |
res43: Int = 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) min | |
res44: Int = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) max | |
res45: Int = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> var builder = new StringBuilder | |
builder: StringBuilder = | |
scala> Seq(1, 2, 3) addString(builder) | |
res46: StringBuilder = 123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) mkString ("a") | |
res47: String = 1a2a3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) view | |
res48: java.lang.Object with scala.collection.SeqView[Int,Seq[Int]] = SeqView(..) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) toArray | |
res5: Array[Int] = Array(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) toIterable | |
res6: Iterable[Int] = List(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq(1, 2, 3) toList | |
res7: List[Int] = List(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Seq((1,"Moses"), (2,"Lucas"), (3,"Henderson")) toMap | |
res8: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> Moses, 2 - | |
> Lucas, 3 -> Henderson) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> Map(1 -> "Moses", 2 -> "Lucas", 3 -> "Henderson").toSeq | |
res9: Seq[(Int, java.lang.String)] = ArrayBuffer((1,Moses), (2,Lucas), (3,Hender | |
son)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment