findp

63 views
Skip to first unread message

John Myles White

unread,
Jan 26, 2013, 9:15:02 AM1/26/13
to julia...@googlegroups.com
Having just recently discovered anyp() and allp() in Base, I wrote a complementary function called findp(). It seems like it would be generally useful, but I'm hesitant to propose adding more things to Base. As such, I'm just leaving it below in case others have a sense for where we might want to put it:

function findp(p::Function, a::AbstractArray)
n = length(a)
indices = Array(Int, n)
t = 0
for i in 1:n
if p(a[i])
t += 1
indices[t] = i
end
end
return indices[1:t]
end

findp(iseven, [1, 2, 3, 4, 5])

Harlan Harris

unread,
Jan 26, 2013, 9:49:00 AM1/26/13
to julia...@googlegroups.com
I think find(f, array) will do the same thing. See https://github.com/JuliaLang/julia/blob/master/base/array.jl, around line 1150 for variations...



--



John Myles White

unread,
Jan 26, 2013, 10:14:42 AM1/26/13
to julia...@googlegroups.com
Ah. I just assumed that wouldn't exist because anyp() and allp() exist.

 -- John

--
 
 

Glen Hertz

unread,
Jan 26, 2013, 10:58:34 AM1/26/13
to julia...@googlegroups.com
Hi,

I would make the same assumption...

I wrote my own `find` because I couldn't find `find`.  I was looking for a function starting with 'ind' or named 'indices' .  Calling a function `find` that returns indices seems too generic to me.  But now I know about it.  

Glen

Harlan Harris

unread,
Jan 26, 2013, 11:02:00 AM1/26/13
to julia...@googlegroups.com
It's a Matlab thing...


--
 
 

Diego Javier Zea

unread,
Jan 26, 2013, 11:09:51 AM1/26/13
to julia...@googlegroups.com
Looking into countp() [ https://groups.google.com/forum/#!topic/julia-dev/R_gMPWNvFYU ] looks like the predicate is not in-lined on the function.
The inline version of the predicate is more than 13x faster than using countp() and a lambda function, and scales better than the function in base.
Maybe can be the same with find() ?

John Myles White

unread,
Jan 26, 2013, 11:15:20 AM1/26/13
to julia...@googlegroups.com
Can I propose that we start producing a Julia Rosetta Stone that can be indexed for searching the language?

Here's a one-row database that translates find into a few languages:

--
 
 

Reply all
Reply to author
Forward
0 new messages