@@ -45,7 +45,7 @@ function mapfoldl_impl(f::F, op::OP, nt, itr) where {F,OP}
4545end
4646
4747function foldl_impl (op:: OP , nt, itr) where {OP}
48- v = _foldl_impl (op, get (nt, :init , _InitialValue ()) , itr)
48+ v = _foldl_impl (op, nt , itr)
4949 v isa _InitialValue && return reduce_empty_iter (op, itr)
5050 return v
5151end
@@ -157,7 +157,7 @@ Like [`mapreduce`](@ref), but with guaranteed left associativity, as in [`foldl`
157157If provided, the keyword argument `init` will be used exactly once. In general, it will be
158158necessary to provide `init` to work with empty collections.
159159"""
160- mapfoldl (f, op, itr; kw ... ) = mapfoldl_impl (f, op, kw . data , itr)
160+ mapfoldl (f, op, itr; init = _InitialValue ()) = mapfoldl_impl (f, op, init , itr)
161161
162162"""
163163 foldl(op, itr; [init])
@@ -200,7 +200,7 @@ Like [`mapreduce`](@ref), but with guaranteed right associativity, as in [`foldr
200200provided, the keyword argument `init` will be used exactly once. In general, it will be
201201necessary to provide `init` to work with empty collections.
202202"""
203- mapfoldr (f, op, itr; kw ... ) = mapfoldr_impl (f, op, kw . data , itr)
203+ mapfoldr (f, op, itr; init = _InitialValue ()) = mapfoldr_impl (f, op, init , itr)
204204
205205
206206"""
@@ -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
467467Sum the results of calling function `f` on each element of `itr`.
468468
469469The 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
471471arguments, 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`. It must be
474+ the additive identity (i.e. zero) as it is unspecified whether `init` is used
475+ for non-empty collections.
476+
477+ !!! compat "Julia 1.6"
478+ Keyword argument `init` requires Julia 1.6 or later.
479+
473480# Examples
474481```jldoctest
475482julia> sum(abs2, [2; 3; 4])
@@ -491,60 +498,88 @@ In the former case, the integers are widened to system word size and therefore
491498the result is 128. In the latter case, no such widening happens and integer
492499overflow 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
499506Returns the sum of all elements in a collection.
500507
501508The 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
503510arguments, 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`. It must be
513+ the additive identity (i.e. zero) as it is unspecified whether `init` is used
514+ for non-empty collections.
515+
516+ !!! compat "Julia 1.6"
517+ Keyword argument `init` requires Julia 1.6 or later.
518+
505519# Examples
506520```jldoctest
507521julia> sum(1:20)
508522210
523+
524+ julia> sum(1:20; init = 0.0)
525+ 210.0
509526```
510527"""
511- sum (a) = sum (identity, a)
512- sum (a:: AbstractArray{Bool} ) = count (a)
528+ sum (a; kw... ) = sum (identity, a; kw... )
529+ sum (a:: AbstractArray{Bool} ; kw... ) =
530+ kw. data === NamedTuple () ? count (a) : reduce (add_sum, a; kw... )
513531
514532# # prod
515533"""
516- prod(f, itr)
534+ prod(f, itr; [init] )
517535
518536Returns the product of `f` applied to each element of `itr`.
519537
520538The return type is `Int` for signed integers of less than system word size, and
521539`UInt` for unsigned integers of less than system word size. For all other
522540arguments, a common return type is found to which all arguments are promoted.
523541
542+ The value returned for empty `itr` can be specified by `init`. It must be the
543+ multiplicative identity (i.e. one) as it is unspecified whether `init` is used
544+ for non-empty collections.
545+
546+ !!! compat "Julia 1.6"
547+ Keyword argument `init` requires Julia 1.6 or later.
548+
524549# Examples
525550```jldoctest
526551julia> prod(abs2, [2; 3; 4])
527552576
528553```
529554"""
530- prod (f, a) = mapreduce (f, mul_prod, a)
555+ prod (f, a; kw ... ) = mapreduce (f, mul_prod, a; kw ... )
531556
532557"""
533- prod(itr)
558+ prod(itr; [init] )
534559
535560Returns the product of all elements of a collection.
536561
537562The return type is `Int` for signed integers of less than system word size, and
538563`UInt` for unsigned integers of less than system word size. For all other
539564arguments, a common return type is found to which all arguments are promoted.
540565
566+ The value returned for empty `itr` can be specified by `init`. It must be the
567+ multiplicative identity (i.e. one) as it is unspecified whether `init` is used
568+ for non-empty collections.
569+
570+ !!! compat "Julia 1.6"
571+ Keyword argument `init` requires Julia 1.6 or later.
572+
541573# Examples
542574```jldoctest
543- julia> prod(1:20)
544- 2432902008176640000
575+ julia> prod(1:5)
576+ 120
577+
578+ julia> prod(1:5; init = 1.0)
579+ 120.0
545580```
546581"""
547- prod (a) = mapreduce (identity, mul_prod, a)
582+ prod (a; kw ... ) = mapreduce (identity, mul_prod, a; kw ... )
548583
549584# # maximum & minimum
550585_fast (:: typeof (min),x,y) = min (x,y)
@@ -610,62 +645,122 @@ function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
610645end
611646
612647"""
613- maximum(f, itr)
648+ maximum(f, itr; [init] )
614649
615650Returns the largest result of calling function `f` on each element of `itr`.
616651
652+ The value returned for empty `itr` can be specified by `init`. It must be
653+ a neutral element for `max` (i.e. which is less than or equal to any
654+ other element) as it is unspecified whether `init` is used
655+ for non-empty collections.
656+
657+ !!! compat "Julia 1.6"
658+ Keyword argument `init` requires Julia 1.6 or later.
659+
617660# Examples
618661```jldoctest
619662julia> maximum(length, ["Julion", "Julia", "Jule"])
6206636
664+
665+ julia> maximum(length, []; init=-1)
666+ -1
667+
668+ julia> maximum(sin, Real[]; init=-1.0) # good, since output of sin is >= -1
669+ -1.0
621670```
622671"""
623- maximum (f, a) = mapreduce (f, max, a)
672+ maximum (f, a; kw ... ) = mapreduce (f, max, a; kw ... )
624673
625674"""
626- minimum(f, itr)
675+ minimum(f, itr; [init] )
627676
628677Returns the smallest result of calling function `f` on each element of `itr`.
629678
679+ The value returned for empty `itr` can be specified by `init`. It must be
680+ a neutral element for `min` (i.e. which is greater than or equal to any
681+ other element) as it is unspecified whether `init` is used
682+ for non-empty collections.
683+
684+ !!! compat "Julia 1.6"
685+ Keyword argument `init` requires Julia 1.6 or later.
686+
630687# Examples
631688```jldoctest
632689julia> minimum(length, ["Julion", "Julia", "Jule"])
6336904
691+
692+ julia> minimum(length, []; init=typemax(Int64))
693+ 9223372036854775807
694+
695+ julia> minimum(sin, Real[]; init=1.0) # good, since output of sin is <= 1
696+ 1.0
634697```
635698"""
636- minimum (f, a) = mapreduce (f, min, a)
699+ minimum (f, a; kw ... ) = mapreduce (f, min, a; kw ... )
637700
638701"""
639- maximum(itr)
702+ maximum(itr; [init] )
640703
641704Returns the largest element in a collection.
642705
706+ The value returned for empty `itr` can be specified by `init`. It must be
707+ a neutral element for `max` (i.e. which is less than or equal to any
708+ other element) as it is unspecified whether `init` is used
709+ for non-empty collections.
710+
711+ !!! compat "Julia 1.6"
712+ Keyword argument `init` requires Julia 1.6 or later.
713+
643714# Examples
644715```jldoctest
645716julia> maximum(-20.5:10)
6467179.5
647718
648719julia> maximum([1,2,3])
6497203
721+
722+ julia> maximum(())
723+ ERROR: ArgumentError: reducing over an empty collection is not allowed
724+ Stacktrace:
725+ [...]
726+
727+ julia> maximum((); init=-Inf)
728+ -Inf
650729```
651730"""
652- maximum (a) = mapreduce (identity, max, a)
731+ maximum (a; kw ... ) = mapreduce (identity, max, a; kw ... )
653732
654733"""
655- minimum(itr)
734+ minimum(itr; [init] )
656735
657736Returns the smallest element in a collection.
658737
738+ The value returned for empty `itr` can be specified by `init`. It must be
739+ a neutral element for `min` (i.e. which is greater than or equal to any
740+ other element) as it is unspecified whether `init` is used
741+ for non-empty collections.
742+
743+ !!! compat "Julia 1.6"
744+ Keyword argument `init` requires Julia 1.6 or later.
745+
659746# Examples
660747```jldoctest
661748julia> minimum(-20.5:10)
662749-20.5
663750
664751julia> minimum([1,2,3])
6657521
753+
754+ julia> minimum([])
755+ ERROR: ArgumentError: reducing over an empty collection is not allowed
756+ Stacktrace:
757+ [...]
758+
759+ julia> minimum([]; init=Inf)
760+ Inf
666761```
667762"""
668- minimum (a) = mapreduce (identity, min, a)
763+ minimum (a; kw ... ) = mapreduce (identity, min, a; kw ... )
669764
670765# # all & any
671766
0 commit comments