Basic Sintax Flashcards
Return the first occurrence of a pettern
Regex.run %r/[aei]/, “aeiou”
Replace all occurrences of a pettern with a value
Regex.replace %r/[a]/, “aeiou”, “A”
Return a list with all occurrences of a pettern
Regex.scan %r/[aeiou]/, “caterpillar”
How to define an anonymous function?
sum = fn (a, b) -> a + b end
How to print a simple IO message?
IO.puts “Hello World”
How to call a anonymous function?
sum.(10, 20)
How is the sintaxe of The & Notation?
add_one = &(&1 + 1)
How to define a basic function?
defmodule Times do
def double(n) do
n*2
end
end
How to define a inline function?
def double(n), do: n * 2
How to define a Guard Clausule?
defmodule Guard do
def what_is(x) when is_number(x) do
IO.puts “#{x} is a number”
end
end
How to define default params?
defmodule Example do
def func(p1, p2 // 2, p3 // 3, p4) do
IO.inspect [p1, p2, p3, p4]
end
end
How to create a alias to a module?
defmodule Example do
def func do
alias My.ModuleFoo, as: Foo
doc = Foo.setup
end
end
How to define a module attribute?
defmodule Example do
@author “User Name”
end
How to implement a list comprehension?
lc x inlist [1,2,3], do: x * 2
How to access a index of a String?
String.at(“123”, 2)