Skip to content

Commit 9cd15bc

Browse files
authored
Style improvements in reduce.jl (#136)
* Style improvements in reduce.jl * Update reduce.jl
1 parent c51154b commit 9cd15bc

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/reduce.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
function _same_length(a) end
2-
function _same_length(a, b, c::Vararg{Any,N}) where {N}
1+
_check_same_length(::Any) = nothing
2+
3+
function _check_same_length(a, b, c::Vararg{Any,N}) where {N}
34
if length(a) != length(b)
45
throw(
56
DimensionMismatch(
6-
"one array has length $(length(a)) which does not match the length of the next one, $(length(b)).",
7+
"one array has length $(length(a)) which does not match the " *
8+
"length of the next one, $(length(b)).",
79
),
810
)
911
end
10-
return _same_length(b, c...)
12+
return _check_same_length(b, c...)
1113
end
14+
1215
reduce_op(op::AddSubMul) = add_sub_op(op)
13-
reduce_op(op::typeof(add_dot)) = +
16+
17+
reduce_op(::typeof(add_dot)) = +
18+
1419
neutral_element(::typeof(+), T::Type) = zero(T)
20+
1521
map_op(::AddSubMul) = *
22+
1623
map_op(::typeof(add_dot)) = LinearAlgebra.dot
24+
1725
function promote_map_reduce(op::Function, args::Vararg{Any,N}) where {N}
18-
return T =
19-
promote_operation(op, promote_operation(map_op(op), args...), args...)
26+
return promote_operation(
27+
op,
28+
promote_operation(map_op(op), args...),
29+
args...,
30+
)
2031
end
2132

2233
function fused_map_reduce(op::F, args::Vararg{Any,N}) where {F<:Function,N}
23-
_same_length(args...)
34+
_check_same_length(args...)
2435
T = promote_map_reduce(op, eltype.(args)...)
2536
accumulator = neutral_element(reduce_op(op), T)
2637
buffer = buffer_for(op, T, eltype.(args)...)
@@ -30,11 +41,12 @@ function fused_map_reduce(op::F, args::Vararg{Any,N}) where {F<:Function,N}
3041
end
3142
return accumulator
3243
end
44+
3345
function operate(::typeof(sum), a::AbstractArray)
3446
return mapreduce(
3547
identity,
3648
add!!,
37-
a,
49+
a;
3850
init = zero(promote_operation(+, eltype(a), eltype(a))),
3951
)
4052
end

0 commit comments

Comments
 (0)