List module
The List module contains functions to deal with lists. Lists are actually Linked List.
at/2
List.at(["a", "b", "c"], 0) # "a"
List.at(["a", "b", "c"], 1) # "b"
List.at(["a", "b", "c"], 2) # "c"
List.at([], 0) # nil
Parameters:
listthe listindexthe index (zero-based)
Returns the element at index in list.
size/1
List.size([]) # 0
List.size([1,2,3,4]) # 4
Parameters:
listthe list
Returns the number of keys in list.
in?/2
List.in?(["bob", "alice"], "alice") # true
List.in?(["bob", "alice"], "fred") # false
Parameters:
listthe haystackelementthe needle
Returns whether the needle is in the haystack.
empty?/1
List.empty?([]) # true
List.empty?(["apple"]) # false
Parameters:
listthe list
Returns whether the list is empty or not.
concat/2
List.concat([], []) # []
List.concat([1,2], [3,4]) # [1,2,3,4]
Parameters:
list1a listlist2another list
Returns a new list where list2 is concatenated to list1.
append/2
List.append([], 1) # [1]
List.append([1], 2) # [1,2]
Parameters:
listthe listelementthe element
Returns a new list where element is appended to the end of list.
If order doesn't matter, we suggest to use prepend/2 instead (Complexity: O(1) instead of O(n)).
prepend/2
List.prepend([], 1) # [1]
List.prepend([1], 0) # [0,1]
Parameters:
listthe listelementthe element
Returns a new list where element is appended to the start of list.
join/2
List.join([], ", ") # ""
List.join(["Mike", "Robert", "Joe"], ", ") # "Mike, Robert, Joe"
Parameters:
listthe list of stringsseparatorthe separator
Returns a string where all list's elements have been joined with separator separator.
sort/1
List.sort([1,3,2]) # [1,2,3]
Parameters:
listthe list of maps
Returns same list but ordered ASC.
sort_by/2
List.sort_by([[a: 1], [a: 3], [a: 2]], "a") # [[a: 1], [a: 2], [a: 3]]
Parameters:
listthe list of mapsfieldthe field of the map to use for sorting
Returns same list but ordered ASC by the field value of each items.
uniq/1
List.uniq([1,2,1,2,3]) # [1,2,3]
Parameters:
listthe list
Returns same list but without duplicated elements.
reverse/1
List.reverse([1,2,2,3]) # [3,2,2,1]
Parameters:
listthe list
Returns same list but reversed.
set_at/3
List.set_at(["index0", "index1"], 1, "value") # ["index0", "value"]
Parameters:
listthe listindex0-based indexvaluethe value to set at index
Returns same list but element at index index is replaced by value.
delete_at/3
List.delete_at(["index0", "index1"], 1) # ["index0"]
Parameters:
listthe listindex0-based index
Returns same list but element at index index is deleted.