Skip to content

List (array)

difference(list1, list2)

Returns a list where the items in list2 are removed from list1.

Note that the resulting list will not be sorted.

Examples:

iex> difference([1, 2, 3], [2, 3])
[1]
iex> difference("abc", "ab")
"c"

filter(list, query)

Filter list of maps according to a MatchEngine query.

first(list)

Return the first item in a list

Examples:

iex> first([1, 2, 3])
1
iex> first("abc")
"a"

flatten(list)

Flatten a list using Elixir's List.flatten/1.

Examples:

iex> flatten([[1, 2], [3, 4]])
[1, 2, 3, 4]
iex> flatten([[1, 2], []])
[1, 2]
iex> flatten("abc")
["a", "b", "c"]

intersection(list1, list2)

Returns the set-intersection of two lists.

Note that the resulting list will not be sorted.

Examples:

iex> intersection([1, 2, 3], [2, 3]) 
[2, 3]
iex> intersection("abc", "ab")
"ab"

item(ets, index)

Return the n-th item from the given list (the first element is at index 0)

Examples:

iex> item([1, 2, 3, 4], 1)
2
iex> item([[1, 2], [3, 4]], 1)
[3, 4]
iex> item("abc", 0)
"a"

join(list, joiner)

Join the given list using a string.

Examples:

iex> join(["06", "121", "448", "66"], "-")
"06-121-448-66"
iex> join("abc", " ")
"a b c"

join(list, joiner, last_joiner)

Join the given list using a string.

The last_joiner string will be used to join the last two items in the list. This helps with joining strings in a "human readable" way:

Examples:

iex> "The candidates are: " + join(["John", "Mary", "Elsa"], ", ", " and ")
"The candidates are: John, Mary and Elsa"

join_and(input, opts \\ [])

Joins a list of items in the current users locale, or in the specified locale.

Given options are passed to join_localized.

Examples:

iex> join_and(["1", "2", "3"])
"1, 2, and 3"
iex> join_and("123")
"1, 2, and 3"

join_localized(list, opts \\ [])

Presents the list in a human localized to the language set by the locale option.

Options:

  • :locale - A locale string, default is "en".
  • :style - The style to join the list in. Defaults to :standard. Available: [:or, :or_narrow, :or_short, :standard, :standard_narrow, :standard_short, :unit, :unit_narrow, :unit_short]

Examples:

iex> join_localized([1, 2, 3, 4], locale: "en")
"1, 2, 3, and 4"
iex> join_localized(["John", "Mary", "Elsa"], locale: "fr")
"John, Mary et Elsa"
iex> join_localized(["John", "Mary", "Elsa"], locale: "fr", style: :or)
"John, Mary ou Elsa"
iex> join_localized(["John", "Mary", "Elsa"], locale: "en", style: :or)
"John, Mary, or Elsa"
iex> join_localized(["John", "Mary", "Elsa"], locale: "en", style: :unit)
"John, Mary, Elsa"
iex> join_localized(["John", "Mary", "Elsa"], locale: "en_GB.FORMAL", style: :unit)
"John, Mary, Elsa"
iex> join_localized("123", locale: "fr")
"1, 2 et 3"

join_or(input, opts \\ [])

Joins a list of items in the current users locale, or in the specified locale.

Given options are passed to join_localized.

This is a shorthand for join_localized(input, style: :or).

Examples:

iex> join_or(["1", "2", "3"])
"1, 2, or 3"
iex> join_or("123")
"1, 2, or 3"

keys(ets)

Return the keys of a map or keyword list.

When a plain list is given, returns a list with the list indexes ([0, 1, 2, ...]).

Raises an error on non list/map values.

Examples:

iex> keys(%{a: :b, b: :c})
[:a, :b]
iex> keys("abc")
[0, 1, 2]

last(list)

Return the last item from a list

Examples:

iex> last([1, 2, 3])
3
iex> last("abc")
"c"

pluck(list, keys)

Given a list of maps, return for each map the item with the given key.

Examples:

iex> pluck([%{name: "John", age: 42, job: "Designer"}, %{name: "Mary", age: 36, job: "Programmer"}], [:name, :job])
[%{name: "John", job: "Designer"}, %{name: "Mary", job: "Programmer"}]

score(list, query)

Score a list of maps according to a MatchEngine query.

shuffle(list)

Shuffle the given list, putting its contents in random order.

sort(enum, opts \\ [])

Sort a list.

Options:

  • :direction - either :asc or :desc
  • :field - when sorting a list of maps, specify which field in the maps to sort on.

Examples:

iex> sort([[num: 1], [num: 2], [num: 3], [num: 4], [num: 5]], field: :num, direction: :desc)
[[num: 5], [num: 4], [num: 3], [num: 2], [num: 1]]
iex> sort("edcba")
["a", "b", "c", "d", "e"]

split(string, pattern, opts \\ [])

Split the given string on the pattern, returning a list.

Options (doc):

  • :parts (positive integer or :infinity) - the string is split into at most as many parts as this options specifies. If :infinity, the string will be split into all possible parts. Defaults to :infinity.

  • :trim (boolean) - if true, empty strings are removed from the resulting list.

Examples:

iex> split("this will be a list", " ")
["this", "will", "be", "a", "list"]
iex> split("one, two, three", ",", parts: 2)
["one", " two, three"]
iex> split("one, two, three,", ",", trim: true)
["one", " two", " three"]

subset?(list1, list2)

Determine whether all items in list1 are also in list2. Returns true or false.

Examples:

iex> subset?([2, 3], [1, 2, 3])
true
iex> subset?([1, 2, 3], [1, 2, 3])
true
iex> subset?([], [1, 2, 3])
true
iex> subset?([4, 5], [1, 2, 3])
false
iex> subset?("abc", "abcd")
true

union(list1, list2)

Returns the set-union of two lists.

Note that the resulting list will not be sorted.

Examples:

iex> union([1, 2, 3], [2, 3])
[1, 2, 3]
iex> union([4, 5], [2, 3])
[2, 3, 4, 5]
iex> union("abc", "abcd")
"abcd"

uniq(list)

Return a list with unique elements (no duplicates)

Examples:

iex> uniq([1, 2, 2, 3, 3, 3])
[1, 2, 3]
iex> uniq("aaabbbccc")
["a", "b", "c"]

values(ets)

Return the values of a map or a keyword list.

When a plain list is given, returns the list itself.

Raises an error on non list/map values.

Examples:

iex> values(%{a: :b, b: :c})
[:b, :c]
iex> values("abc")
["a", "b", "c"]