Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 0e5f95b

Browse files
committed
Merge pull request JuliaLang#4782 from stevengj/pushitr
support multi-arg push! and unshift!
2 parents be9421e + d48681e commit 0e5f95b

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

base/abstractarray.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1704,3 +1704,12 @@ function map(f::Callable, As::AbstractArray...)
17041704
dest = similar(As[1], typeof(first), shape)
17051705
return map_to!(f, first, dest, As...)
17061706
end
1707+
1708+
# multi-item push!, unshift! (built on top of type-specific 1-item version)
1709+
# (note: must not cause a dispatch loop when 1-item case is not defined)
1710+
push!(A) = A
1711+
push!(A, a, b) = push!(push!(A, a), b)
1712+
push!(A, a, b, c...) = push!(push!(A, a, b), c...)
1713+
unshift!(A) = A
1714+
unshift!(A, a, b) = unshift!(unshift!(A, b), a)
1715+
unshift!(A, a, b, c...) = unshift!(unshift!(A, c...), a, b)

doc/helpdb.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,9 @@
11571157
11581158
"),
11591159

1160-
("Dequeues","Base","push!","push!(collection, item) -> collection
1160+
("Dequeues","Base","push!","push!(collection, items...) -> collection
11611161
1162-
Insert an item at the end of a collection.
1162+
Insert items at the end of a collection.
11631163
11641164
"),
11651165

@@ -1169,9 +1169,9 @@
11691169
11701170
"),
11711171

1172-
("Dequeues","Base","unshift!","unshift!(collection, item) -> collection
1172+
("Dequeues","Base","unshift!","unshift!(collection, items...) -> collection
11731173
1174-
Insert an item at the beginning of a collection.
1174+
Insert items at the beginning of a collection.
11751175
11761176
"),
11771177

doc/stdlib/base.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -769,17 +769,17 @@ Partially implemented by: ``Array``.
769769
Dequeues
770770
--------
771771

772-
.. function:: push!(collection, item) -> collection
772+
.. function:: push!(collection, items...) -> collection
773773

774-
Insert an item at the end of a collection.
774+
Insert items at the end of a collection.
775775

776776
.. function:: pop!(collection) -> item
777777

778778
Remove the last item in a collection and return it.
779779

780-
.. function:: unshift!(collection, item) -> collection
780+
.. function:: unshift!(collection, items...) -> collection
781781

782-
Insert an item at the beginning of a collection.
782+
Insert items at the beginning of a collection.
783783

784784
.. function:: shift!(collection) -> item
785785

test/arrayops.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,19 @@ let
204204
end
205205

206206
## arrays as dequeues
207-
l = {1,2,3}
208-
push!(l,8)
207+
l = {1}
208+
push!(l,2,3,8)
209209
@test l[1]==1 && l[2]==2 && l[3]==3 && l[4]==8
210210
v = pop!(l)
211211
@test v == 8
212212
v = pop!(l)
213213
@test v == 3
214214
@test length(l)==2
215+
unshift!(l,4,7,5)
216+
@test l[1]==4 && l[2]==7 && l[3]==5 && l[4]==1 && l[5]==2
217+
v = shift!(l)
218+
@test v == 4
219+
@test length(l)==4
215220

216221
# concatenation
217222
@test isequal([ones(2,2) 2*ones(2,1)], [1. 1 2; 1 1 2])

0 commit comments

Comments
 (0)