This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Ticker do | |
@interval 2000 | |
@name :ticket | |
def start do | |
pid = spawn(__MODULE__, :generator, [[]]) | |
:global.register_name(@name, pid) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Parallel do | |
def pmap(collection, fun) do | |
me = self | |
collection | |
|> Enum.map(fn (elem) -> | |
spawn_link fn -> (send me, { self, fun.(elem) }) end | |
end) | |
|> Enum.map(fn (pid) -> | |
receive do { ^pid, result } -> result end | |
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Spawn1 do | |
def greet do | |
receive do | |
{sender, msg} -> | |
send sender, { :ok, "Hello, #{msg}" } | |
greet | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Chain do | |
def counter(next_pid) do | |
receive do | |
n -> | |
send next_pid, n + 1 | |
end | |
end | |
def create_processes(n) do | |
last = Enum.reduce 1..n, self, |
Creating a project tree:
mix new <project name>
Running tests:
mix test
Represent numbers, names, ranges and regular expressions.
Can be written as:
- decimal (1234).
- Once a variable a list such as
[1, 2 ,3]
you know it will always reference those same variables (until you rebind the variable). - What if you need to add 100 to each element in
[1, 2, 3]
? Elixir produces a copy of the original, containing the new values.
This uses the [ head | tail ]
operator to build a new list with head
as the first element and tail
as the rest:
list1 = [3, 2, 1]
- String concatenation:
=
is not assignment in Elixir. More like an assertion.- Succeeds if it can find a way of making left side equal to right side.
a = 1 #1
a + 3 #1
In thise cases the lef side is a variable and right side is an integer literal. So to make the match it binds the variable to the integer.
NewerOlder