@@ -45,7 +45,7 @@ function mapfoldl_impl(f::F, op::OP, nt, itr) where {F,OP}
45
45
end
46
46
47
47
function 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)
49
49
v isa _InitialValue && return reduce_empty_iter (op, itr)
50
50
return v
51
51
end
@@ -157,7 +157,7 @@ Like [`mapreduce`](@ref), but with guaranteed left associativity, as in [`foldl`
157
157
If provided, the keyword argument `init` will be used exactly once. In general, it will be
158
158
necessary to provide `init` to work with empty collections.
159
159
"""
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)
161
161
162
162
"""
163
163
foldl(op, itr; [init])
@@ -200,7 +200,7 @@ Like [`mapreduce`](@ref), but with guaranteed right associativity, as in [`foldr
200
200
provided, the keyword argument `init` will be used exactly once. In general, it will be
201
201
necessary to provide `init` to work with empty collections.
202
202
"""
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)
204
204
205
205
206
206
"""
@@ -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`. 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
+
473
480
# Examples
474
481
```jldoctest
475
482
julia> sum(abs2, [2; 3; 4])
@@ -491,60 +498,88 @@ 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`. 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
+
505
519
# Examples
506
520
```jldoctest
507
521
julia> sum(1:20)
508
522
210
523
+
524
+ julia> sum(1:20; init = 0.0)
525
+ 210.0
509
526
```
510
527
"""
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... )
513
531
514
532
# # prod
515
533
"""
516
- prod(f, itr)
534
+ prod(f, itr; [init] )
517
535
518
536
Returns the product of `f` applied to each element of `itr`.
519
537
520
538
The return type is `Int` for signed integers of less than system word size, and
521
539
`UInt` for unsigned integers of less than system word size. For all other
522
540
arguments, a common return type is found to which all arguments are promoted.
523
541
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
+
524
549
# Examples
525
550
```jldoctest
526
551
julia> prod(abs2, [2; 3; 4])
527
552
576
528
553
```
529
554
"""
530
- prod (f, a) = mapreduce (f, mul_prod, a)
555
+ prod (f, a; kw ... ) = mapreduce (f, mul_prod, a; kw ... )
531
556
532
557
"""
533
- prod(itr)
558
+ prod(itr; [init] )
534
559
535
560
Returns the product of all elements of a collection.
536
561
537
562
The return type is `Int` for signed integers of less than system word size, and
538
563
`UInt` for unsigned integers of less than system word size. For all other
539
564
arguments, a common return type is found to which all arguments are promoted.
540
565
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
+
541
573
# Examples
542
574
```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
545
580
```
546
581
"""
547
- prod (a) = mapreduce (identity, mul_prod, a)
582
+ prod (a; kw ... ) = mapreduce (identity, mul_prod, a; kw ... )
548
583
549
584
# # maximum & minimum
550
585
_fast (:: typeof (min),x,y) = min (x,y)
@@ -610,62 +645,122 @@ function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
610
645
end
611
646
612
647
"""
613
- maximum(f, itr)
648
+ maximum(f, itr; [init] )
614
649
615
650
Returns the largest result of calling function `f` on each element of `itr`.
616
651
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
+
617
660
# Examples
618
661
```jldoctest
619
662
julia> maximum(length, ["Julion", "Julia", "Jule"])
620
663
6
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
621
670
```
622
671
"""
623
- maximum (f, a) = mapreduce (f, max, a)
672
+ maximum (f, a; kw ... ) = mapreduce (f, max, a; kw ... )
624
673
625
674
"""
626
- minimum(f, itr)
675
+ minimum(f, itr; [init] )
627
676
628
677
Returns the smallest result of calling function `f` on each element of `itr`.
629
678
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
+
630
687
# Examples
631
688
```jldoctest
632
689
julia> minimum(length, ["Julion", "Julia", "Jule"])
633
690
4
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
634
697
```
635
698
"""
636
- minimum (f, a) = mapreduce (f, min, a)
699
+ minimum (f, a; kw ... ) = mapreduce (f, min, a; kw ... )
637
700
638
701
"""
639
- maximum(itr)
702
+ maximum(itr; [init] )
640
703
641
704
Returns the largest element in a collection.
642
705
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
+
643
714
# Examples
644
715
```jldoctest
645
716
julia> maximum(-20.5:10)
646
717
9.5
647
718
648
719
julia> maximum([1,2,3])
649
720
3
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
650
729
```
651
730
"""
652
- maximum (a) = mapreduce (identity, max, a)
731
+ maximum (a; kw ... ) = mapreduce (identity, max, a; kw ... )
653
732
654
733
"""
655
- minimum(itr)
734
+ minimum(itr; [init] )
656
735
657
736
Returns the smallest element in a collection.
658
737
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
+
659
746
# Examples
660
747
```jldoctest
661
748
julia> minimum(-20.5:10)
662
749
-20.5
663
750
664
751
julia> minimum([1,2,3])
665
752
1
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
666
761
```
667
762
"""
668
- minimum (a) = mapreduce (identity, min, a)
763
+ minimum (a; kw ... ) = mapreduce (identity, min, a; kw ... )
669
764
670
765
# # all & any
671
766
0 commit comments