Skip to content

Commit f12cde0

Browse files
emstoudenmireViralBShahtkf
authored
Created a docstring for ntuple(f::F, ::Val{N}) (#32468)
* Created a docstring for ntuple(f::F, ::Val{N}) * Incorporate fredrikekre's suggestions * Incorporated more suggestions into text. * Added note about using integer input when N not known at compile time * Update base/ntuple.jl Co-authored-by: Takafumi Arakaki <takafumi.a@gmail.com> * Update ntuple docstring to be more consistent with method signature * Remove trailing whitespace in docstring Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com> Co-authored-by: Takafumi Arakaki <takafumi.a@gmail.com>
1 parent 97e3fe8 commit f12cde0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

base/ntuple.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ ntuple(f, ::Val{1}) = (@_inline_meta; (f(1),))
4242
ntuple(f, ::Val{2}) = (@_inline_meta; (f(1), f(2)))
4343
ntuple(f, ::Val{3}) = (@_inline_meta; (f(1), f(2), f(3)))
4444

45+
"""
46+
ntuple(f, ::Val{N})
47+
48+
Create a tuple of length `N`, computing each element as `f(i)`,
49+
where `i` is the index of the element. By taking a `Val(N)`
50+
argument, it is possible that this version of ntuple may
51+
generate more efficient code than the version taking the
52+
length as an integer. But `ntuple(f, N)` is preferable to
53+
`ntuple(f, Val(N))` in cases where `N` cannot be determined
54+
at compile time.
55+
56+
# Examples
57+
```jldoctest
58+
julia> ntuple(i -> 2*i, Val(4))
59+
(2, 4, 6, 8)
60+
```
61+
"""
4562
@inline function ntuple(f::F, ::Val{N}) where {F,N}
4663
N::Int
4764
(N >= 0) || throw(ArgumentError(string("tuple length should be ≥ 0, got ", N)))

0 commit comments

Comments
 (0)