Skip to content

Commit

Permalink
Duck type the function in map, reduce, etc.
Browse files Browse the repository at this point in the history
This is motivated by the existence of call-overloading
  • Loading branch information
timholy committed Feb 28, 2015
1 parent 73f19aa commit 4f9bb4a
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 41 deletions.
1 change: 0 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1499,4 +1499,3 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real)
end

randsubseq{T}(A::AbstractArray{T}, p::Real) = randsubseq!(T[], A, p)

6 changes: 3 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1298,9 +1298,9 @@ end
## Filter ##

# given a function returning a boolean and an array, return matching elements
filter(f::Function, As::AbstractArray) = As[map(f, As)::AbstractArray{Bool}]
filter(f, As::AbstractArray) = As[map(f, As)::AbstractArray{Bool}]

function filter!(f::Function, a::Vector)
function filter!(f, a::Vector)
insrt = 1
for curr = 1:length(a)
if f(a[curr])
Expand All @@ -1312,7 +1312,7 @@ function filter!(f::Function, a::Vector)
return a
end

function filter(f::Function, a::Vector)
function filter(f, a::Vector)
r = Array(eltype(a), 0)
for i = 1:length(a)
if f(a[i])
Expand Down
2 changes: 1 addition & 1 deletion base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function length_checked_equal(args...)
n
end

map(f::Callable, a::Array{Any,1}) = Any[ f(a[i]) for i=1:length(a) ]
map(f, a::Array{Any,1}) = Any[ f(a[i]) for i=1:length(a) ]

macro thunk(ex); :(()->$(esc(ex))); end
macro L_str(s); s; end
Expand Down
4 changes: 2 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ map(f::Callable, A::BitArray, B::BitArray) = map(specialized_bitwise_binary(f),
map(f::BitFunctorUnary, A::BitArray) = map!(f, similar(A), A)
map(f::BitFunctorBinary, A::BitArray, B::BitArray) = map!(f, similar(A), A, B)

map!(f::Callable, A::BitArray) = map!(f, A, A)
map!(f, A::BitArray) = map!(f, A, A)
map!(f::Callable, dest::BitArray, A::BitArray) = map!(specialized_bitwise_unary(f), dest, A)
map!(f::Callable, dest::BitArray, A::BitArray, B::BitArray) = map!(specialized_bitwise_binary(f), dest, A, B)

Expand Down Expand Up @@ -1607,7 +1607,7 @@ end

## Filter ##

function filter(f::Function, Bs::BitArray)
function filter(f, Bs::BitArray)
boolmap::Array{Bool} = map(f, Bs)
Bs[boolmap]
end
Expand Down
Loading

0 comments on commit 4f9bb4a

Please sign in to comment.