title | category | tags | updated | weight | |
---|---|---|---|---|---|
Elixir |
Elixir |
|
2018-07-04 |
-10 |
{: .-three-column}
{: .-prime}
# hello.exs
defmodule Greeter do
def greet(name) do
message = "Hello, " <> name <> "!"
IO.puts message
end
end
Greeter.greet("world")
elixir hello.exs
# Hello, world!
{: .-setup}
age = 23
user = %{
name: "John",
city: "Melbourne"
}
IO.puts "Hello, " <> user.name
{: .-setup}
users = [ "Tom", "Dick", "Harry" ]
{: data-line="1"}
Enum.map(users, fn user ->
IO.puts "Hello " <> user
end)
source
|> transform(:hello)
|> print()
{: data-line="2,3"}
# Same as:
print(transform(source, :hello))
These two are equivalent.
user = %{name: "Tom", age: 23}
%{name: username} = user
{: data-line="2"}
This sets username
to "Tom"
.
def greet(%{name: username}) do
IO.puts "Hello, " <> username
end
user = %{name: "Tom", age: 23}
{: data-line="1"}
Pattern matching works in function parameters too.
{: .-three-column}
if false do
"This will never be seen"
else
"This will"
end
case {1, 2, 3} do
{4, 5, 6} ->
"This clause won't match"
{1, x, 3} ->
"This will match and bind x to 2"
_ ->
"This will match any value"
end
cond do
1 + 1 == 3 ->
"I will never be seen"
2 * 5 == 12 ->
"Me neither"
true ->
"But I will (this is essentially an else)"
end
with {:ok, {int, _asdf}} <- Integer.parse("123asdf"),
{:ok, datetime, _utc_offset} <- DateTime.from_iso8601("2021-10-27T12:00:00Z") do
DateTime.add(datetime, int, :second)
# optional else clause. if not provided and an error occurs, the error is returned
else
:error -> "couldn't parse integer string"
{:error, :invalid_format} -> "couldn't parse date string"
_ -> "this will never get hit because all errors are handled"
end
try do
throw(:hello)
catch
message -> "Got #{message}."
after
IO.puts("I'm the after clause.")
end
Sample | Type |
---|---|
nil |
Nil/null |
true / false |
Boolean |
--- | --- |
?a |
Integer (ASCII) |
23 |
Integer |
3.14 |
Float |
--- | --- |
'hello' |
Charlist |
<<2, 3>> |
Binary |
"hello" |
Binary string |
:hello |
Atom |
--- | --- |
[a, b] |
List |
{a, b} |
Tuple |
--- | --- |
%{a: "hello"} |
Map |
%MyStruct{a: "hello"} |
Struct |
fn -> ... end |
Function |
is_atom/1
is_bitstring/1
is_boolean/1
is_function/1
is_function/2
is_integer/1
is_float/1
is_binary/1
is_list/1
is_map/1
is_tuple/1
is_nil/1
is_number/1
is_pid/1
is_port/1
is_reference/1
left != right # equal
left !== right # match
left ++ right # concat lists
left <> right # concat string/binary
left =~ right # regexp
require Redux # compiles a module
import Redux # compiles, and you can use without the `Redux.` prefix
use Redux # compiles, and runs Redux.__using__/1
use Redux, async: true
import Redux, only: [duplicate: 2]
import Redux, only: :functions
import Redux, only: :macros
import Foo.{Bar, Baz}
alias Foo.Bar, as: Bar
alias Foo.Bar # same as above
alias Foo.{Bar, Baz}
import String
{: .-setup}
str = "hello"
str |> length() # → 5
str |> codepoints() # → ["h", "e", "l", "l", "o"]
str |> slice(2..-1) # → "llo"
str |> split(" ") # → ["hello"]
str |> capitalize() # → "Hello"
str |> match(regex)
inspect(object, opts \\ [])
value |> IO.inspect()
value |> IO.inspect(label: "value")
abs(n)
round(n)
rem(a, b) # remainder (modulo)
div(a, b) # integer division
import Float
{: .-setup}
n = 10.3
{: .-setup}
n |> ceil() # → 11.0
n |> ceil(2) # → 11.30
n |> to_string() # → "1.030000+e01"
n |> to_string([decimals: 2, compact: true])
Float.parse("34") # → { 34.0, "" }
import Integer
{: .-setup}
n = 12
{: .-setup}
n |> digits() # → [1, 2]
n |> to_charlist() # → '12'
n |> to_string() # → "12"
n |> is_even()
n |> is_odd()
# Different base:
n |> digits(2) # → [1, 1, 0, 0]
n |> to_charlist(2) # → '1100'
n |> to_string(2) # → "1100"
parse("12") # → {12, ""}
undigits([1, 2]) # → 12
Float.parse("34.1") # → {34.1, ""}
Integer.parse("34") # → {34, ""}
Float.to_string(34.1) # → "3.4100e+01"
Float.to_string(34.1, [decimals: 2, compact: true]) # → "34.1"
m = %{name: "hi"} # atom keys (:name)
m = %{"name" => "hi"} # string keys ("name")
import Map
{: .-setup}
m = %{m | name: "yo"} # key must exist
m |> put(:id, 2) # → %{id: 2, name: "hi"}
m |> put_new(:id, 2) # only if `id` doesn't exist (`||=`)
m |> put(:b, "Banana")
m |> merge(%{b: "Banana"})
m |> update(:a, &(&1 + 1))
m |> update(:a, fun a -> a + 1 end)
m |> get_and_update(:a, &(&1 || "default"))
# → {old, new}
m |> delete(:name) # → %{}
m |> pop(:name) # → {"John", %{}}
m |> get(:id) # → 1
m |> keys() # → [:id, :name]
m |> values() # → [1, "hi"]
m |> to_list() # → [id: 1, name: "hi"]
# → [{:id, 1}, {:name, "hi"}]
put_in(map, [:b, :c], "Banana")
put_in(map[:b][:c], "Banana") # via macros
get_and_update_in(users, ["john", :age], &{&1, &1 + 1})
Map.new([{:b, 1}, {:a, 2}])
Map.new([a: 1, b: 2])
Map.new([:a, :b], fn x -> {x, x} end) # → %{a: :a, b: :b}
Map.from_struct(%AnyStruct{a: "b"}) # → %{a: "b"}
struct(AnyStruct, %{a: "b"}) # → %AnyStruct{a: "b"}
import List
{: .-setup}
l = [ 1, 2, 3, 4 ]
{: .-setup}
l = l ++ [5] # push (append)
l = [ 0 | list ] # unshift (prepend)
l |> first()
l |> last()
l |> flatten()
l |> flatten(tail)
Also see Enum.
import Enum
{: .-setup}
list = [:a, :b, :c]
{: .-setup}
list |> at(0) # → :a
list |> count() # → 3
list |> empty?() # → false
list |> any?() # → true
list |> concat([:d]) # → [:a, :b, :c, :d]
Also, consider streams instead.
list |> reduce(fn)
list |> reduce(acc, fn)
list |> map(fn)
list |> reject(fn)
list |> any?(fn)
list |> empty?(fn)
[1, 2, 3, 4]
|> Enum.reduce(0, fn(x, acc) -> x + acc end)
import Tuple
{: .-setup}
t = { :a, :b }
t |> elem(1) # like tuple[1]
t |> put_elem(index, value)
t |> tuple_size()
list = [{ :name, "John" }, { :age, 15 }]
list[:name]
# For string-keyed keyword lists
list = [{"size", 2}, {"type", "shoe"}]
List.keyfind(list, "size", 0) # → {"size", 2}
square = fn n -> n*n end
square.(20)
square = &(&1 * &1)
square.(20)
square = &Math.square/1
fun.(args)
apply(fun, args)
apply(module, fun, args)
def join(a, b \\ nil)
def join(a, b) when is_nil(b) do: a
def join(a, b) do: a <> b
defmodule User do
defstruct name: "", age: nil
end
%User{name: "John", age: 20}
%User{}.struct # → User
See: Structs
defprotocol Blank do
@doc "Returns true if data is considered blank/empty"
def blank?(data)
end
defimpl Blank, for: List do
def blank?([]), do: true
def blank?(_), do: false
end
Blank.blank?([]) # → true
defimpl Blank, for: Any do ... end
defmodule User do
@derive Blank # Falls back to Any
defstruct name: ""
end
Enumerable
andEnum.map()
Inspect
andinspect()
for n <- [1, 2, 3, 4], do: n * n
for n <- 1..4, do: n * n
for {key, val} <- %{a: 10, b: 20}, do: val
# → [10, 20]
for {key, val} <- %{a: 10, b: 20}, into: %{}, do: {key, val*val}
for n <- 1..10, rem(n, 2) == 0, do: n
# → [2, 4, 6, 8, 10]
for dir <- dirs,
file <- File.ls!(dir), # nested comprehension
path = Path.join(dir, file), # invoked
File.regular?(path) do # condition
IO.puts(file)
end
__MODULE__
__MODULE__.__info__
@after_compile __MODULE__
def __before_compile__(env)
def __after_compile__(env, _bytecode)
def __using__(opts) # invoked on `use`
@on_definition {__MODULE__, :on_def}
def on_def(_env, kind, name, args, guards, body)
@on_load :load_check
def load_check
exp = ~r/hello/
exp = ~r/hello/i
"hello world" =~ exp
~r/regexp/
~w(list of strings)
~s|strings with #{interpolation} and \x20 escape codes|
~S|no interpolation and no escapes|
~c(charlist)
Allowed chars: /
|
"
'
(
[
{
<
"""
.
See: Sigils
@spec round(number) :: integer
@type number_with_remark :: {number, String.t}
@spec add(number, number) :: number_with_remark
Useful for dialyzer. See: Typespecs
defmodule Parser do
@callback parse(String.t) :: any
@callback extensions() :: [String.t]
end
defmodule JSONParser do
@behaviour Parser
def parse(str), do: # ... parse JSON
def extensions, do: ["json"]
end
See: Module
{: .-one-column}