shift!
shift!(collection) -> item
Remove the first item
from collection
.
julia> A = [1, 2, 3, 4, 5, 6]
6-element Array{Int64,1}:
1
2
3
4
5
6
julia> shift!(A)
1
julia> A
5-element Array{Int64,1}:
2
3
4
5
6
Examples
julia> a = [1, 2, 3]
shift!(a)
1
julia> print(a)
[2,3]
julia> a = [1, 2, 3]
shift!(a)
shift!(a)
print(a)
[3]
julia> foo = [1, 2, 3];
3-element Array{Int64,1}:
1
2
3
julia> shift!(foo);
1
julia> foo
2-element Array{Int64,1}:
2
3
-
Remove the first item from an array:
julia> A = [1, 2, 3, 4, 5, 6]; julia> shift!(A) 1
This example removes the first item (1) from the array
A
. -
Modify a vector of strings:
julia> words = ["apple", "banana", "orange", "grape"]; julia> shift!(words) "apple"
It removes the first string ("apple") from the vector of strings.
- Handle edge cases when shifting an empty collection:
julia> empty_array = Int64[]; julia> shift!(empty_array) ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [1]
When trying to shift an empty array, an error is thrown as there are no items to remove.
Common mistake example:
julia> A = [1, 2, 3];
julia> shift!(A, 2)
ERROR: MethodError: no method matching shift!(::Array{Int64,1}, ::Int64)
In this example, the shift!
function is mistakenly called with two arguments. The function only expects the collection
argument and not an additional index. Make sure to call the function with the correct number of arguments to avoid this error.
See Also
append!, delete!, deleteat!, empty!, endof, filter, filter!, gc, get!, getkey, haskey, insert!, isempty, keys, map, map!, merge, merge!, pop!, prepend!, push!, reduce, resize!, shift!, splice!, unshift!, values,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.