Scala ã® Collection ã®ä½¿ãåã
Scalaã§éçºããã¦ããã¨æ§ã
ãªå ´é¢ã§ Collection ã使ããã¨ãããã¨æãããCollection ã¨è¨ã£ã¦ãScalaã«ã¯å¤ãã®ç¨®é¡ã®ã¯ã©ã¹ãåå¨ããã
ãããããã®ç¨®é¡ã¯ããªãå¤ãããã«åé¡ã大å¤ã ã
ã¾ããmutableããªã®ã imutableã
ããã« IndexedSecããªã®ã LinearSec ãªã®ãã§ã¾ã£ããç¨éã使ãæ¹ãéãã
ç§èªèº«ãããã ããJava ããæ¥ã人ã¯è¦ãããã¨å¤ãçµæ§æ¸æã£ã¦ãã¾ãã®ã§ã¯ãªãã ãããã
ã»scala.collection.immutable ããã±ã¼ã¸å
ã®å
¨ã¦ã®ã³ã¬ã¯ã·ã§ã³
ã»scala.collection.mutable ããã±ã¼ã¸å
ã®å
¨ã¦ã®ã³ã¬ã¯ã·ã§ã³
ä¸è¨ã®åã¯ã©ã¹ã®ã¡ã½ããã®ç¨®é¡ãå 容ãç´¹ä»ãã¦ããè¨äºãããã°ã¯å¤ãã®ã ããã©ã®ã¯ã©ã¹ãã©ã®å ´åã«ä½¿ãã°ããã®ããã©ããã£ãç¨éã«ãã£ã¦ããããã¾ã¨ããå 容ãè¦å½ãããªãã£ãã®ã§ãä»åã©ããã£ãå ´åã«ã©ã®ã¯ã©ã¹ã使ãã¹ãããç°¡åãªä¾ã¨ä¸ç·ã«ã¾ã¨ãã¦ã¿ãã
種é¡ãå¤ããã¨ããããä»åã¯ä½¿ããã¨ãå¤ã immutable 㪠Seq ãã¡ã¤ã³ã«ã¾ã¨ãã¦ã¿ãã
Seq ã¨ã¯ä½ã
Scala Documentaion ã«ã¯ä¸è¨ã®ããã«è¨è¿°ãã¦ããã
å (Seq) ãã¬ã¤ãã¯ãé·ã (length) ããããããããã®è¦ç´ ã« 0 ããæ°ããããåºå®ãããæ·»å (index) ããã Iterable ã®ä¸ç¨®ã ã
Seq ãã¬ã¤ãã«ã¯ LinearSeq 㨠IndexedSeq ã¨ããäºã¤ã®åãã¬ã¤ããããã
ãããã¯æ°ããæ¼ç®ãå®ç¾©ããªãããããããç°ãªã£ãæ§è½ç¹æ§ããã¤ã
ç·å½¢å (linear sequence) ã¯å¹çç㪠head 㨠tail æ¼ç®ãæã¡ãä¸æ¹æ·»åä»ãå (indexed sequence) ã¯å¹ççãªapplyãlengthãããã³ (å¯å¤ã®å ´å) update æ¼ç®ãæã¤ã
ã¤ã¾ã㯠Seq ã¨ãããã¬ã¤ãã¯ãªã¹ããé
åã¿ãããªãã®ã§ã2種é¡ã®åãã¬ã¤ããããããããã次ã®ãããªç¹å¾´ãããã
â IndexSeq
ä»»æã®å ´æã®è¦ç´ ãèªã¿è¾¼ãã ããå¤æ´ãããããã®ã«åãã¦ããã
â LineardexSeq
å é é ç®ãæ«å°¾é ç®ã®è¿½å ãåé¤ãé«éã ããã©ã³ãã ã¢ã¯ã»ã¹ãé ãã®ã§å é ããé çªã«ä»»æã®å¦çãã¦ããã®ã«åãã¦ããã
- head/tailãéã
- Java ã§ããé åãLinkedList
ã§ã¯ãç¶ãã¦IndexSeqãLineardexSeqãã®ç¶æ¿ããã¦ããåã¯ã©ã¹ã®å
容ã¨ä½¿ãéãè¦ã¦ãããã
IndexSeq
ã»Vector
Scala 2.8 ããå°å ¥ãããæ°ããã³ã¬ã¯ã·ã§ã³ã¯ã©ã¹ã
Vectorã¯ã©ã¹ã®ã³ã¡ã³ãã«ãããããã«ãã©ã³ãã ã¢ã¯ã»ã¹ã¨æ´æ°ãå¾æãªã®ã§
åºæ¬çã«ã¯ãããããã©ã«ãã§ä½¿ã£ã¦è¯ãããã ã
Because vectors strikea good balance between fast random selections and fast random functional updates, they are currently the default implementation of immutable indexed sequences.
Javaã§ããã¨ããã®ArrayListã¿ãããªãã®ã ã
å®éã«IndexedSecã®ããã©ã«ãã¯Vectorãçæããããã«ãªã£ã¦ããã
scala> IndexedSeq(1,2,3) res1: IndexedSeq[Int] = Vector(1, 2, 3)
ã»Range
çééã§ä¸¦ã¹ãããã½ã¼ãæ¸ã¿ã®æ´æ°ã·ã¼ã±ã³ã¹ã
ã¡ãªã¿ã«Range以å¤ã§ç¯å²ã欲ããå ´åã«ã次ã®ããã«rangeã¡ã½ããã使ããã¨ãå¯è½ã ã
scala> Vector.range(1,5) res1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4)
ç¯å²ã使ãå ´åã«éã£ã¦ã¯Rangeãä¸çªã¤ã³ã¹ã¿ã³ã¹çæãæ©ããããã
Scala Seqメモ(Hishidama's Scala Seq Memo)
scala> Range(1 , 5) res27: scala.collection.immutable.Range = Range(1, 2, 3, 4)
ã»NumericRange
Rangeã®æ±ç¨çã
Rangeã®å ´åã¯Intããæ±ããªããããã以å¤ã® Char,BigDecimal,Long ãªã©ãæ±ãã.
scala> 'a' to 'd' res1: scala.collection.immutable.NumericRange.Inclusive[Char] = NumericRange(a, b, c, d) scala> 1L to 5L res25: scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(1, 2, 3, 4, 5)
以ä¸ãããã¾ã§ãç°¡åã«ã¾ã¨ããã¨
- Intåã®ç¯å²ã欲ããå ´åã¯Rangeã使ç¨
- Intå以å¤ã®ç¯å²ã欲ããå ´åNumericRangeã使ç¨
- head/tailã®å¦çãã¡ã¤ã³ã§ãªããã©ã³ãã ã¢ã¯ã»ã¹ãæ´æ°å¦çï¼å®éã«ã¯æ°ããã¤ã³ã¹ã¿ã³ã¹ãçæï¼ãè¡ããã¨ãå¤ãå ´åãã¨ããããç¯å²ãã¤ãããªãå ´å以å¤ã¯åºæ¬çã«ã¯Vectorã使ç¨
ã§ã¯ãç¶ãã¦ãLinearSeq ã®æ¹ãè¦ã¦è¡ããã
LinearSeq
ã»Stream
ãªã¹ãã«ä¼¼ã¦ããããè¦ç´ ã¯é
延è©ä¾¡ãããã®ã§ãç¡éã®é·ãããã¤ãã¨ãã§ããã
ãã以å¤ã¯ List ã¨åãæ§è½ç¹æ§ããã¤ã
使ãããã¾ã§å¤ãè©ä¾¡ããªãã®ã§æå¾ã¯ "?" ã¨ãªã£ã¦ããã
scala> val stream = 1 #:: 2 #:: 3 #:: Stream.empty stream: scala.collection.immutable.Stream[Int] = Stream(1, ?)
ä¸è¨ããï¼val stream = (1 to 100000000).toStreamï¼ã¨ãã£ãé常ã«å¤§ããªè¦ç´ æ°ããã¤ãªã¹ãã§ã tream.tail ã¨ãã£ãå¦çã¯ä¸ç¬ã§è¿ã£ã¦ããã(mapãfilterãåæ§)
大ããªè¦ç´ æ°ãæã¤ãªã¹ããæä½ãããã¨ãã«æå¹ã
â»ãã ããstream.max, stream.size, stream.sum ã¨ãã£ã Transformer method(â»1) ã§ãªããã®ã¯
java.lang.OutOfMemoryErrorããå¼ãèµ·ããã®ã§æ³¨æãããã
ã»Stack
å¾å
¥ãå
åºã(LIFO) ã®åã
æ©è½çã«ãªã¹ãã¨ãã¶ãã®ã§ä½¿ããããã¨ã¯ãã¾ããªãã
Stack ã® push 㯠List ã® :: ã¨åãã§ãpop 㯠List ã® tail ã¨åãã
scala> import scala.collection.immutable.Stack import scala.collection.immutable.Stack scala> val stack = Stack(1, 2, 3) stack: scala.collection.immutable.Stack[Int] = Stack(1, 2, 3)
ã»Queue
Stackã¨ä¼¼ã¦ããããStackã¨éã£ã¦å å ¥ãå åºã (FIFO: first in first out) ã®å
scala> import scala.collection.immutable.Queue import scala.collection.immutable.Queue scala> val queue = Queue(1, 2, 3) queue: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)
ã»List
This class is optimal for last-in-first-out (LIFO), stack-like access patterns. If you need another access
pattern, for example, random access or FIFO, consider using a collection more suited to this than `List`.
List.scala ã®ã³ã¡ã³ãã«ãããããã« LIFO ã head/tail ã®å¦çãã¡ã¤ã³ã®å ´åã«ä½¿ãã¹ãã
ã©ã³ãã ã¢ã¯ã»ã¹ãªã©ã®å¦çã使ãå ´å㯠List 以å¤ã使ãããã«ãããã¨ã
ã¡ãªã¿ã«ã«Seqã®ããã©ã«ãã¯Listãçæãããã¨ãã List ã§ã¯ãªã Seq ã使ãã®ãä¸è¬çã®ããã ã
qiita.com
scala> Seq(1,2,3) seq: Seq[Int] = List(1, 2, 3)
以ä¸ãLinearSeq ã«ã¤ãã¦ããã¾ã§ãç°¡åã«ã¾ã¨ããã¨
- Stack, Queue ã«ã¤ãã¦ã¯ åãæ©è½ãæã¤ListãVectorããã§ã«åå¨ãããã¨ããããã¾ãæèããå¿ è¦ã¯ãªãã
- ã³ã¬ã¯ã·ã§ã³ã®è¦ç´ æ°ãé常ã«å¤§ããå ´åãé 延è©ä¾¡ãè¡ããªã¹ãã使ãããå ´åã¯ãStreamã使ãã¨è¯ãã
- head/tail ã®å¦çãã¡ã¤ã³ã®å ´å㯠List ã使ãã¨ããããSeq ããªãã¹ã使ãããã«ãããã
ãã
ã
ã
ã¾ã¨ã
以ä¸ãåã³ã¬ã¯ã·ã§ã³ã®å 容ãç¹å¾´ãè¦ã¦ããããããã©ã¼ãã³ã¹ãç¨éã®è¦³ç¹ã使ãããããã Collection ãé¸æããå ´åããã¯ã Vector ã使ãã®ãä¸è¬çãã¤ç¡é£ã®ããã ã
ä¸è¨ã®è¨äºãããã¥ã¡ã³ããæ¯éåèã«ãã¦æ¬²ããã
- Listã®ä¸»ãªå¦çãç·å½¢ãªã®ã«å¯¾ãã¦ãVectorã¯å ¨ã¦å®è³ªå®æ°ã
ãCollections - 性能特性 - Scala Documentation
- å®éã«ããã©ã¼ãã³ã¹ãè¨æ¸¬ãããã®ã ãããã¯ã Vector ã®ä½¿ç¨ãé²ãã¦ããã
åè
Collections - はじめに - Scala Documentation
Scalaでリスト処理
When should I choose Vector in Scala? - Stack Overflow
Scalaコレクションメモ(Hishidama's Scala collection Memo)
Scala Cookbook: Recipes for Object-Oriented and Functional Programming: Alvin Alexander: 9781449339616: Amazon.com: Books
Amazon.co.jp: Scalaスケーラブルプログラミング第2版: Martin Odersky, Lex Spoon, Bill Venners, 羽生田 栄一, 水島 宏太, 長尾 高弘: 本
â»1 Transformer method
Transformer method ã¨ã¯ååã®éãä¸ããããã³ã¬ã¯ã·ã§ã³ãæ°ããã³ã¬ã¯ã·ã§ã³ã«å¤æããã³ã¬ã¯ã·ã§ã³ã¡ã½ããã®ãã¨ãæãã
map, filter, reverse ãªã©ã¯ä¸ããããã³ã¬ã¯ã·ã§ã³ãæ°ããã³ã¬ã¯ã·ã§ã³ã«å¤æããã®ã§ Transformer method ã§ããããmax, size, sum ãªã©ã¯ä¸è¨ã®å®ç¾©ã«å½ã¦ã¯ã¾ããªãã®ã§ããã§ã¯ãªãã