Skip to content

Commit cd96085

Browse files
committed
Update docs for chunk
1 parent fffe72e commit cd96085

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/utils.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ unstack(xs; dims::Int) = [copy(selectdim(xs, dims, i)) for i in 1:size(xs, dims)
122122

123123
"""
124124
chunk(x, n; [dims])
125+
chunk(x; [size, dims])
125126
126-
Split `x` into `n` parts. The parts contain the same number of elements
127-
except possibly for the last one that can be smaller.
127+
Split `x` into `n` parts or alternatively, into equal chunks of size `size`. The parts contain
128+
the same number of elements except possibly for the last one that can be smaller.
128129
129130
If `x` is an array, `dims` can be used to specify along which dimension to
130131
split (defaults to the last dimension).
@@ -138,6 +139,14 @@ julia> chunk(1:10, 3)
138139
5:8
139140
9:10
140141
142+
julia> chunk(1:10; size = 2)
143+
5-element Vector{UnitRange{Int64}}:
144+
1:2
145+
3:4
146+
5:6
147+
7:8
148+
9:10
149+
141150
julia> x = reshape(collect(1:20), (5, 4))
142151
5×4 Matrix{Int64}:
143152
1 6 11 16
@@ -156,6 +165,19 @@ julia> xs[1]
156165
1 6 11 16
157166
2 7 12 17
158167
3 8 13 18
168+
169+
julia> xes = chunk(x; size = 2, dims = 2)
170+
2-element Vector{SubArray{Int64, 2, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}}:
171+
[1 6; 2 7; … ; 4 9; 5 10]
172+
[11 16; 12 17; … ; 14 19; 15 20]
173+
174+
julia> xes[2]
175+
5×2 view(::Matrix{Int64}, :, 3:4) with eltype Int64:
176+
11 16
177+
12 17
178+
13 18
179+
14 19
180+
15 20
159181
```
160182
"""
161183
chunk(x; size::Int) = collect(Iterators.partition(x, size))

0 commit comments

Comments
 (0)