defmodule BankAcount do def f1(stack) do number = :rand.uniform(9) + 1 case rem(number, 2) do 0 -> ["ErrBankAccountEmpty", [:f1 | stack]] _ -> ["ErrInvestmentLost", [:f1 | stack]] end end def f2(stack) do f1([:f2 | stack]) end def f3(stack) do f1([:f3 | stack]) end def f4(stack) do number = :rand.uniform(9) + 1 case rem(number, 2) do 0 -> f2([:f4 | stack]) _ -> f3([:f4 | stack]) end end def main() do case f4([:main]) do [_err, [:f1, :f2, :f4, :main]] -> "Aand it's gone" [_err, [:f1, :f3, :f4, :main]] -> "The money in your account didn't do well" _err -> "This line is for bank members only" end |> raise end end BankAcount.main()
Write, Run & Share Elixir code online using OneCompiler's Elixir online compiler for free. It's one of the robust, feature-rich online compilers for Elixir language, running on the latest version 1.9.4. Getting started with the OneCompiler's Elixir compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Elixir
and start coding.
Elixir is a general purpose programming language built on top of Erlang or Ericsson language. It is very efficient for building distributed, fault tolerant appications. It's been used by many companies like Pinterest, PagerDuty, FarmBot, E-Metro Tel etc.
Variables must be declared and assigned a value.
variable-name = value
Data type | Usage | Description |
---|---|---|
Numeric | x = 21 | Elixir supports not only integer and float values but also a number can be defined in octal, hexadecimal and binary bases. |
Atom | :true | Atoms are constant values whose name is same as their value |
Boolean | : false, :true | Either true or false, usually declared as atoms |
Strings | "Hello" | Strings are enclosed in double quotes(" ") and multi line strings are enclosed in triple double quotes(""" """) |
Lists | ['a', 10, :true] | Lists are used to store different types of values and are represented in square brackets [] |
Tuples | {'apple', 100, :false} | Similar to Lists and are represented in curly brackets {}. Tuples are good for accessing the values and lists are good for insertion and deletion of values |
When ever you want to perform a set of operations based on a condition If is used.
if condition do
#Code
end
When there is requirement to add code for false condition to IF block.
if condition do
#Code
else
#code
end
Unless is similar to If but the code get executed only if the condition fails.
unless condition do
#Code
end
Unless-Else is similar to If-Else but the code get executed only if the condition fails.
unless condition do
#Code if condition fails
else
#Code if condition satisfies
end
Cond is used when you want to execute a piece of code based on multiple conditions.
cond do
condition-1 -> #code if condition is true
condition-2 -> #code if condition is true
...
true -> #code if none of the above are true
end
Case is similar to switch in C language.
case value do
value-1 -> #code if value matches value-1
value-2 -> #code if value matches value-2
value-3 -> #code if value matches value-3
...
_ -> #code if value does not match any of the above and is similar to default in switch
end
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in Elixir
Anonymous functions are functions with no name and they use fn..end
constructs.
Assign names to functions so that they can be called easily. Always named functions are defined in modules.
defmodule modulename do
def function_name(parameter1, parameter2) do
#code
end
end