Panex v0.1.0 Panex.Collection

Collection utilities

Link to this section Summary

Functions

Remove all falsy values in collection

This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they’re compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument

Return a slice of array excluding elements dropped from the end

Fills a number of elements of a list with initial value

Return a map from a list of pairs

Returns the list dropping its last element

Get intersection from given lists

Find index from right to left

Same as last_index_of/2 but search from the index

Remove all elements that match the pattern in a collection

Link to this section Functions

Link to this function compact(collection)
compact(list()) :: list()

Remove all falsy values in collection

Examples

iex> compact([1, 2, 3, nil, 4, nil, 5])
[1, 2, 3, 4, 5]
iex> compact(["hello", "hola", false, "bonjour"])
["hello", "hola", "bonjour"]
Link to this function difference_by(list, alist, key)
difference_by(list(), list(), (any() -> any())) :: list()

This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they’re compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument

Examples

iex> difference_by([2.1, 1.2], [2.3, 3.4], &Kernel.round/1)
[1.2]
iex> difference_by([%{"x" => 2}, %{"x" => 1}], [%{"x" => 1}], &(&1["x"]))
[%{"x" => 2}]

Iteratee shorthand

iex> difference_by([%{"x" => 2}, %{"x" => 1}], [%{"x" => 1}], "x")
[%{"x" => 2}]
iex> difference_by([%{x: 2}, %{x: 1}], [%{x: 1}], :x)
[%{x: 2}]
Link to this function drop_right_while(list, func)
drop_right_while(list(), (... -> boolean())) :: list()

Return a slice of array excluding elements dropped from the end.

Examples

iex> drop_right_while([1, 2, 3, 4, 5], &(&1 == 4))
[1, 2, 3]
iex> drop_right_while([%{a: 1, b: 2}, %{a: 2, b: 2}], &(&1.a == 2))
[%{a: 1, b: 2}]
Link to this function fill(num, val)
fill(integer(), any()) :: [any()]

Fills a number of elements of a list with initial value

Examples

iex> fill(5, nil)
[nil, nil, nil, nil, nil]
iex> fill(3, "hi")
["hi", "hi", "hi"]
Link to this function from_pairs(list)
from_pairs(list()) :: map()

Return a map from a list of pairs

Examples

iex> from_pairs([[:a, 1], [:b, 2]])
%{a: 1, b: 2}
iex> from_pairs([["vn", "Xin chao"], ["us", "hello"]])
%{"us" => "hello", "vn" => "Xin chao"}
Link to this function initial(collection)
initial(list()) :: list()

Returns the list dropping its last element

Examples

iex> initial([1, 2, 3, 4])
[1, 2, 3]
Link to this function intersection(first, second)
intersection(list(), list()) :: list()

Get intersection from given lists.

Examples

iex> intersection([1, 2, 3, 4], [2, 3])
[2, 3]
iex> intersection(["index", "carousel", "header", "footer"], ["header", "footer"])
["header", "footer"]
Link to this function last_index_of(list, el)
last_index_of(list(), any()) :: integer()

Find index from right to left.

Examples

iex> last_index_of([1, 1, 3, 4, 2], 4)
3
iex> last_index_of([1, 9, 3, 1, 2], 4)
nil
Link to this function last_index_of(list, el, from)
last_index_of(list(), any(), integer()) :: integer()

Same as last_index_of/2 but search from the index.

Examples

iex> last_index_of([1, 1, 3, 4, 2], 4, 2)
3
iex> last_index_of("abbcd" |> String.graphemes, "b", 3)
nil
Link to this function reject(collection, pattern)
reject(list(), any()) :: list()

Remove all elements that match the pattern in a collection

Examples

iex> reject([1, 2, 3, nil, 4, nil, 5], nil)
[1, 2, 3, 4, 5]
iex> reject(["morning", "afternoon", %{a: "noon"}, "night"], %{a: "noon"})
["morning", "afternoon", "night"]