Skip to content

Commit a9fd8b8

Browse files
committed
Add init keyword argument to sum and prod
1 parent 98e678f commit a9fd8b8

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

base/reduce.jl

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,21 @@ reduce(op, a::Number) = a # Do we want this?
462462
## sum
463463

464464
"""
465-
sum(f, itr)
465+
sum(f, itr; init)
466466
467467
Sum the results of calling function `f` on each element of `itr`.
468468
469469
The return type is `Int` for signed integers of less than system word size, and
470470
`UInt` for unsigned integers of less than system word size. For all other
471471
arguments, a common return type is found to which all arguments are promoted.
472472
473+
The value returned for empty `itr` can be specified by `init` which must be the
474+
additive identity. It is unspecified whether `init` is used for non-empty
475+
collections.
476+
477+
!!! compat "Julia 1.6"
478+
Keyword argument `init` requires Julia 1.6 or later.
479+
473480
# Examples
474481
```jldoctest
475482
julia> sum(abs2, [2; 3; 4])
@@ -491,60 +498,83 @@ In the former case, the integers are widened to system word size and therefore
491498
the result is 128. In the latter case, no such widening happens and integer
492499
overflow results in -128.
493500
"""
494-
sum(f, a) = mapreduce(f, add_sum, a)
501+
sum(f, a; kw...) = mapreduce(f, add_sum, a; kw...)
495502

496503
"""
497-
sum(itr)
504+
sum(itr; init)
498505
499506
Returns the sum of all elements in a collection.
500507
501508
The return type is `Int` for signed integers of less than system word size, and
502509
`UInt` for unsigned integers of less than system word size. For all other
503510
arguments, a common return type is found to which all arguments are promoted.
504511
512+
The value returned for empty `itr` can be specified by `init` which must be the
513+
additive identity. It is unspecified whether `init` is used for non-empty
514+
collections.
515+
516+
!!! compat "Julia 1.6"
517+
Keyword argument `init` requires Julia 1.6 or later.
518+
505519
# Examples
506520
```jldoctest
507521
julia> sum(1:20)
508522
210
509523
```
510524
"""
511-
sum(a) = sum(identity, a)
512-
sum(a::AbstractArray{Bool}) = count(a)
525+
sum(a; kw...) = sum(identity, a; kw...)
526+
sum(a::AbstractArray{Bool}; kw...) = count(a)
527+
# Note: It is OK to ignore `init` to `sum(::AbstractArray{Bool})`
528+
# because it is unspecified if the value of `init` is used or not.
513529

514530
## prod
515531
"""
516-
prod(f, itr)
532+
prod(f, itr; init)
517533
518534
Returns the product of `f` applied to each element of `itr`.
519535
520536
The return type is `Int` for signed integers of less than system word size, and
521537
`UInt` for unsigned integers of less than system word size. For all other
522538
arguments, a common return type is found to which all arguments are promoted.
523539
540+
The value returned for empty `itr` can be specified by `init` which must be the
541+
multiplicative identity. It is unspecified whether `init` is used for non-empty
542+
collections.
543+
544+
!!! compat "Julia 1.6"
545+
Keyword argument `init` requires Julia 1.6 or later.
546+
524547
# Examples
525548
```jldoctest
526549
julia> prod(abs2, [2; 3; 4])
527550
576
528551
```
529552
"""
530-
prod(f, a) = mapreduce(f, mul_prod, a)
553+
prod(f, a; kw...) = mapreduce(f, mul_prod, a; kw...)
531554

532555
"""
533-
prod(itr)
556+
prod(itr; init)
534557
535558
Returns the product of all elements of a collection.
536559
537560
The return type is `Int` for signed integers of less than system word size, and
538561
`UInt` for unsigned integers of less than system word size. For all other
539562
arguments, a common return type is found to which all arguments are promoted.
540563
564+
The value returned for empty `itr` can be specified by `init` which must be the
565+
multiplicative identity. It is unspecified whether `init` is used for non-empty
566+
collections.
567+
568+
!!! compat "Julia 1.6"
569+
Keyword argument `init` requires Julia 1.6 or later.
570+
541571
# Examples
542572
```jldoctest
543573
julia> prod(1:20)
544574
2432902008176640000
545575
```
546576
"""
547-
prod(a) = mapreduce(identity, mul_prod, a)
577+
prod(a; kw...) = mapreduce(identity, mul_prod, a; kw...)
548578

549579
## maximum & minimum
550580
_fast(::typeof(min),x,y) = min(x,y)

0 commit comments

Comments
 (0)