Code · 1 file
Sample Elixir File
Elixir is a functional language on the Erlang VM, used for fault-tolerant services and Phoenix. The Elixir sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.
Elixir · 675 B
Sample Elixir file
An idiomatic Elixir example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
# Sample Elixir module — samplefiles.com
defmodule Sample.Customer do
defstruct [:id, :name, :balance]
def vip?(%__MODULE__{balance: balance}), do: balance >= 10_000
def total_balance(customers), do: customers |> Enum.map(& &1.balance) |> Enum.sum()
end
alias Sample.Customer
customers = [
%Customer{id: 1, name: "Ada", balance: 12_500.0},
%Customer{id: 2, name: "Grace", balance: 830.5},
%Customer{id: 3, name: "Linus", balance: 10_000.0}
]
for c <- customers do
IO.puts("#{String.pad_trailing(c.name, 6)} #{:erlang.float_to_binary(c.balance, decimals: 2)} #{if Customer.vip?(c), do: "VIP"}")
end
IO.puts("Total: #{Customer.total_balance(customers)}")File details
Download from the command line
curl -L -o sample.ex https://files.samplefiles.com/code/ex/sample.exwget https://files.samplefiles.com/code/ex/sample.exhttps://files.samplefiles.com/code/ex/sample.exprintf '%s %s\n' d1502db521c59255398803f8347d2b0422bd40ec87bd4dd70cffe60738aadbdc sample.ex | shasum -a 256 -cOr check the download in your browser with the checksum calculator.