This file contains hidden or 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
| def add_ten(x) do | |
| x + 10 | |
| end | |
| add_five = fn x -> x + 5 end | |
| add_two = &(&1 + 2) | |
| add_ten(10) # 20 | |
| add_five.(10) # 15 | |
| add_two.(10) # 12 |
This file contains hidden or 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
| function addTen(x) { | |
| return x + 10; | |
| } | |
| // You can also assign an arrow functions to a variable | |
| const addTen = (x) => x + 10; | |
| addTen(10); // 20 |
This file contains hidden or 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
| case [1, 2, 3] do | |
| [1, 2, 4] -> false | |
| [a, 2, 3] -> true | |
| _ -> true # default condition | |
| end |
This file contains hidden or 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
| case (Date.utc_today |> Date.day_of_week) do | |
| 0 -> "Sunday" | |
| 1 -> "Monday" | |
| _ -> "Some other day..." | |
| end |
This file contains hidden or 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
| switch (new Date().getDay()) { | |
| case 0: | |
| return "Sunday"; | |
| case 1: | |
| return "Monday"; | |
| default: | |
| return "Some other day..."; | |
| } |
This file contains hidden or 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
| unless condition do | |
| # occurs when condition is false | |
| end | |
| if not condition do | |
| # same as if not | |
| end |
This file contains hidden or 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
| cond do | |
| x > y -> "x is larger" | |
| x < y -> "x is smaller" | |
| x == y -> "x and y are equal" | |
| true -> "this is always true" | |
| end |
This file contains hidden or 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
| if condition do | |
| else | |
| end |
This file contains hidden or 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
| if (condition) { | |
| } else if (otherCondition) { | |
| } else { | |
| } |
This file contains hidden or 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
| arr = [1, 2, 3, 4, 5] | |
| arr_sqrd = Enum.map(arr, fn x -> x * x end) | |
| arr_sqrd_sum = Enum.reduce(arr_sqrd, fn x, s -> x + s end) |