elixir KERNEL 4 Flashcards

1
Q

defmodule Sum do

def add(pid, x, y) do

send(pid, x + y)

end

end

spawn(Sum, :add, [self(), 1, 2])

receive do val -> val end

A

=> 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pets = %{

“cat” => %{age: 1},

“dog” => %{age: 3}

}

pets[“cat”][:age] |> update_in(&(&1 + 1))

A

=> %{

“cat” => %{age: 2},

“dog” => %{age: 3}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

c = %{age: 5}

v = %{age: 20}

auto = %{“car” => c, “van” => v}

add1 = &(&1 + 1)

auto[“van”].age |> update_in(add1)

A

%{“car” => %{age:5}, “van” => %{age: 21}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

e = %{age: 4}

m = %{age: 7}

dogs = %{ellie: e, max: m}

update_in(dogs.max.age, &(&1 + 1))

A

%{ellie: %{age:4}, max: %{age: 8}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

b = %{age: 1}

c = %{age: 2}

toys = %{“bear” => b, “car” => c}

plus1 = &(&1 + 1)

toys |> update_in([“car”, :age], plus1)

A

%{“bear” => %{age: 1}, %{“car” => %{age: 3}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

defmodule Add do

def inc(pid, x),

do: send(pid, x + 1)

end

spawn_link(Add, :inc, [self(), 1])

receive do

val -> val

end

A

=> 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

b = %{age: 1}

a = %{age: 2}

bots = %{“bob” => b, “amy” => a}

bots[“bob”][:age] |> get_and_update_in(&{&1, &1+1})

A

=> %{“bob” => %{age: 2}, “amy” => %{age: 2}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

abs(-1.11)

A

=> 1.11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

abs(28)

A

=> 28

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ceil(-1.25)

A

=> -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

floor(3.55)

A

=> 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

1 in {1, 2, 3}

A

** (Protocol.UndefinedError)

protocol Enumerable not implemented for {1, 2, 3}

*Note: Tuple is a basic type,

but in/2 expects an enumerable on the right side

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

1 in [1, 2] == Enum.member?([1, 2], 1)

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

x = 1

case x do

x when x in [2, 3] -> “boo”

x when x in 0..2 -> “foo”

_x => “whatevs”

end

A

=> “foo”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

c = %{age: 30}

n = %{age:20}

jobs = %{“cop” => c, “nun” => n}

jobs[“cop”].age |> get_and_update_in(&(&1, &1+1))

A

=> {30, “cop” => %{age: 31}, “nun” => %{age: 20}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

is_map_key(%{a: 1}, :a)

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

1 not in [1, 2, 3]

A

=> false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

defmodule Feeling do

def love, do: “love you!”

end

Feeling.love()

A

=> “love you!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

is_map_key(:a, %{a: 1})

A

BadMapError

*Note: is_map_key/2 expects a map as a first argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

pets = %{amy: %{age: 7}, mia: %{age: 8}}

pets.amy.age |> get_and_update_in(&(&1, &1 + 1))

A

=> {7, %{amy: %{age: 8}, mia: %{age: 8}}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

is_struct(URI.parse(“/”))

A

=> true

eg: URI.parse(“https://hexdocs.pm/elixir/Kernel.html#content”) =>

%URI{
authority: “hexdocs.pm”,
fragment: “content”,
host: “hexdocs.pm”,
path: “/elixir/Kernel.html”,
port: 443,
query: nil,
scheme: “https”,
userinfo: nil
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

is_struct(%{})

23
Q

defmodule Lang do

def desc(a, b) do

”#{a} #{b}”

end

end

Lang.desc(“elixir”, “cool”)

A

=> “elixir cool”

24
Q

throw “hello”

A

** (throw) “hello”

25
defmodule Inc do def add(x, inc\_by \\1) do x + inc\_by end end Inc.add(1)
=\> 2 \*Note: Inc.add(3, 5) =\> 8
26
defmodule Title do def name(name, title \\ "") def name(name, title) do title \<\> name end end Title.name("Smith", "Dr."
=\> "Dr.Smith" \*Note: if only 1 argument is provided, the first def is called and name and default title are fed into the second def because there is no instruction block defined by the first def. Therefore, Title.name("Johnson") =\> "Johnson"
27
all = fn :get\_and\_update, data, next -\> Enum.map(data, next) end get\_and\_update\_in( [%{x: 1, y: 0}, %{x: 0, y: 0}], [all, :x], &{&1, &1 +1} )
=\> [ {1, %{x: 2, y: 0}}, {0, %{x: 1, y: 0}} ]
28
try do throw(:catch\_this) catch :throw, :catch\_this -\> :it\_was\_caught end
=\> :it\_was\_caught
29
defmodule Title do def name(name, title \\ "") def name(name, title) do title \<\> name end end Title.name("Smith")
=\> "Smith"
30
try do throw(:tantrum) catch :throw, value -\> value after IO.puts "Are you ok?" end
=\> :tantrum \*Note: "Are you ok?" would be printed before returning the value of throw.
31
defmodule End do def perform(reason) do exit reason catch :exit, _ -\> "Or keep going?" end end End.perform("Give up")
=\> "Or keep going?"
32
try do throw(:ball) catch x -\> x end
:ball
33
try do throw(:tantrum) catch \_value -\> \_\_STACKTRACE\_\_ end
Returns a stacktrace Example from my particular iex session: [ {:erl\_eval, :do\_apply, 6, [file: 'erl\_eval.erl', line: 680]}, {:erl\_eval, :try\_clauses, 8, [file: 'erl\_eval.erl', line: 914]}, {:elixir, :recur\_eval, 3, [file: 'src/elixir.erl', line: 278]}, {:elixir, :eval\_forms, 3, [file: 'src/elixir.erl', line: 263]}, {IEx.Evaluator, :handle\_eval, 5, [file: 'lib/iex/evaluator.ex', line: 258]}, {IEx.Evaluator, :do\_eval, 3, [file: 'lib/iex/evaluator.ex', line: 239]}, {IEx.Evaluator, :eval, 3, [file: 'lib/iex/evaluator.ex', line: 217]}, {IEx.Evaluator, :loop, 1, [file: 'lib/iex/evaluator.ex', line: 103]} ]
34
destructure([x, y], [1, 2, 3]) {x, y}
=\> {1, 2}
35
destructure([a, b], ["oy"]) "a is #{a} and b is #{b}"
=\> "a is oy and b is "
36
score = %{rob: %{win: 2}, ann: %{win: 3}} get\_and\_update\_in(score, [:rob, :win], &{&1, &1 + 1})
{2, %{ann: %{win: 3}, rob %{win: 3}}
37
defstructure([x, y], [1]) {x, y}
=\> {1, nil}
38
a = "a" destructure([^a, 1], [:a, 1])
\*\* (MatchError) no match of right hand side value: [:a, 1]
39
defmodule Car do defstruct color: :red end struct(Car)
=\> %Car{color: :red}
40
defmodule Car do defstruct color: :red end struct(Car, color: :blue)
=\> %Car{color: :blue}
41
defmodule Pet do defstruct name: "Bob" end pet = struct(Pet, name: "Mia") struct(pet, age: 1)
=\> %Pet{name: "Mia"}
42
defmodule Lang do defstruct name: "Eli" \<\> "xir" end struct(Lang, %{"name" =\> "ruby"})
=\> %Lang{name: "Elixir"}
43
defmodule Car do defstruct make: :BMW end a = struct(Car) b = %{make: :BMW, \_\_struct\_\_: Car} a == b
=\> true
44
defmodule Car do defstruct [:make] end struct(Car)
=\> %Car{make: nil}
45
defmodule Car do @enforce\_keys [:make] defstruct [:make] end struct(Car)
=\> %Car{make: nil} \*Note: enforce\_keys has no effect here. Use struct!/2 or % to enforce keys
46
defmodule Car do @enforce\_keys [:make] defstruct [:make] end struct!(Car)
\*\* (ArgumentError) The following keys must also be given when building struct Car: [:make]
47
defmodule Car do defstruct [:make] end struct!(Car)
=\> %Car{make: nil}
48
defmodule Car do defstruct [:make] end struct!(Car, color: :red)
\*\* (KeyError) key :color not found in: %Car{make: nil}
49
defmodule Child do defstruct name: "El" end a = struct!(%Child{}) struct!(a, age: 1)
\*\* (KeyError) key :age not found in: %Child{name: "El"}
50
~U[2020-03-01T20:00:07.001+00:00]
~U[2020-03-01 20:00:07.001Z]
51
# Define a type, t, for given module so its type can be referenced as User.t defmodule User do defstruct name: "Bob", age: 18 end
@type t :: %\_\_MODULE\_\_{ name: String.t(), age: non\_neg\_integer }
52
~U[2020-03-01 20:34:00Z]
=\> ~U[2020-03-01 20:34:00Z]
53
:a in [:"a"]
=\> true
54
pid = self() fn -\> send(pid, 1 + 1) end |\> spawn\_link() receive do val -\> val end
=\> 2