Skip to content

Commit

Permalink
Remove protocol mentions from lists, since it no longer implements th…
Browse files Browse the repository at this point in the history
…e integer access.
  • Loading branch information
José Valim committed Apr 2, 2012
1 parent 8e192d3 commit 9aa4fac
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions getting_started/2.markdown
Expand Up @@ -32,7 +32,7 @@ On the other hand, updating a tuple is expensive as it needs to duplicate the tu


> Note: If you are an Erlang developer, you will notice that we used the `elem` and `setelem` functions instead of Erlang's `element` and `setelement`. The reason for this choice is that Elixir attempts to normalize Erlang API's to always receive the `subject` of the function as the first argument. > Note: If you are an Erlang developer, you will notice that we used the `elem` and `setelem` functions instead of Erlang's `element` and `setelement`. The reason for this choice is that Elixir attempts to normalize Erlang API's to always receive the `subject` of the function as the first argument.
Since updating a tuple is expensive, when we want to iterate, add or remove elements, we use lists. Since lists are linked, it means accessing the first element of the list is very cheap, however, accessing the n-th element will require the algorithm to pass through n-1 nodes before reaching the n-th. We can access the `head` of the list as follows: Since updating a tuple is expensive, when we want to add or remove elements, we use lists. Since lists are linked, it means accessing the first element of the list is very cheap, however, accessing the n-th element will require the algorithm to pass through n-1 nodes before reaching the n-th. We can access the `head` of the list as follows:


iex> [head | tail] = [1,2,3] iex> [head | tail] = [1,2,3]
[1,2,3] [1,2,3]
Expand All @@ -45,18 +45,7 @@ Since updating a tuple is expensive, when we want to iterate, add or remove elem
iex> length [head | tail] iex> length [head | tail]
3 3


In the example above, we have assigned the head of the list to the variable `head` and the tail of the list to the variable `tail`. Finally, notice both lists and tuples implement the access protocol, this allows us to access them using indexes: In the example above, we have assigned the head of the list to the variable `head` and the tail of the list to the variable `tail`. The [`Enum` module](https://github.com/elixir-lang/elixir/blob/master/lib/enum.ex) provides several helpers to manipulate lists (and other enumerables in general) while the [`List` module](https://github.com/elixir-lang/elixir/blob/master/lib/list.ex) provides several helpers specific to lists:

iex> tuple = { :a, :b, :c }
{ :a, :b, :c }
iex> tuple[1]
:a
iex> tuple[-1]
:c
iex> [:a, :b, :c][2]
:b

The [`Enum` module](https://github.com/elixir-lang/elixir/blob/master/lib/enum.ex) provides several helpers to manipulate lists (and other enumerables in general) while the [`List` module](https://github.com/elixir-lang/elixir/blob/master/lib/list.ex) provides several helpers specific to lists:


iex> Enum.map [1,2,3], fn(x) -> x * 2 end iex> Enum.map [1,2,3], fn(x) -> x * 2 end
[2,4,6] [2,4,6]
Expand All @@ -75,7 +64,7 @@ Notice keywords is nothing more than a list of two element tuples, where the fir
iex> [foo: 1, bar: 2] iex> [foo: 1, bar: 2]
[{:bar,2},{:foo,1}] [{:bar,2},{:foo,1}]


The reason for such ordering is faster access when reading and updating the dictionary. Keywords lists also implement the access protocol, which allows a developer to quickly access an atom key: The reason for such ordering is faster access when reading and updating the dictionary. Keywords lists implement the access protocol, which allows a developer to quickly access an atom key:


iex> x = [a: 1, b: 2] iex> x = [a: 1, b: 2]
[{:a, 1}, {:b, 2}] [{:a, 1}, {:b, 2}]
Expand All @@ -84,12 +73,7 @@ The reason for such ordering is faster access when reading and updating the dict
iex> x[:b] iex> x[:b]
2 2


Since keywords lists are lists, using integers access the tuple in the underlying list: We are going to discuss protocols with more details on chapter 4. Deleting, merging and updating keywords lists can also be done with help of the [`Keyword` module](https://github.com/elixir-lang/elixir/blob/master/lib/keyword.ex), so be sure to check it for more information.

iex> [a: 1, b: 2][1]
{:a,1}

Deleting, merging and updating keywords lists can also be done with help of the [`Keyword` module](https://github.com/elixir-lang/elixir/blob/master/lib/keyword.ex), so be sure to check it for more information.


## 2.3 Char lists and binaries ## 2.3 Char lists and binaries


Expand Down

0 comments on commit 9aa4fac

Please sign in to comment.