Skip to content

Commit d05c810

Browse files
committed
[Add] anonymous functions, head and tail, read_file examples
1 parent 817fc42 commit d05c810

6 files changed

+29
-0
lines changed

anonymous_function.exs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add = fn(a, b) -> a + b end
2+
3+
IO.puts add.(1,2)
4+
5+
double = fn a -> add.(a, a) end
6+
7+
IO.puts double.(2)

case.exs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case {1, 2, 3} do
2+
{4, 5, 6} ->
3+
IO.puts "This clause won't match"
4+
{1, x, 3} ->
5+
IO.puts "This clause will match and bind x to 2 in this clause"
6+
_ ->
7+
IO.puts "This clause would match any value"
8+
end

clauses_guards.exs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
f = fn
2+
x, y when x > 0 -> x + y
3+
x, y -> x * y
4+
end
5+
6+
IO.puts f.(1, 3)

example.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Mauricio
2+
Serna
3+
Florez

head_tail.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[head | tail] = [1, "Mauricio", "Serna"]
2+
3+
IO.puts head
4+
IO.puts tail

read_file.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File.read("./example.txt")

0 commit comments

Comments
 (0)