Elixir Cheatsheet
50 essential methods for coding interviews
Showing 50 of 50 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
Enum.map | Enum.map(enumerable, fun) | Transforms each element by applying a function | O(n) | essential |
Enum.filter | Enum.filter(enumerable, fun) | Returns elements where the function returns truthy value | O(n) | essential |
Enum.reduce | Enum.reduce(enumerable, acc, fun) | Reduces enumerable to a single value using accumulator | O(n) | essential |
Enum.find | Enum.find(enumerable, default \\ nil, fun) | Returns first element where function returns truthy value | O(n) | essential |
Enum.sort | Enum.sort(enumerable, sorter \\ :asc) | Sorts elements using merge sort algorithm | O(n log n) | essential |
Enum.reverse | Enum.reverse(enumerable) | Reverses the enumerable | O(n) | essential |
Enum.any? | Enum.any?(enumerable, fun \\ fn x -> x end) | Returns true if any element satisfies the function | O(n) | essential |
Enum.all? | Enum.all?(enumerable, fun \\ fn x -> x end) | Returns true if all elements satisfy the function | O(n) | essential |
Enum.count | Enum.count(enumerable) | Enum.count(enumerable, fun) | Returns count of elements (optionally matching function) | O(n) | essential |
Enum.max/min | Enum.max(enumerable) | Enum.min(enumerable) | Returns maximum or minimum element | O(n) | essential |
Enum.group_by | Enum.group_by(enumerable, key_fun, value_fun \\ fn x -> x end) | Groups elements by key function result | O(n) | essential |
Enum.frequencies | Enum.frequencies(enumerable) | Returns map with elements as keys and counts as values | O(n) | essential |
Enum.uniq | Enum.uniq(enumerable) | Removes duplicate elements preserving first occurrence | O(n) | essential |
Enum.with_index | Enum.with_index(enumerable, offset \\ 0) | Wraps each element with its index as tuple | O(n) | essential |
hd/tl | hd(list) | tl(list) | Returns head (first element) or tail (rest) of list | O(1) | essential |
List prepend [h|t] | [element | list] | Prepends element to list (cons operation) | O(1) | essential |
++ operator | list1 ++ list2 | Concatenates two lists | O(n) | essential |
length | length(list) | Returns the length of a list | O(n) | essential |
String.split | String.split(string, pattern, opts \\ []) | Splits string by pattern into list | O(n) | essential |
String.join | Enum.join(enumerable, joiner \\ "") | Joins list elements into string with separator | O(n) | essential |
String.reverse | String.reverse(string) | Reverses a string (grapheme-aware) | O(n) | essential |
String.length | String.length(string) | Returns number of graphemes in string | O(n) | essential |
String.contains? | String.contains?(string, contents) | Checks if string contains given contents | O(n*m) | essential |
String.graphemes | String.graphemes(string) | Splits string into list of graphemes | O(n) | essential |
Map.get | Map.get(map, key, default \\ nil) | Gets value for key with optional default | O(log n) | essential |
Map.put | Map.put(map, key, value) | Puts value under key (inserts or updates) | O(log n) | essential |
Map.update | Map.update(map, key, default, fun) | Updates key with function, uses default if key missing | O(log n) | essential |
Map.has_key? | Map.has_key?(map, key) | Checks if map contains key | O(log n) | essential |
case expression | case expr do pattern -> result end | Pattern matches expression against clauses | O(1) | essential |
Guards | when guard_expression | Additional conditions in pattern matching | O(1) | essential |
MapSet.new | MapSet.new(enumerable) | Creates a set from enumerable | O(n) | essential |
MapSet.member? | MapSet.member?(set, value) | Checks if value is in set | O(log n) | essential |
Recursive list processing | def fun([head | tail]) do ... fun(tail) end | Pattern match head and recurse on tail | O(n) | essential |
Tail recursion with accumulator | def fun(list, acc \\ initial) | Tail-recursive pattern with accumulator | O(n) | essential |
Enum.sort_by | Enum.sort_by(enumerable, mapper, sorter \\ :asc) | Sorts by applying mapper function to determine order | O(n log n) | common |
Enum.sum | Enum.sum(enumerable) | Sums all elements in the enumerable | O(n) | common |
Enum.take/drop | Enum.take(enumerable, n) | Enum.drop(enumerable, n) | Takes or drops first n elements from enumerable | O(n) | common |
Enum.at | Enum.at(enumerable, index, default \\ nil) | Returns element at index (0-based) | O(n) | common |
Enum.member? | Enum.member?(enumerable, element) | Checks if element exists in enumerable | O(n) | common |
Enum.zip | Enum.zip(enumerables) | Zips corresponding elements from multiple enumerables | O(n) | common |
Enum.flat_map | Enum.flat_map(enumerable, fun) | Maps and flattens result by one level | O(n*m) | common |
List.flatten | List.flatten(list, tail \\ []) | Flattens nested lists into single list | O(n) | common |
String.at/slice | String.at(string, pos) | String.slice(string, range) | Returns grapheme at position or substring from range | O(n) | common |
String.starts_with?/ends_with? | String.starts_with?(string, prefix) | String.ends_with?(string, suffix) | Checks if string starts/ends with given prefix/suffix | O(m) | common |
String.downcase/upcase/trim | String.downcase(s) | String.upcase(s) | String.trim(s) | Case conversion and whitespace trimming | O(n) | common |
String.replace | String.replace(subject, pattern, replacement, opts \\ []) | Replaces occurrences of pattern in string | O(n) | common |
Map.new | Map.new(enumerable) | Map.new(enumerable, transform) | Creates a new map from enumerable | O(n) | common |
Map.keys/values/merge | Map.keys(map) | Map.values(map) | Map.merge(m1, m2) | Get all keys/values or merge two maps | O(n) | common |
cond/with expressions | cond do cond -> result end | with pattern <- expr do body end | Multi-condition evaluation or chained pattern matching | O(n) | common |
MapSet operations | MapSet.put(set, val) | MapSet.intersection/union/difference(s1, s2) | Add to set or perform set operations | O(n log n) | common |