This file contains 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(name) do | |
"Hello #{name}" | |
end | |
def say_hello(name) when name === "Brooklin" do | |
"Oh it's just you again" | |
end | |
end | |
Greeting.say_hello("Brooklin") # Hello Brooklin |
This file contains 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(name) when name === "Brooklin" do | |
"Oh it's just you again" | |
end | |
def say_hello(name) do | |
"Hello #{name}" | |
end | |
end | |
Greeting.say_hello("Brooklin") # Oh it's just you again |
This file contains 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(name \\ "Brooklin") do | |
"Hello #{name}" | |
end | |
end | |
Greeting.say_hello() # Hello Brooklin |
This file contains 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 M do | |
def above_20?(val) do | |
val > 20 | |
end | |
end | |
M.above_20?(21) # true |
This file contains 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
String.length "a string" # 8 | |
String.contains?("string", "str") # true | |
String.at("mystring", 2) # s | |
String.slice("mystring", 2, 3) | |
String.split("my string", " ") # ["my", "string"] | |
String.reverse("string") # gnirts | |
String.upcase("string") # STRING | |
String.downcase("STRING") # string | |
String.capitalize("string") # "String" |
This file contains 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
# with string syntax | |
map = %{"key" => "value"} | |
Map.get(map, "key") # value | |
# with atom syntax | |
map = %{key: "value"} | |
Map.get(map, :key) # value |
This file contains 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
Tuple.append({1}, 2) # {1, 2} | |
Tuple.duplicate(0, 5) # {0, 0, 0, 0, 0} | |
Tuple.insert_at({1, 3}, 1, 2) # {1, 2, 3} |
This file contains 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
map = %{"key" => "value"} | |
%{map | "key" => "value2"} # %{"key" => "value2"} | |
%{map | "key2" => "value2"} | |
# ** (KeyError) key "key2" not found | |
main.exs:3: (file) |
This file contains 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
[head | tail] = [1,2,3] | |
head # 1 | |
tail # [2, 3] |
This file contains 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
List.last([1,2,3]) # 3 | |
List.first([1, 2, 3]) # 1 | |
List.delete([1,2,3], 1) # [2, 3] | |
List.delete_at([1,2,3], 0) # [2, 3] |