Skip to content

Commit 3d76f85

Browse files
committed
Chapter 1: Thinking Functional
1 parent bb22d77 commit 3d76f85

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
list = [1, 2, 3, 4]
2+
3+
list_without_last = List.delete_at(list, -1)
4+
IO.inspect list_without_last # [1, 2, 3]
5+
6+
list_with_one_more = list ++ [1]
7+
IO.inspect list_with_one_more # [1, 2, 3, 4, 1]
8+
9+
# the list value is immutable
10+
IO.inspect list # [1, 2, 3, 4]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# simple functions
2+
add2 = fn (n) -> n + 2 end
3+
result = add2.(2)
4+
IO.inspect result
5+
6+
# passing functions as arguments
7+
animals = ["cat", "dog", "fish"]
8+
uppercase_animals = Enum.map(animals, &String.upcase/1)
9+
IO.inspect uppercase_animals
10+
11+
# using pipes to process data
12+
def capitalize_words(title) do
13+
title
14+
|> String.split
15+
|> capitalize_all
16+
|> join_with_whitespace
17+
end

0 commit comments

Comments
 (0)