LoginSignup
67
54

More than 5 years have passed since last update.

第11章:ScalaのSeqリファレンス

Last updated at Posted at 2013-09-30

今回はSeqについて、リファレンスなノリで紹介するよ。

入門編としてこちらも読んでね(^^)v
第8章:Scalaのコレクション(Seq, Set, Map)入門

scala.collection.immutable.Seq

要素の順序を意識する必要がある場合に、使う箱だ。
JavaのListと役割は似ているね。
ただ色んなことができるんだ。
しかも前回の 第10章:ScalaのTraversableリファレンスで紹介したメソッドも使えるんだ。
前回のソースのほとんどがSeqだったことに気付いたかな?

添字参照

(i)

添字の要素を返す

scala> Seq(1, 2, 3)(2)
res0: Int = 3

isDefinedAt

指定した値が、添字の範囲に存在するか判定する

scala> Seq(1, 2, 3) isDefinedAt (3)
res1: Boolean = false

scala> Seq(1, 2, 3) isDefinedAt (2)
res2: Boolean = true

長さ

length

要素の数を返す

scala> Seq(1, 2, 3) length
res3: Int = 3

lengthCompare

要素数が指定した値と比較し以下を返す
- 少ない場合は1
- 同じ場合は0
- 多い場合は1

scala> Seq(1, 2, 3) lengthCompare (2)
res4: Int = 1

scala> Seq(1, 2, 3) lengthCompare(3)
res5: Int = 0

scala> Seq(1, 2, 3) lengthCompare (4)
res6: Int = -1

indices

添字の範囲を返す

scala> Seq(1, 2, 3) indices
res7: scala.collection.immutable.Range = Range(0, 1, 2)

検索

indexOf

指定した値と等しい最初の要素の添字を返す

scala> Seq(1, 2, 3, 2) indexOf (2)
res8: Int = 1

scala> Seq(1, 2, 3, 2) indexOf (4)
res9: Int = -1

lastIndexOf

指定した値と等しい最後の要素の添字を返す

scala> Seq(1, 2, 3, 2) lastIndexOf (2)
res10: Int = 3

indexOfSlince

指定したシーケンスと等しい最初の要素の添字を返す

scala> Seq(1, 2, 3, 2, 3) indexOfSlice Seq(2, 3)
res11: Int = 1

scala> Seq(1, 2, 3, 2, 3) indexOfSlice Seq(2, 2)
res12: Int = -1

lastIndexOfSlice

指定したシーケンスと等しい最後の要素の添字を返す

scala> Seq(1, 2, 3, 2, 3) lastIndexOfSlice Seq(2, 3)
res13: Int = 3

indexWhere

関数を満たす最初の要素の添字を返す

scala> Seq(1, 2, 3, 2, 3) indexWhere (_ == 2)
res14: Int = 1

prefixLength

関数を満たさない最初の要素の添字を返す

scala> Seq(1, 2, 3, 2) prefixLength (_ != 3)
res15: Int = 2

加算・追加

+:

指定した要素をSeqの先頭に追加した新しいSeqを返す

scala> 4 +: Seq(1, 2, 3)
res16: Seq[Int] = List(4, 1, 2, 3)

:+

指定した要素をSeqの末尾に追加した新しいSeqを返す

scala> Seq(1, 2, 3) :+ 4
res17: Seq[Int] = List(1, 2, 3, 4)

padTo

指定した長さになるまで、指定した要素をパディングする

scala> Seq(1, 2, 3) padTo (10, 4)
res18: Seq[Int] = List(1, 2, 3, 4, 4, 4, 4, 4, 4, 4)

更新

patch

指定した添字から指定した要素数を、指定したSeqで置き換えた新しいSeqを返す

scala> Seq(1, 2, 3) patch (1, Seq(4, 5, 6), 2)
res19: Seq[Int] = List(1, 4, 5, 6)

updated

指定した添字の要素を、指定した要素で置き換えた新しいSeqを返す

scala> Seq(1, 2, 3) updated (2, 4)
res20: Seq[Int] = List(1, 2, 4)

ソート

sorted

要素をソートした新しいSeqを返す

scala> Seq(4, 1, 5, 2, 3, 7, 8) sorted
res21: Seq[Int] = List(1, 2, 3, 4, 5, 7, 8)

逆順

reverse

要素を逆順にした新しいSeqを返す

scala> Seq(1, 2, 3) reverse
res22: Seq[Int] = List(3, 2, 1)

比較

startsWith

指定されたSeqで始まっているか判定する

scala> Seq(1, 2, 3) startsWith Seq(1, 2)
res23: Boolean = true

scala> Seq(1, 2, 3) startsWith Seq(2, 3)
res24: Boolean = false

endsWith

指定されたSeqで終わっているか判定する

scala> Seq(1, 2, 3) endsWith Seq(2, 3)
res25: Boolean = true

scala> Seq(1, 2, 3) endsWith Seq(1, 2)
res26: Boolean = false

contains

指定した要素を含んでいるか判定する

scala> Seq(1, 2, 3) contains (3)
res27: Boolean = true

scala> Seq(1, 2, 3) contains (4)
res28: Boolean = false

containsSlice

指定したSeqを含んでいるか判定する

scala> Seq(1, 2, 3, 4, 5) containsSlice Seq(3, 4)
res29: Boolean = true

scala> Seq(1, 2, 3, 4, 5) containsSlice Seq(4, 4)
res30: Boolean = false

集合間演算

intersect

積集合を返す

scala> Seq(1, 2, 3) intersect Seq(2, 3, 4, 5, 6)
res28: Seq[Int] = List(2, 3)

diff

差集合を返す

scala> Seq(2, 3, 4, 5, 6) diff Seq(1, 2, 3)
res30: Seq[Int] = List(4, 5, 6)

union

和集合を返す

scala> Seq(2, 3, 4, 5, 6) union Seq(1, 2, 3)
res31: Seq[Int] = List(2, 3, 4, 5, 6, 1, 2, 3)

distinct

重複要素を取り除いた新しいSeqを返す

scala> Seq(2, 3, 4, 5, 6, 2, 3) distinct
res32: Seq[Int] = List(2, 3, 4, 5, 6)

まとめ

今回は、 コレクションのSeqトレイトのメソッドについて語ってみたけどどうだった?
前回と同じで、やっぱりこの辺は体を動かさないとわからないね。
どのような場面で使うかを想像しながら、ソースを書いて動かしてみてね。

今回も
体で感じてくれたかな?

67
54
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
54