Last active
August 29, 2015 14:21
-
-
Save darioghilardi/c2fd1206f1e1f78b835a to your computer and use it in GitHub Desktop.
elixir_002: Modules and functions
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 Names do | |
def full_name(name, surname, title \\ "Mr") do | |
title <> " " <> String.capitalize(name) <> " " <> String.capitalize(surname) | |
end | |
end | |
Names.full_name("john", "doe") # Mr John Doe | |
Names.full_name("melissa", "doe", "Ms") # Ms Melissa Doe |
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 Names do | |
def full_name(name, surname) do | |
String.capitalize(name) <> " " <> String.capitalize(surname) | |
end | |
end | |
fun = &Names.full_name/2 | |
# &Names.full_name/2 | |
fun.("John", "Doe") | |
# "John Doe" |
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 Names do | |
def full_name(name, surname) do | |
name <> "" <> surname | |
end | |
def capitalized_name(name) do | |
String.capitalize(name) | |
end | |
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
defmodule Names do | |
def full_name(name) do | |
String.capitalize(name) | |
end | |
def full_name(name, surname) do | |
String.capitalize(name) <> " " <> String.capitalize(surname) | |
end | |
end | |
Names.full_name("john") # John | |
Names.full_name("john", "doe") # John Doe | |
Names.full_name("john", "doe", "jr") # (UndefinedFunctionError) undefined function: Names.full_name/3 |
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 Names do | |
def name(name, married) when married == true do | |
"Mrs" <> " " <> String.capitalize(name) | |
end | |
def name(name, married) when married == false do | |
"Ms" <> " " <> String.capitalize(name) | |
end | |
end | |
Names.name("melissa", true) # Mrs Melissa | |
Names.name("Hilary", false) # Ms Hilary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment