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
| [1, 2] ++ [3, 4] # [1, 2, 3, 4] | |
| [1, 2, 3] -- [1, 3] # [2] | |
| [1, 2, 3] -- [1] # [2, 3] | |
| [1, 2, 3] -- [3, 1] # [2] |
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
| Enum.map([1, 2, 3], fn x -> x * 2 end) # [2, 4, 6] | |
| Enum.sum([1, 2, 3]) # 6 | |
| Enum.map(1..3, fn x -> x * 2 end) # [2, 4, 6] | |
| Enum.sum(1..3) # 6 | |
| map = %{"a" => 1, "b" => 2} | |
| Enum.map(map, fn {k, v} -> {k, v * 2} end) # [{"a", 2}, {"b", 4}] |
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
| 1 # integer | |
| 0b1010 # 10 integer in binary | |
| 0o777 # 511 integer in octal | |
| 0x1F # 31 integer in hexadecimal | |
| 1.2 # float | |
| :example # atom | |
| "hello" # string | |
| true # boolean | |
| false # boolean | |
| [1, 2, 3] # list |
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
| defmodule Greeting do | |
| def main do | |
| say_hello() | |
| end | |
| def say_hello do | |
| "Hello" | |
| end | |
| end | |
| Greeting.main() # Hello |
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
| defmodule Greeting do | |
| def say_hello do | |
| "Hello" | |
| end | |
| def say_hello(name) do | |
| "Hello #{name}" | |
| end | |
| def say_hello(name1, name2) do | |
| "Hello #{name1}, Hi #{name2}" | |
| 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(true, do: "code to run when true", else: "code to run when false") | |
| # brackets are optional | |
| if true, do: "code to run when true", else: "code to run when false" |
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 true do | |
| # code to run when true | |
| else | |
| # code to run when false | |
| 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
| unless false do | |
| # code to run when false | |
| else | |
| # code to run when 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
| case 1 do | |
| 1 -> "the 1 path is executed" | |
| 2 -> "the 2 path is skipped" | |
| _ -> "the default path is skipped" | |
| end | |
| case 2 do | |
| 1 -> "the 1 path is skipped" | |
| 2 -> "the 2 path is executed" | |
| _ -> "the default path is skipped" |
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 | |
| 5 < 3 -> "this will be skipped" | |
| 4 == 4 and 5 > 3 -> "this code will execute" | |
| end |