1 [h1]
EN: Maps
JA: マップ

2 [p]
EN: A map is an associative collection. It holds a set of entries, each of which
maps a key to a value. The same data structure has a variety of names in
other languages: hash table, dictionary, association, table, etc.
JA: マップは連想コレクションです。キーを値に対応付けるエントリーの集まりを格納します。同じデータ構造には、ほかの言語でハッシュテーブル、辞書、連想配列、テーブルなど、さまざまな名前が付いています。

3 [p]
EN: You can create a map by placing a series of comma-separated entries inside
curly braces. Each entry is a key and a value separated by a colon:
JA: コンマで区切った一連のエントリーを波括弧で囲むと、マップを作成できます。各エントリーは、コロンで区切ったキーと値です。

UNCHANGED CODE 1

{
  "maple":  "Sugar Maple (Acer Saccharum)",
  "larch":  "Alpine Larch (Larix Lyallii)",
  "oak":    "Red Oak (Quercus Rubra)",
  "fir":    "Fraser Fir (Abies Fraseri)"
}


4 [p]
EN: This creates a map that associates a type of tree (key) to a specific 
tree within that family (value). Syntactically, in a map literal, keys 
can be any literal, a variable name, or a parenthesized expression. 
Values can be any expression. Here, we’re using string literals for both keys 
and values.
JA: これは、木の種類（キー）を、その仲間の特定の木（値）に対応付けるマップを作成します。構文上、マップリテラルのキーには、任意のリテラル、変数名、丸括弧で囲んだ式を使えます。値には任意の式を使えます。この例では、キーと値の両方に文字列リテラルを使っています。

5 [p]
EN: Semantically, values can be any object, and multiple keys may map to the same
value. 
JA: 意味上は、値にどんなオブジェクトでも使え、複数のキーを同じ値に対応付けることもできます。

6 [p]
EN: Keys have a few limitations. They must be one of the immutable built-in
value types in Wren. That means a number, string, range, bool, or null.
You can also use a class object as a key (not an instance of that class, 
the actual class itself).
JA: キーにはいくつかの制限があります。Wrenに組み込まれた不変の値型のいずれかでなければなりません。つまり、数値、文字列、範囲、真偽値、またはnullです。クラスオブジェクトもキーに使えます（そのクラスのインスタンスではなく、クラスそのものです）。

7 [p]
EN: The reason for this limitation—and the reason maps are called “hash
tables” in other languages—is that each key is used to generate a numeric
hash code. This lets a map locate the value associated with a key in constant
time, even in very large maps. Since Wren only knows how to hash certain
built-in types, only those can be used as keys.
JA: この制限がある理由、そしてほかの言語でマップを「ハッシュテーブル」と呼ぶ理由は、各キーから数値のハッシュコードを生成するためです。これにより、とても大きなマップでも、キーに対応する値を定数時間で見つけられます。Wrenは特定の組み込み型のハッシュ方法しか知らないので、キーに使えるのもそれらだけです。

8 [h2]
EN: Adding entries #
JA: エントリーの追加 #

9 [p]
EN: You add new key-value pairs to the map using the subscript operator:
JA: 新しいキーと値の組をマップに追加するには、添字演算子を使います。

UNCHANGED CODE 2

var capitals = {}
    capitals["Georgia"] = "Atlanta"
    capitals["Idaho"] = "Boise"
    capitals["Maine"] = "Augusta"


10 [p]
EN: If the key isn’t already present, this adds it and associates it with the given
value. If the key is already there, this just replaces its value.
JA: キーがまだ存在しなければ、そのキーを追加して指定した値に対応付けます。キーがすでに存在すれば、その値を置き換えるだけです。

11 [h2]
EN: Looking up values #
JA: 値の検索 #

12 [p]
EN: To find the value associated with some key, again you use your friend the
subscript operator:
JA: キーに対応する値を見つけるときも、おなじみの添字演算子を使います。

UNCHANGED CODE 3

System.print(capitals["Idaho"]) //> Boise


13 [p]
EN: If the key is present, this returns its value. Otherwise, it returns null. Of
course, null itself can also be used as a value, so seeing null here
doesn’t necessarily mean the key wasn’t found.
JA: キーが存在すれば、その値を返します。存在しなければ、nullを返します。もちろん、null自体も値として使えるため、ここでnullが得られても、必ずしもキーが見つからなかったとは限りません。

