Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support init keyword in sum/prod/maximum/minimum #36188

Merged
merged 26 commits into from
Jun 11, 2020
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ed1cfc9
Support `init` keyword in `maximum`/`minimum`
timholy May 11, 2020
a9fd8b8
Add `init` keyword argument to `sum` and `prod`
tkf Jun 8, 2020
16b13d2
Merge remote-tracking branch 'origin/teh/reduce_init' into sum-init
tkf Jun 8, 2020
72d01c0
Test sum and prod
tkf Jun 8, 2020
c3e66a5
Add compat annotations to `minimum` and `maximum`
tkf Jun 8, 2020
31feb69
Tweak docstring signature style
tkf Jun 8, 2020
32e0c99
Fix reflection doctest
tkf Jun 8, 2020
b8a686b
Add a NEWS item
tkf Jun 8, 2020
749e2ce
Fix `sum(::AbstractArray{Bool}; dims)`
tkf Jun 8, 2020
bf551a7
Fix a typo
tkf Jun 8, 2020
e1173cb
Fix sum(::AbstractArray{Bool}) optimization path
tkf Jun 8, 2020
791f93c
Apply suggestions from code review
tkf Jun 8, 2020
2b84499
Reflect the same changes to other docstrings
tkf Jun 8, 2020
2122e0e
Mention how `nothing` is used in the test
tkf Jun 8, 2020
6918213
Demonstrate `init` for `sum` and `prod`
tkf Jun 8, 2020
ea2cc8b
Reflect the same changes to other docstrings (2)
tkf Jun 8, 2020
32d7f93
Use `function noncallable end`
tkf Jun 8, 2020
3e7b09c
Use init=Inf for minimum docstring
tkf Jun 8, 2020
0791d49
Apply suggestions from code review
tkf Jun 9, 2020
923bbd8
Revert: maximum(length, []; init=-1)
tkf Jun 9, 2020
0050d4f
Use typemax(Int64) for minimum(length, []; init=typemax(Int64))
tkf Jun 9, 2020
6951b2c
Yet another example: minimum(tanh, Real[]; init=1.0)
tkf Jun 9, 2020
04c0cc9
Yet another example: maximum(tanh, Real[]; init=-1.0)
tkf Jun 9, 2020
5d952ec
Apply suggestions from code review
tkf Jun 10, 2020
42a0155
A short note on the output bound of tanh
tkf Jun 10, 2020
254a7cb
Use sin instead of tanh
tkf Jun 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add init keyword argument to sum and prod
  • Loading branch information
tkf committed Jun 8, 2020
commit a9fd8b8a8792a197b7063b6e4e9f37b93c097062
48 changes: 39 additions & 9 deletions base/reduce.jl
Original file line number Diff line number Diff line change
@@ -462,14 +462,21 @@ reduce(op, a::Number) = a # Do we want this?
## sum

"""
sum(f, itr)
sum(f, itr; init)

Sum the results of calling function `f` on each element of `itr`.

The return type is `Int` for signed integers of less than system word size, and
`UInt` for unsigned integers of less than system word size. For all other
arguments, a common return type is found to which all arguments are promoted.

The value returned for empty `itr` can be specified by `init` which must be the
additive identity. It is unspecified whether `init` is used for non-empty
collections.
tkf marked this conversation as resolved.
Show resolved Hide resolved

!!! compat "Julia 1.6"
Keyword argument `init` requires Julia 1.6 or later.

# Examples
```jldoctest
julia> sum(abs2, [2; 3; 4])
@@ -491,60 +498,83 @@ In the former case, the integers are widened to system word size and therefore
the result is 128. In the latter case, no such widening happens and integer
overflow results in -128.
"""
sum(f, a) = mapreduce(f, add_sum, a)
sum(f, a; kw...) = mapreduce(f, add_sum, a; kw...)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved

"""
sum(itr)
sum(itr; init)

Returns the sum of all elements in a collection.

The return type is `Int` for signed integers of less than system word size, and
`UInt` for unsigned integers of less than system word size. For all other
arguments, a common return type is found to which all arguments are promoted.

The value returned for empty `itr` can be specified by `init` which must be the
additive identity. It is unspecified whether `init` is used for non-empty
collections.

!!! compat "Julia 1.6"
Keyword argument `init` requires Julia 1.6 or later.

# Examples
```jldoctest
julia> sum(1:20)
210
tkf marked this conversation as resolved.
Show resolved Hide resolved
```
"""
sum(a) = sum(identity, a)
sum(a::AbstractArray{Bool}) = count(a)
sum(a; kw...) = sum(identity, a; kw...)
sum(a::AbstractArray{Bool}; kw...) = count(a)
# Note: It is OK to ignore `init` to `sum(::AbstractArray{Bool})`
# because it is unspecified if the value of `init` is used or not.

## prod
"""
prod(f, itr)
prod(f, itr; init)

Returns the product of `f` applied to each element of `itr`.

The return type is `Int` for signed integers of less than system word size, and
`UInt` for unsigned integers of less than system word size. For all other
arguments, a common return type is found to which all arguments are promoted.

The value returned for empty `itr` can be specified by `init` which must be the
multiplicative identity. It is unspecified whether `init` is used for non-empty
collections.

!!! compat "Julia 1.6"
Keyword argument `init` requires Julia 1.6 or later.

# Examples
```jldoctest
julia> prod(abs2, [2; 3; 4])
576
tkf marked this conversation as resolved.
Show resolved Hide resolved
```
"""
prod(f, a) = mapreduce(f, mul_prod, a)
prod(f, a; kw...) = mapreduce(f, mul_prod, a; kw...)

"""
prod(itr)
prod(itr; init)

Returns the product of all elements of a collection.

The return type is `Int` for signed integers of less than system word size, and
`UInt` for unsigned integers of less than system word size. For all other
arguments, a common return type is found to which all arguments are promoted.

The value returned for empty `itr` can be specified by `init` which must be the
multiplicative identity. It is unspecified whether `init` is used for non-empty
collections.

!!! compat "Julia 1.6"
Keyword argument `init` requires Julia 1.6 or later.

# Examples
```jldoctest
julia> prod(1:20)
2432902008176640000
```
"""
prod(a) = mapreduce(identity, mul_prod, a)
prod(a; kw...) = mapreduce(identity, mul_prod, a; kw...)

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