Scala Tutorialsを読む 28 Immutable collections with var

http://scalatutorials.com/tour/interactive_tour_of_scala_immutable_collections_with_var.html

28 Immutable collections with var

28 varでのイムータブルコレクション

One thing to note is the difference between the +=, +==, -=, -== etc functions regarding mutable and immutable collections.

1つ注意すべき点は+=,+==,-=,-==等の関数のmutableとimmutableコレクションの違いです。

  • For immutable collections - they are not methods of the collection (since it's not modifiable) but if used on an immutable collection declared with var instead of val then the Scala compiler expands them to variable = variable op param. (see on the left for an example)

  • イムータブルなコレクションでは - これらのメソッドはコレクションにありませんが、もしもimmutableコレクションをvalではなくvarを使って宣言した場合はScalaコンパイラはvariable = variable op 引数に展開します。(左の例を見てください)

  • For mutable collections - they are actually methods on the collection, and they modify the collection when used.

  • ムータぶるなコレクションでは - これらのメソッドを持っており、使用した場合はコレクションは更新されます。

Scala Tutorialsを読む 27 Mutable Collections Operations

27 Mutable Collections Operations

ムータブルコレクション演算

http://scalatutorials.com/tour/interactive_tour_of_scala_mutable_collections_operations.html

Mutable collections allow to add / remove a single / multple items while modifying the collection itself

ムータブルなコレクションは1つあるいは複数のアイテムで追加 / 削除の操作で更新出来ます。

the methods +=, +==, -=, -== are actually defined in the mutable collections

+=, +==, -=, -== メソッドはムータブルなコレクションに実際に定義されています。

See Also opens in new page

新しいページを開いて参照

  • BufferLike

Scala Tutorialsを読む 26 Mutable Collections

http://scalatutorials.com/tour/interactive_tour_of_scala_mutable_collections.html

26 Mutable Collections

  • Scala "encourages" using immutable collections (hence they are those used by default) however sometimes mutable collections might have some benefits, either for CPU / memory performance, for code readability or simply a matter of preference.
  • Scala はimmutableコレクションを使う事を"奨励しています"。(ゆえに、デフォルトでそれらをつかようになっている) しかしながらCPU/メモリパフォーマンス、コードの読みやすさもしくは単純に好みで、時にはmutableなコレクションを使った方がいい場合もあるでしょう。
  • As we saw earlier, Scala provides concrete mutable collections in scala.collections.mutable
  • 我々は先に見たように、Scalaは、具体的な変更可能なコレクションをscala.collections.mutableで提供しています。
  • Best practice suggests that you prefix mutable collections with mutable, this might not seem useful for ArrayBuffer, but it will for scala.collections.mutable.Map
  • ベストプラクティスはmutableなコレクションのプリフィックスをmutableのために使う事で、これはArrayBufferより役立つとは思わないかもしれませんが、scala.collections.mutable.Mapを使う事です。
  • ArrayBuffer is the more or less equivalent to java's java.util.ArrayList which is backed by an array
  • ArrayBufferは配列を背後に持つjava.util.ArrayListよりよかったり悪かったりします。
  • ListBuffer is the mutable partner for scala's immutable List, implemented as a Linked List
  • ListBufferはmutableなscalaのimmutable Listのパートナーで、リンクドリストで実装されています。
  • There are many other mutable collections types, see links below for more information
  • 他にも沢山のmutableコレクションタイプがありますので、以下のリンクの情報を見てください

See Also opens in new page

新しいページを開いて参照

  • Concrete Mutable Collection Classes
  • 具象Mutableコレクションクラス
  • What is scala's equivalent of java.util.ArrayList in stackoverflow
  • スタックオーバーフローでの何がjava.util.ArrayListscala環境か。
  • scala.collections.mutable package docs
  • scala.collections.mutable パッケージドキュメント

Scala Tutorialsを読む 25 Maps

http://scalatutorials.com/tour/interactive_tour_of_scala_maps.html

今日は、Map、写像ですね。

  • Maps are constructed simply using Map(key1 -> value1, key2 -> value2, ...)
  • Mapは単純にMap(キー1->値1,キー2->値2,...)を使って構築します。
  • The Map can contain mixed types, but the final type of the Map keys / values will be the lowest common denominator type
  • Mapは型を混ぜて含む事が出来ますが、Mapのキーと値の最終的な型は共通した小限の型になります。
  • The default Map is Predef.Map which points to scala.collection.immutable.Map
  • デフォルトのMapはPredef.Mapで、scala.collection.immutable.Mapへのリンクです。
  • As it currently stands, Map implementation up to size of 4 has a specific class Map1, Map2, - Map3, Map4, beyond that, it uses an immutable HashMap
  • 現在、Mapの実装はサイズが4までは特別なMap1,Map2,Map3,Map4のクラスを使い、それ以降は汎用的な書き換え不能なHashMapを使用します。
  • You can't have duplicate keys, adding a key value pair whose key already exists, overwrites the value
  • キーの集合を複製する事は出来ませんが、キーと値のペアを追加する事で、既に存在しているキーの値を上書きします。
  • Order of iteration is not guaranteed to be consistent
  • イテレーションの順番は保証されません。

See Also opens in new page 新しいページを開いて参照

  • Predef source
  • Predef ソース
  • Predef docs
  • Predef ドキュメント
  • Map's scala docs
  • Mapのscalaドキュメント
  • Map's sources
  • Mapのソース

Scala Tutorialsを読む 24 Sets

http://scalatutorials.com/tour/interactive_tour_of_scala_sets.html

24 Sets 集合

  • Sets are constructed simply using Set(element1, element2, ...)
  • 集合は単純にSet(要素1,要素2,...)を使って構築されます。
  • The Set can contain mixed types, but the final type of the elements will be the lowest common denominator
  • Setは混ぜた型を含む事が出来ますが、最終的な要素の型は共通の小限になります。
  • The default Set is Predef.Set which points to scala.collection.immutable.Set
  • デフォルト集合はPredef.Setにありscala.collection.immutable.Setのリンク貼ってる感じです。
  • As it currently stands, Set implementation up to size of 4 has a specific class Set1, Set2, Set3, Set4, beyond that, it uses an immutable HashSet
  • 現在表したように、Setの実装は4のサイズまでは、特別なクラスSet1,Set2,Set3,Set4を持ち、それ以降はimmutable HashSetを使う。
  • You can't have duplicate values, adding a value that already exists overwrites the value
  • 値はコピー出来ず、値を追加する事は既に存在している値を上書きする
  • Order of iteration is not guaranteed to be consistent
  • イテレーションするオーダーは一貫性を保証するものではない。 See Also opens in new page 新しいページを開いて参照

  • Predef source Predefのソース

  • Predef docs Predefのドキュメント
  • Set's scala docs Scala集合ドキュメント
  • Set's sources 集合のソース

Scala Tutorialsを読む 23 Lists

http://scalatutorials.com/tour/interactive_tour_of_scala_lists.html

ここを読みます。

  • List are constructed simply using List(element1, element2, ...)
  • ListはシンプルにList(要素1,要素2,...)を使って作成される
  • List elements can be of any type, but the List final type will be the lowest common denominator
  • Listの要素はいかなる型を取る事が出来ますが、Listの最終的な型は共通の小限の型になります。
class Foo(val value1:Int)
class Bar(value1:Int, val value2:Int) extends Foo(value1)
val list:List[Foo] = List(new Foo(1), new Bar(2,3))
  • The default List in scala, is scala.List which points to scala.collection.immutable.List src docs, and defined in scala/package.scala src docs
  • Scalaでのデフォルトのリストはscala.Listで、scala.collection.immutable.Listのソースドキュメントとscala/package.scalaソースドキュメントで定義されています。
  • The default List is implemented as a Linked list
  • デフォルトリストはリンクリストで実装されています。
  • It is immutable (any "changes" craete a new list, the original is untouched)
  • これは書き換え不能です(変更するには元のリストは変えずに、新しいリストを作成します)

See Also opens in new page

新しいページを開いて参照

  • List's scala docs
  • リストのscalaドキュメント
  • scala package object docs scala
  • パッケージオブジェクトドキュメント

Scala Tutorialsを読む 22-3

http://scalatutorials.com/tour/interactive_tour_of_scala_arrays.html

Since they are using Java's arrays, to print nicely an Array's content, use .mkString(",") Javaの配列を使っているので、配列は.mkString(",")を使ってプリント出来ます。

Array elements can be of any type, but the Array's final type will be the lowest common denominator

配列の要素はどんな型でも使えますが、配列の最終的な型は最小の共通部分になります。

See Also opens in new page

新しいページを開いて参照

  • Array's scala docs
  • scalaドキュメントの配列

配列読み終えました。