14 [p]
EN: To tell definitively if a key exists, you can call containsKey():
JA: キーが存在するかを確実に調べるには、containsKey()を呼び出せます。

UNCHANGED CODE 4

var capitals = {"Georgia": null}

System.print(capitals["Georgia"]) //> null (though key exists)
System.print(capitals["Idaho"])   //> null 
System.print(capitals.containsKey("Georgia")) //> true
System.print(capitals.containsKey("Idaho"))   //> false


15 [p]
EN: You can see how many entries a map contains using count:
JA: countを使うと、マップに含まれるエントリーの数を確認できます。

UNCHANGED CODE 5

System.print(capitals.count) //> 3


16 [h2]
EN: Removing entries #
JA: エントリーの削除 #

17 [p]
EN: To remove an entry from a map, call remove() and pass in the key for the
entry you want to delete:
JA: マップからエントリーを削除するには、remove()を呼び出し、削除したいエントリーのキーを渡します。

UNCHANGED CODE 6

capitals.remove("Maine")
System.print(capitals.containsKey("Maine")) //> false


18 [p]
EN: If the key was found, this returns the value that was associated with it:
JA: キーが見つかれば、それに対応していた値を返します。

UNCHANGED CODE 7

System.print(capitals.remove("Georgia")) //> Atlanta


19 [p]
EN: If the key wasn’t in the map to begin with, remove() just returns null.
JA: もともとマップにキーがなかった場合は、remove()はnullを返すだけです。

20 [p]
EN: If you want to remove everything from the map, like with lists, you call
clear():
JA: マップからすべてを削除したい場合は、リストと同様、clear()を呼び出します。

UNCHANGED CODE 8

capitals.clear()
System.print(capitals.count) //> 0


21 [h2]
EN: Iterating over the contents #
JA: 内容の反復処理 #

22 [p]
EN: The subscript operator works well for finding values when you know the key
you’re looking for, but sometimes you want to see everything that’s in the map.
You can use a regular for loop to iterate the contents, and map exposes two 
additional methods to access the contents: keys and values. 
JA: 探しているキーが分かっている場合、添字演算子は値を見つけるのに適しています。しかし、マップのすべての内容を見たいこともあります。通常のforループで内容を反復処理できます。また、マップは内容にアクセスするため、keysとvaluesという二つのメソッドも公開しています。

23 [p]
EN: The keys method on a map returns a Sequence that iterates over all of
the keys in the map, and the values method returns one that iterates over the values.
JA: マップのkeysメソッドは、すべてのキーを反復処理するSequenceを返します。valuesメソッドは、値を反復処理するSequenceを返します。

24 [p]
EN: Regardless of how you iterate, the order that things are iterated in 
isn’t defined. Wren makes no promises about what order keys and values are 
iterated. All it promises is that every entry will appear exactly once.
JA: どの方法で反復処理しても、その順序は定義されていません。Wrenは、キーや値をどの順番で反復するかを保証しません。保証するのは、すべてのエントリーがそれぞれ一度だけ現れることです。

25 [p]
EN: Iterating with for(entry in map) 
When you iterate a map with for, you’ll be handed an entry, which contains
a key and a value field. That gives you the info for each element in the map.
JA: for(entry in map)による反復処理forでマップを反復処理すると、keyフィールドとvalueフィールドを持つエントリーを受け取ります。これで、マップ内の各要素の情報が得られます。

UNCHANGED CODE 9

var birds = {
  "Arizona": "Cactus wren",
  "Hawaii": "Nēnē",
  "Ohio": "Northern Cardinal"
}

for (bird in birds) {
  System.print("The state bird of %(bird.key) is %(bird.value)")
}


26 [p]
EN: Iterating using the keys 
JA: キーを使った反復処理

27 [p]
EN: You can also iterate over the keys and use each to look up its value:
JA: キーを反復処理し、それぞれのキーで値を検索することもできます。

UNCHANGED CODE 10

var birds = {
  "Arizona": "Cactus wren",
  "Hawaii": "Nēnē",
  "Ohio": "Northern Cardinal"
}

for (state in birds.keys) {
  System.print("The state bird of %(state) is " + birds[state])
}


28 [p]
EN: 
Method Calls →
← Lists
JA: メソッド呼び出し →← リスト
