@@ -42,7 +42,8 @@ mul_prod(x::SmallUnsigned,y::SmallUnsigned) = UInt(x) * UInt(y)
4242
4343# # foldl && mapfoldl
4444
45- @noinline function mapfoldl_impl (f, op, init, itr, i... )
45+ @noinline function mapfoldl_impl (f, op, nt:: NamedTuple{(:init,)} , itr, i... )
46+ init = nt. init
4647 # Unroll the while loop once; if init is known, the call to op may
4748 # be evaluated at compile time
4849 y = iterate (itr, i... )
@@ -56,32 +57,32 @@ mul_prod(x::SmallUnsigned,y::SmallUnsigned) = UInt(x) * UInt(y)
5657 return v
5758end
5859
59- function mapfoldl_impl (f, op, :: NoInit , itr)
60+ function mapfoldl_impl (f, op, nt :: NamedTuple{()} , itr)
6061 y = iterate (itr)
6162 if y === nothing
6263 return Base. mapreduce_empty_iter (f, op, itr, IteratorEltype (itr))
6364 end
6465 (x, i) = y
6566 init = mapreduce_first (f, op, x)
66- mapfoldl_impl (f, op, init, itr, i)
67+ mapfoldl_impl (f, op, ( init= init,) , itr, i)
6768end
6869
6970
7071"""
71- mapfoldl(f, op, itr; init=Base.NoInit() )
72+ mapfoldl(f, op, itr; [ init] )
7273
7374Like [`mapreduce`](@ref), but with guaranteed left associativity, as in [`foldl`](@ref).
74- If provided, `init` will be used exactly once. In general, it will be necessary to provide
75- `init` to work with empty collections.
75+ If provided, the keyword argument `init` will be used exactly once. In general, it will be
76+ necessary to provide `init` to work with empty collections.
7677"""
77- mapfoldl (f, op, itr; init = NoInit ()) = mapfoldl_impl (f, op, init , itr)
78+ mapfoldl (f, op, itr; kw ... ) = mapfoldl_impl (f, op, kw . data , itr)
7879
7980"""
80- foldl(op, itr; init=Base.NoInit() )
81+ foldl(op, itr; [ init] )
8182
82- Like [`reduce`](@ref), but with guaranteed left associativity. If provided, `init` will be
83- used exactly once. In general, it will be necessary to provide `init` to work with empty
84- collections.
83+ Like [`reduce`](@ref), but with guaranteed left associativity. If provided, the keyword
84+ argument `init` will be used exactly once. In general, it will be necessary to provide
85+ `init` to work with empty collections.
8586
8687# Examples
8788```jldoctest
@@ -92,11 +93,12 @@ julia> foldl(=>, 1:4; init=0)
9293(((0=>1)=>2)=>3) => 4
9394```
9495"""
95- foldl (op, itr; init = NoInit ()) = mapfoldl (identity, op, itr; init = init )
96+ foldl (op, itr; kw ... ) = mapfoldl (identity, op, itr; kw ... )
9697
9798# # foldr & mapfoldr
9899
99- function mapfoldr_impl (f, op, init, itr, i:: Integer )
100+ function mapfoldr_impl (f, op, nt:: NamedTuple{(:init,)} , itr, i:: Integer )
101+ init = nt. init
100102 # Unroll the while loop once; if init is known, the call to op may
101103 # be evaluated at compile time
102104 if isempty (itr) || i == 0
@@ -112,29 +114,29 @@ function mapfoldr_impl(f, op, init, itr, i::Integer)
112114 end
113115end
114116
115- function mapfoldr_impl (f, op, :: NoInit , itr, i:: Integer )
117+ function mapfoldr_impl (f, op, :: NamedTuple{()} , itr, i:: Integer )
116118 if isempty (itr)
117119 return Base. mapreduce_empty_iter (f, op, itr, IteratorEltype (itr))
118120 end
119- return mapfoldr_impl (f, op, mapreduce_first (f, op, itr[i]), itr, i- 1 )
121+ return mapfoldr_impl (f, op, (init = mapreduce_first (f, op, itr[i]), ), itr, i- 1 )
120122end
121123
122124"""
123- mapfoldr(f, op, itr; init=Base.NoInit() )
125+ mapfoldr(f, op, itr; [ init] )
124126
125127Like [`mapreduce`](@ref), but with guaranteed right associativity, as in [`foldr`](@ref). If
126- provided, `init` will be used exactly once. In general, it will be necessary to provide
127- `init` to work with empty collections.
128+ provided, the keyword argument `init` will be used exactly once. In general, it will be
129+ necessary to provide `init` to work with empty collections.
128130"""
129- mapfoldr (f, op, itr; init = NoInit ()) = mapfoldr_impl (f, op, init , itr, lastindex (itr))
131+ mapfoldr (f, op, itr; kw ... ) = mapfoldr_impl (f, op, kw . data , itr, lastindex (itr))
130132
131133
132134"""
133- foldr(op, itr; init=Base.NoInit() )
135+ foldr(op, itr; [ init] )
134136
135- Like [`reduce`](@ref), but with guaranteed right associativity. If provided, `init` will be
136- used exactly once. In general, it will be necessary to provide `init` to work with empty
137- collections.
137+ Like [`reduce`](@ref), but with guaranteed right associativity. If provided, the keyword
138+ argument `init` will be used exactly once. In general, it will be necessary to provide
139+ `init` to work with empty collections.
138140
139141# Examples
140142```jldoctest
@@ -145,7 +147,7 @@ julia> foldr(=>, 1:4; init=0)
1451471 => (2=>(3=>(4=>0)))
146148```
147149"""
148- foldr (op, itr; init = NoInit ()) = mapfoldr (identity, op, itr; init = init )
150+ foldr (op, itr; kw ... ) = mapfoldr (identity, op, itr; kw ... )
149151
150152# # reduce & mapreduce
151153
@@ -183,7 +185,7 @@ mapreduce_impl(f, op, A::AbstractArray, ifirst::Integer, ilast::Integer) =
183185 mapreduce_impl (f, op, A, ifirst, ilast, pairwise_blocksize (f, op))
184186
185187"""
186- mapreduce(f, op, itr; init=Base.NoInit() )
188+ mapreduce(f, op, itr; [ init] )
187189
188190Apply function `f` to each element in `itr`, and then reduce the result using the binary
189191function `op`. If provided, `init` must be a neutral element for `op` that will be returne
@@ -206,7 +208,7 @@ implementations may reuse the return value of `f` for elements that appear multi
206208`itr`. Use [`mapfoldl`](@ref) or [`mapfoldr`](@ref) instead for
207209guaranteed left or right associativity and invocation of `f` for every value.
208210"""
209- mapreduce (f, op, itr; init = NoInit ()) = mapfoldl (f, op, itr; init = init )
211+ mapreduce (f, op, itr; kw ... ) = mapfoldl (f, op, itr; kw ... )
210212
211213# Note: sum_seq usually uses four or more accumulators after partial
212214# unrolling, so each accumulator gets at most 256 numbers
@@ -330,7 +332,7 @@ mapreduce(f, op, a::Number) = mapreduce_first(f, op, a)
330332_mapreduce (f, op, :: IndexCartesian , A:: AbstractArray ) = mapfoldl (f, op, A)
331333
332334"""
333- reduce(op, itr; init=Base.NoInit() )
335+ reduce(op, itr; [ init] )
334336
335337Reduce the given collection `itr` with the given binary operator `op`. If provided, the
336338initial value `init` must be a neutral element for `op` that will be returned for empty
@@ -362,7 +364,7 @@ julia> reduce(*, [2; 3; 4]; init=-1)
362364-24
363365```
364366"""
365- reduce (op, itr; init = NoInit ()) = mapreduce (identity, op, itr; init = init )
367+ reduce (op, itr; kw ... ) = mapreduce (identity, op, itr; kw ... )
366368
367369reduce (op, a:: Number ) = a # Do we want this?
368370
0 commit comments