Let say I have a list [1, 2, 3, 4]
How can I get all elements from this list except last? So, I'll have [1, 2, 3]
You can either do [head | tail]. = list (If you need the first element, head, as well) or tail = tl(list) (if you don't need the head) to get the tail of a list, then just map over that.
The last() function of the List module returns the last item in the list or nil. int length = sizeof(items) / sizeof(items[0]); int x = items[length - 1];
The length() function returns the length of the list that is passed as a parameter.
Lists are a basic data type in Elixir for holding a collection of values. Lists are immutable, meaning they cannot be modified. Any operation that changes a list returns a new list. Lists implement the Enumerable protocol, which allows the use of Enum and Stream module functions.
Use Enum.drop/2 like this:
list = [1, 2, 3, 4] Enum.drop list, -1 # [1, 2, 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With