Skip to content

Add space around vertical bar #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The idea of the parallel compiler is very simple: for each file we want to compi

In Elixir, we could write this code as follows:

def spawn_compilers([current|files], output) do
def spawn_compilers([current | files], output) do
parent = Process.self()
child = spawn_link(fn ->
:elixir_compiler.file_to_path(current, output)
Expand All @@ -32,7 +32,7 @@ In Elixir, we could write this code as follows:
:done
end

In the first line, we define a function named `spawn_compilers` that receives two arguments, the first is a list of files to compile and the second is a string telling us where to write the compiled file. The first argument is represented as a list with head and tail (`[current|files]`) where the top of the list is assigned to `current` and the remaining items to `files`. If the list is empty, the first clause of `spawn_compilers` is not going to match, the clause `spawn_compilers([], _output)` defined at the end will instead.
In the first line, we define a function named `spawn_compilers` that receives two arguments, the first is a list of files to compile and the second is a string telling us where to write the compiled file. The first argument is represented as a list with head and tail (`[current | files]`) where the top of the list is assigned to `current` and the remaining items to `files`. If the list is empty, the first clause of `spawn_compilers` is not going to match, the clause `spawn_compilers([], _output)` defined at the end will instead.

Inside `spawn_compilers`, we first retrieve the PID of the current process with `Process.self` (remember we are talking about Erlang processes/actors and not OS processes) and then proceed to spawn a new process to execute the given function in parallel. Spawning a new process is done with the `spawn_link` function.

Expand Down Expand Up @@ -121,13 +121,13 @@ Notice that we have two small additions. First we store the `:elixir_parent_comp

Second, our main process can now receive a new `{ :waiting, child, module }` message, so we need to extend it to account for those messages. Not only that, we need to control which PIDs we have spawned so we can notify them whenever a new module is compiled, forcing us to add a new argument to the `spawn_compilers` function. `spawn_compilers` would then be rewritten as follows:

def spawn_compilers([current|files], output, stack) do
def spawn_compilers([current | files], output, stack) do
parent = Process.self()
child = spawn_link(fn ->
:elixir_compiler.file_to_path(current, output)
send parent, { :compiled, Process.self() }
end)
wait_for_messages(files, output, [child|stack])
wait_for_messages(files, output, [child | stack])
end

# No more files and stack is empty, we are done
Expand Down
4 changes: 2 additions & 2 deletions _posts/2013-05-23-elixir-v0-9-0-released.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defimpl Enumerable, for: List do
do_reduce(list, acc, fun)
end

defp do_reduce([h|t], acc, fun) do
defp do_reduce([h | t], acc, fun) do
do_reduce(t, fun.(h, acc), fun)
end

Expand All @@ -101,7 +101,7 @@ The `Enum.map/2` we have used above is now implemented in terms of this reducing
defmodule Enum do
def map(collection, fun) do
Enumerable.reduce(collection, [], fn(x, acc) ->
[fun.(x, acc)|acc]
[fun.(x, acc) | acc]
end) |> reverse
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function.
```elixir
defmodule Interleave do
def interleave(a, b) do
step = fn x, acc -> { :suspend, [x|acc] } end
step = fn x, acc -> { :suspend, [x | acc] } end
af = &Enumerable.reduce(a, &1, step)
bf = &Enumerable.reduce(b, &1, step)
do_interleave(af, bf, []) |> :lists.reverse()
Expand Down
4 changes: 2 additions & 2 deletions crash-course.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ Pattern matching in Elixir is based on Erlang's implementation and in general is
**Erlang**

```erlang
loop_through([H|T]) ->
loop_through([H | T]) ->
io:format('~p~n', [H]),
loop_through(T);

Expand All @@ -472,7 +472,7 @@ loop_through([]) ->
**Elixir**

```elixir
def loop_through([h|t]) do
def loop_through([h | t]) do
IO.inspect h
loop_through t
end
Expand Down
2 changes: 1 addition & 1 deletion getting-started/basic-types.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ What is the difference between lists and tuples?
Lists are stored in memory as linked lists, meaning that each element in a list holds its value and points to the following element until the end of the list is reached. We call each pair of value and pointer a **cons cell**:

```iex
iex> list = [1|[2|[3|[]]]]
iex> list = [1 | [2 | [3 | []]]]
[1, 2, 3]
```

Expand Down
2 changes: 1 addition & 1 deletion getting-started/meta/domain-specific-languages.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule TestCase do
function_name = String.to_atom("test " <> description)
quote do
# Prepend the newly defined test to the list of tests
@tests [unquote(function_name)|@tests]
@tests [unquote(function_name) | @tests]
def unquote(function_name)(), do: unquote(block)
end
end
Expand Down
2 changes: 1 addition & 1 deletion getting-started/mix-otp/agent.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ And play a bit with agents:
```iex
iex> {:ok, agent} = Agent.start_link fn -> [] end
{:ok, #PID<0.57.0>}
iex> Agent.update(agent, fn list -> ["eggs"|list] end)
iex> Agent.update(agent, fn list -> ["eggs" | list] end)
:ok
iex> Agent.get(agent, fn list -> list end)
["eggs"]
Expand Down
6 changes: 3 additions & 3 deletions getting-started/pattern-matching.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ iex> tail
Similar to the `hd/1` and `tl/1` functions, we can't match an empty list with a head and tail pattern:

```iex
iex> [h|t] = []
iex> [h | t] = []
** (MatchError) no match of right hand side value: []
```

Expand All @@ -111,7 +111,7 @@ The `[head | tail]` format is not only used on pattern matching but also for pre
```iex
iex> list = [1, 2, 3]
[1, 2, 3]
iex> [0|list]
iex> [0 | list]
[0, 1, 2, 3]
```

Expand Down Expand Up @@ -162,7 +162,7 @@ iex> {x, x} = {1, 2}
In some cases, you don't care about a particular value in a pattern. It is a common practice to bind those values to the underscore, `_`. For example, if only the head of the list matters to us, we can assign the tail to underscore:

```iex
iex> [h|_] = [1, 2, 3]
iex> [h | _] = [1, 2, 3]
[1, 2, 3]
iex> h
1
Expand Down
10 changes: 5 additions & 5 deletions getting-started/recursion.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Let's now see how we can use the power of recursion to sum a list of numbers:

```elixir
defmodule Math do
def sum_list([head|tail], accumulator) do
def sum_list([head | tail], accumulator) do
sum_list(tail, head + accumulator)
end

Expand All @@ -68,9 +68,9 @@ end
IO.puts Math.sum_list([1, 2, 3], 0) #=> 6
```

We invoke `sum_list` with the list `[1, 2, 3]` and the initial value `0` as arguments. We will try each clause until we find one that matches according to the pattern matching rules. In this case, the list `[1, 2, 3]` matches against `[head|tail]` which binds `head` to `1` and `tail` to `[2, 3]`; `accumulator` is set to `0`.
We invoke `sum_list` with the list `[1, 2, 3]` and the initial value `0` as arguments. We will try each clause until we find one that matches according to the pattern matching rules. In this case, the list `[1, 2, 3]` matches against `[head | tail]` which binds `head` to `1` and `tail` to `[2, 3]`; `accumulator` is set to `0`.

Then, we add the head of the list to the accumulator `head + accumulator` and call `sum_list` again, recursively, passing the tail of the list as its first argument. The tail will once again match `[head|tail]` until the list is empty, as seen below:
Then, we add the head of the list to the accumulator `head + accumulator` and call `sum_list` again, recursively, passing the tail of the list as its first argument. The tail will once again match `[head | tail]` until the list is empty, as seen below:

```elixir
sum_list [1, 2, 3], 0
Expand All @@ -87,8 +87,8 @@ What if we instead want to double all of the values in our list?

```elixir
defmodule Math do
def double_each([head|tail]) do
[head * 2|double_each(tail)]
def double_each([head | tail]) do
[head * 2 | double_each(tail)]
end

def double_each([]) do
Expand Down