Overview
August 22, 2023 3 minutes • 448 words
Table of contents
Elixir uses dynamic types based on Erlang where the type is determined by the data entered in the variable.
Data Types
Built-in types
- Atom - literal constants with a name (true, false, and nil are atoms)
- Float - numbers with floating point precision
- Function - a reference to code chunk, created with the fn/1 special form
- Integer - whole numbers (not fractions)
- List - collections of a variable number of elements (linked lists)
- Map - collections of key-value pairs
- Process - light-weight threads of execution
- Port - mechanisms to interact with the external world
- Tuple - collections of a fixed number of elements
Data types without a module
- Bitstring - a sequence of bits, created with Kernel.SpecialForms.«»/1. When the number of bits is divisible by 8, they are called binaries and can be manipulated with Erlang’s :binary module
- Reference - a unique value in the runtime system, created with make_ref/0
Other Data types
These are built on top of the types above.
- Date - year-month-day structs in a given calendar
- DateTime - date and time with time zone in a given calendar
- Exception - data raised from errors and unexpected scenarios
- MapSet - unordered collections of unique elements
- NaiveDateTime - date and time without time zone in a given calendar
- Keyword - lists of two-element tuples, often representing optional values
- Range - inclusive ranges between two integers
- Regex - regular expressions
- String - UTF-8 encoded binaries representing characters
- Time - hour:minute:second structs in a given calendar
- URI - representation of URIs that identify resources
- Version - representation of versions and requirements
“Strings”
Manipulating Strings
String.split
- converts string into a list with each word put in quotes, separated by a comma
"asdfasd" |> String.split
{Tuples} for fixed number of elements
{"string", integer, f.loat, :atom}
elem(variable, position_in_index)
to extract values in the tuple
a = {"Lam", 143}
elem(a, 1)`
# 123
[Character List]
Denoted by ~c
?x
denotes the code point of x
Binary «1, 2, 3»
Atom
An atom’s name is the same as its value
Atom
:atom_name
:Atom_name
:"Atom name"
Converting Data Types
Convert string to numbers
String.to_integer
and String.to_float
- converts a string-integer and string-float into an integer and float
"123" |> String.to_integer
"123.123" |> String.to_float
Integer.parse, Float.parse
123 |> Integer.parse
# returns tuple e.g. {123.45, ""}
Integer.parse(n)
Float.parse(n)
# returns integer
String.to_integer(n)
String.to_float(n)
Decimal.new(n) |> Decimal.to_integer
Decimal.new(n) |> Decimal.to_float
Example
"1.0 1 3 10 100" |> String.split |> Enum.map(fn n -> Float.parse(n) |> elem(0) end)
[1.0, 1.0, 3.0, 10.0, 100.0]
Convert numbers to string
n |> to_string([decimals: 2, compact: true])
Convert float to charlist
n |> to_charlist()
Erlang Functions
:timer
IO.puts "whatever"
Module
container for the functions
defmodule ModuleName1 do
def function_name(argument1, argument2,..) do
ModuleName2.function_name()