Crutches.List

Summary

from(collection, position)

Returns the tail of the array from +position+

shorten(list, amount \\ 1)

Shorten a list by a given amount

split(collection, x)

Return a List containing the original List splitted by an element or by a function

to(collection, position)

Returns a copy of the List from the beginning to the required index

to_sentence(words, options \\ [])

Converts the array to a comma-separated sentence where the last element is joined by the connector word

Types

t :: List

i :: Integer

a :: any

Functions

from(collection, position)

Specs:

  • from(t, i) :: t

Returns the tail of the array from +position+.

Examples

iex> List.from(["a", "b", "c", "d"], 0)
["a", "b", "c", "d"]

iex> List.from(["a", "b", "c", "d"], 2)
["c", "d"]

iex> List.from(["a", "b", "c", "d"], 10)
[]

iex> List.from([], 0)
[]

iex> List.from(["a", "b", "c", "d"], -2)
["c", "d"]

iex> List.from(["a", "b", "c", "d"], -10)
[]
shorten(list, amount \\ 1)

Specs:

  • shorten(t, integer) :: t

Shorten a list by a given amount.

When the list is shorter than the amount given, this function returns nil.

Examples

iex> List.shorten(["one", "two", "three"], 2)
["one"]

iex> List.shorten([5, 6], 2)
[]

iex> List.shorten([5, 6, 7, 8], 5)
nil
split(collection, x)

Specs:

  • split(t, any) :: t

Return a List containing the original List splitted by an element or by a function.

Examples

iex> List.split(["a", "b", "c", "d", "c", "e"], "c")
[["a", "b"], ["d"], ["e"]]

iex> List.split(["c", "a", "b"], "c")
[[], ["a", "b"]]

iex> List.split([], 1)
[[]]

iex> List.split([1, 2, 3, 4, 5, 6, 7, 8], fn(x) -> rem(x, 2) == 0 end)
[[1], [3], [5], [7], []]

iex> List.split(1..15, &(rem(&1,3) == 0))
[[1, 2], [4, 5], [7, 8], [10 , 11], [13, 14], []]
to(collection, position)

Specs:

  • to(t, i) :: t

Returns a copy of the List from the beginning to the required index.

Examples

iex> List.to(["a", "b", "c"], 0)
["a"]

iex> List.to(["a", "b", "c"], 1)
["a", "b"]

iex> List.to(["a", "b", "c"], 20)
["a", "b", "c"]

iex> List.to(["a", "b", "c"], -1)
[]
to_sentence(words, options \\ [])

Converts the array to a comma-separated sentence where the last element is joined by the connector word.

You can pass the following options to change the default behavior. If you pass an option key that doesn’t exist in the list below, it will raise an

ArgumentError.

Options

  • :words_connector - The sign or word used to join the elements in arrays with two or more elements (default: “, “).
  • :two_words_connector - The sign or word used to join the elements in arrays with two elements (default: “ and “).
  • :last_word_connector - The sign or word used to join the last element in arrays with three or more elements (default: “, and “).
  • :locale - If +i18n+ is available, you can set a locale and use the connector options defined on the ‘support.array’ namespace in the corresponding dictionary file.

Examples

iex> List.to_sentence([])
""

iex> List.to_sentence(["one"])
"one"

iex> List.to_sentence(["one", "two"])
"one and two"

iex> List.to_sentence(["one", "two", "three"])
"one, two, and three"

iex> List.to_sentence(["one", "two"], [{:passing, "invalid option"}])
** (ArgumentError) invalid key passing

iex> List.to_sentence(["one", "two"], [{:two_words_connector, "-"}])
"one-two"

iex> List.to_sentence(["one", "two", "three"], [{:words_connector, " or "}, {:last_word_connector, " or at least "}])
"one or two or at least three"

Using <tt>:locale</tt> option:

Given this locale dictionary:

 es:
   support:
     array:
       words_connector: " o "
       two_words_connector: " y "
       last_word_connector: " o al menos "

iex> es = [support: [array: [words_connector: " o ", two_words_connector: " y ", last_word_connector: " o al menos "]]]
iex> List.to_sentence(['uno', 'dos'], [{:locale, es}])
"uno y dos"

iex> es = [support: [array: [words_connector: " o ", two_words_connector: " y ", last_word_connector: " o al menos "]]]
iex> List.to_sentence(['uno', 'dos', 'tres'], [{:locale, es}])
"uno o dos o al menos tres"