@@ -462,14 +462,21 @@ reduce(op, a::Number) = a # Do we want this?
462
462
# # sum
463
463
464
464
"""
465
- sum(f, itr)
465
+ sum(f, itr; init )
466
466
467
467
Sum the results of calling function `f` on each element of `itr`.
468
468
469
469
The return type is `Int` for signed integers of less than system word size, and
470
470
`UInt` for unsigned integers of less than system word size. For all other
471
471
arguments, a common return type is found to which all arguments are promoted.
472
472
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
+
473
480
# Examples
474
481
```jldoctest
475
482
julia> sum(abs2, [2; 3; 4])
@@ -491,60 +498,83 @@ In the former case, the integers are widened to system word size and therefore
491
498
the result is 128. In the latter case, no such widening happens and integer
492
499
overflow results in -128.
493
500
"""
494
- sum (f, a) = mapreduce (f, add_sum, a)
501
+ sum (f, a; kw ... ) = mapreduce (f, add_sum, a; kw ... )
495
502
496
503
"""
497
- sum(itr)
504
+ sum(itr; init )
498
505
499
506
Returns the sum of all elements in a collection.
500
507
501
508
The return type is `Int` for signed integers of less than system word size, and
502
509
`UInt` for unsigned integers of less than system word size. For all other
503
510
arguments, a common return type is found to which all arguments are promoted.
504
511
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
+
505
519
# Examples
506
520
```jldoctest
507
521
julia> sum(1:20)
508
522
210
509
523
```
510
524
"""
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.
513
529
514
530
# # prod
515
531
"""
516
- prod(f, itr)
532
+ prod(f, itr; init )
517
533
518
534
Returns the product of `f` applied to each element of `itr`.
519
535
520
536
The return type is `Int` for signed integers of less than system word size, and
521
537
`UInt` for unsigned integers of less than system word size. For all other
522
538
arguments, a common return type is found to which all arguments are promoted.
523
539
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
+
524
547
# Examples
525
548
```jldoctest
526
549
julia> prod(abs2, [2; 3; 4])
527
550
576
528
551
```
529
552
"""
530
- prod (f, a) = mapreduce (f, mul_prod, a)
553
+ prod (f, a; kw ... ) = mapreduce (f, mul_prod, a; kw ... )
531
554
532
555
"""
533
- prod(itr)
556
+ prod(itr; init )
534
557
535
558
Returns the product of all elements of a collection.
536
559
537
560
The return type is `Int` for signed integers of less than system word size, and
538
561
`UInt` for unsigned integers of less than system word size. For all other
539
562
arguments, a common return type is found to which all arguments are promoted.
540
563
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
+
541
571
# Examples
542
572
```jldoctest
543
573
julia> prod(1:20)
544
574
2432902008176640000
545
575
```
546
576
"""
547
- prod (a) = mapreduce (identity, mul_prod, a)
577
+ prod (a; kw ... ) = mapreduce (identity, mul_prod, a; kw ... )
548
578
549
579
# # maximum & minimum
550
580
_fast (:: typeof (min),x,y) = min (x,y)
0 commit comments