Skip to content

Commit 7bda2c1

Browse files
mortenpifredrikekre
authored andcommitted
Fix Distributed.head_and_tail (#32431)
The last return statement is currently referring to a non-existent s variable. Also fix the related doctest, even though it does not get tested at the moment.
1 parent fa766bc commit 7bda2c1

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

stdlib/Distributed/src/pmap.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,11 @@ Return `head`: the first `n` elements of `c`;
238238
and `tail`: an iterator over the remaining elements.
239239
240240
```jldoctest
241-
julia> a = 1:10
242-
1:10
243-
244-
julia> b, c = Base.head_and_tail(a, 3)
245-
([1,2,3],Base.Iterators.Rest{UnitRange{Int64},Int64}(1:10,4))
241+
julia> b, c = Distributed.head_and_tail(1:10, 3)
242+
([1, 2, 3], Base.Iterators.Rest{UnitRange{Int64},Int64}(1:10, 3))
246243
247244
julia> collect(c)
248-
7-element Array{Any,1}:
245+
7-element Array{Int64,1}:
249246
4
250247
5
251248
6
@@ -268,7 +265,7 @@ function head_and_tail(c, n)
268265
i += 1
269266
head[i] = y[1]
270267
end
271-
return head, Iterators.rest(c, s)
268+
return head, Iterators.rest(c, y[2])
272269
end
273270

274271
"""

stdlib/Distributed/test/distributed_exec.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,32 @@ let code = """
16261626
@test success(`$(Base.julia_cmd()) --startup-file=no -e $code`)
16271627
end
16281628

1629+
# PR 32431: tests for internal Distributed.head_and_tail
1630+
let (h, t) = Distributed.head_and_tail(1:10, 3)
1631+
@test h == 1:3
1632+
@test collect(t) == 4:10
1633+
end
1634+
let (h, t) = Distributed.head_and_tail(1:10, 0)
1635+
@test h == []
1636+
@test collect(t) == 1:10
1637+
end
1638+
let (h, t) = Distributed.head_and_tail(1:3, 5)
1639+
@test h == 1:3
1640+
@test collect(t) == []
1641+
end
1642+
let (h, t) = Distributed.head_and_tail(1:3, 3)
1643+
@test h == 1:3
1644+
@test collect(t) == []
1645+
end
1646+
let (h, t) = Distributed.head_and_tail(Int[], 3)
1647+
@test h == []
1648+
@test collect(t) == []
1649+
end
1650+
let (h, t) = Distributed.head_and_tail(Int[], 0)
1651+
@test h == []
1652+
@test collect(t) == []
1653+
end
1654+
16291655
# Run topology tests last after removing all workers, since a given
16301656
# cluster at any time only supports a single topology.
16311657
rmprocs(workers())

0 commit comments

Comments
 (0)