diff --git a/base/array.jl b/base/array.jl index 7535b4b8d4587..ace1bb3205923 100644 --- a/base/array.jl +++ b/base/array.jl @@ -777,7 +777,7 @@ function findprev(testf::Function, A, start::Integer) end findlast(testf::Function, A) = findprev(testf, A, length(A)) -function find(testf::Function, A::AbstractArray) +function find(testf::Function, A) # use a dynamic-length array to store the indexes, then copy to a non-padded # array for the return tmpI = Array(Int, 0) @@ -791,9 +791,9 @@ function find(testf::Function, A::AbstractArray) I end -function find(A::AbstractArray) +function find(A) nnzA = countnz(A) - I = similar(A, Int, nnzA) + I = Vector{Int}(nnzA) count = 1 for (i,a) in enumerate(A) if a != 0 diff --git a/test/arrayops.jl b/test/arrayops.jl index f548085c15bb9..1b261afcddc5e 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -362,6 +362,14 @@ a = [0,1,2,3,0,1,2,3] @test findprev(isodd, [2,4,5,3,9,2,0], 7) == 5 @test findprev(isodd, [2,4,5,3,9,2,0], 2) == 0 +# find with general iterables +s = "julia" +@test find(s) == [1,2,3,4,5] +@test find(c -> c == 'l', s) == [3] +g = graphemes("日本語") +@test find(g) == [1,2,3] +@test find(isascii, g) == Int[] + ## findn ## b = findn(ones(2,2,2,2))