|
I need...
|
Function
|
Example
|
BigO
|
|
Execute without return
|
Enum.each/2
|
Enum.each([1,2,3], &IO.puts/1)
|
O(n)
|
|
Transform each element
|
Enum.map/2
|
Enum.map([1,2,3], &(&1 * 2)) # [2,4,6]
|
O(n)
|
|
Filter elements
|
Enum.filter/2
|
Enum.filter([1,2,3], &(&1 > 1)) # [2,3]
|
O(n)
|
|
Accumulate a value
|
Enum.reduce/2
|
Enum.reduce([1, 2, 3, 4], fn x, acc -> x * acc end)
|
O(n)
|
|
Find a value
|
Enum.find/2
|
Enum.find([1,2,3], &(&1 > 1)) # 2
|
O(n) (worst case)
|
|
Check if all match condition
|
Enum.all?/2
|
Enum.all?([2,4,6], &rem(&1, 2) == 0) # true
|
O(n) (worst case)
|
|
Check if exists inside
|
Enum.member?/2
|
Enum.member?([1,2,3], 2) # true
|
O(n)
|
|
Data Structure
|
Access Compex
|
Observations
|
|
list
|
O(n)
|
sorted, duplicated keys
|
|
tupple
|
O(1)
|
|
|
map
|
O(1) map > 32 / O(n) map < 32
|
unsorted, no key duplication
|
|
keyword list
|
O(n)
|
Inserting with prepend is O(1)
|
|
Name
|
Time Complex
|
Comments
|
|
Merge Sort
|
🔹 O(n log n) / O(n log n) / O(n log n)
|
Recommended in Elixir (recursive and stable)
|
|
Insertion Sort
|
✅ O(n) / 🔺 O(n²) / O(n²)
|
Good for small or semi-ordered lists
|
|
Radix Sort
|
O(nk) / O(nk) / O(nk)
|
Very fast on small integers
|
|
Bubble Sort
|
✅ O(n) / 🔺 O(n²) / ❌ O(n²)
|
Inefficient, useful only for small or already sorted lists
|
|
Selection Sort
|
🔺 O(n²) / O(n²) / O(n²)
|
Simple, but slow
|
|
QuickSort
|
✅ O(n log n) / O(n log n) / ❌ O(n²)
|
Better than MergeSort on average, but can degrade to O(n²)
|
|
Heap Sort
|
O(n log n) / O(n log n) / O(n log n)
|
|
|
Counting Sort
|
O(n + k) / O(n + k) / O(n + k)
|
Only works with small numbers and limited range
|
|
Bucket Sort
|
O(n + k) / O(n + k) / O(n²)
|
Useful if data is evenly distributed
|
{{lang-elixir}}
defmodule MergeSort do
def sort([]), do: []
def sort([x]), do: [x]
def sort(list) do
midpoint = div(length(list), 2)
{left, right} = Enum.split(list, midpoint)
new_left = sort(left)
new_right = sort(right)
merge(new_left, new_right)
end
def merge(left, []), do: left
def merge([], right), do: right
def merge([l | left], [r | right]) when l <= r do
[l | merge(left, [r | right])]
end
def merge([l | left], [r | right]) do
[r | merge([l | left], right)]
end
end
MergeSort.sort([2,4,6,7,1,2,3,5])
{{lang-elixir}}
defmodule Sort do
def insert([], x), do: [x]
def insert([h | t], x) when x <= h, do: [x, h | t] # Insert before if is lower
def insert([h | t], x), do: [h | insert(t, x)] # Continue searching if is greater
def insertion_sort([]), do: []
def insertion_sort([h | t]) do
sorted_tail = insertion_sort(t)
insert(sorted_tail, h)
end
end
Sort.insertion_sort([3, 1, 4, 2])
{{lang-elixir}}
defmodule Anagram do
def anagram?(word1, word2) do
Enum.sort(String.graphemes(word1)) == Enum.sort(String.graphemes(word2))
end
end
IO.inspect(Anagram.anagram?("listen", "silent")) # true
{{lang-elixir}}
defmodule Palindrome do
def is_polindrome?(word) do
word_to_charlist = to_charlist(word)
(word_to_charlist == Enum.reverse(word_to_charlist))
end
end
Palindrome.is_polindrome?("acbca")
{{lang-elixir}}
defmodule Fibonacci do
def fib(n), do: fib(n, 0, 1)
defp fib(0, a, _b), do: a
defp fib(n, a, b), do: fib(n - 1, b, a + b)
end
IO.inspect(Fibonacci.fib(10)) # => 55
|
Acumulate
|
reduce
|
|
Return in map or diff type than map
|
reduce
|
|
Return list
|
map
|
|
Discard / Clean
|
filter
|
|
mount
|
|
|
handle_event
|
clicks, forms, keys, hooks.
|
|
handle_info
|
messages from other processes (send)
|
|
handle_params
|
Respond to URL changes
|
|
init
|
|
|
handle_call
|
|
|
handle_cast
|
|
|
handle_info
|
|
|
code_change
|
Hot code update
|
|
terminate
|
|
|
Lists (Prepend)
|
[new_item | list]
|
|
Maps insert
|
Map.put O(1) except hash collisions
|
|
Maps update
|
%{map | a: 42} better than Map.put
|
|
Keyword List (Prepend)
|
[{:key, val} | kw_list]
|
{{lang-elixir}}
#Directly using sigil ~c
~c"abc"
#from symbol
def foo(name) do
to_charlist(name)
end
|
use
|
Extend modules and add functionality
|
use GenServer
|
|
require
|
Use macros from another module
|
require Logger
|
|
import
|
Use functions without module prefix
|
import Enum, only: [map: 2]
|
|
alias
|
Short modules names
|
alias MyApp.Foo.Foo2
|
GenServer has state and response to casts.
GenState is data-flow with backpresure.
{{lang-elixir}}
String.graphemes("AB")
@impl trueuse macro. This extends functions in the module.|
Caracterist
|
OBAN
|
Exq
|
|
DB
|
Postgres
|
Redis
|
|
Distributed
|
Multi-node
|
Needs Redis Cluster
|
|
Retries
|
Configurable with backoff
|
Less flexible
|
|
Scalable
|
Excelent for big systems
|
Good, but depends on Redis
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.