Skip to content

Commit ad38b08

Browse files
backport findprev, findlast & tests
See e3aa57e
1 parent 204652a commit ad38b08

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: src/Compat.jl

+25
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,29 @@ else
310310
import Base: Libc, Libdl
311311
end
312312

313+
if VERSION < v"0.4.0-dev+2418"
314+
function findprev(A, start)
315+
for i = start:-1:1
316+
A[i] != 0 && return i
317+
end
318+
0
319+
end
320+
findlast(A) = findprev(A, length(A))
321+
function findprev(A, v, start)
322+
for i = start:-1:1
323+
A[i] == v && return i
324+
end
325+
0
326+
end
327+
findlast(A, v) = findprev(A, v, length(A))
328+
function findprev(testf::Function, A, start)
329+
for i = start:-1:1
330+
testf(A[i]) && return i
331+
end
332+
0
333+
end
334+
findlast(testf::Function, A) = findprev(testf, A, length(A))
335+
export findprev, findlast
336+
end
337+
313338
end # module

Diff for: test/runtests.jl

+17
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,20 @@ let A, B, s
238238
end
239239
@test s == 210
240240
end
241+
242+
# findlast, findprev
243+
let a = [0,1,2,3,0,1,2,3]
244+
@test findlast(a) == 8
245+
@test findlast(a.==0) == 5
246+
@test findlast(a.==5) == 0
247+
@test findlast([1,2,4,1,2,3,4], 3) == 6
248+
@test findlast(isodd, [2,4,6,3,9,2,0]) == 5
249+
@test findlast(isodd, [2,4,6,2,0]) == 0
250+
@test findprev(a,4) == 4
251+
@test findprev(a,5) == 4
252+
@test findprev(a,1) == 0
253+
@test findprev(a,1,4) == 2
254+
@test findprev(a,1,8) == 6
255+
@test findprev(isodd, [2,4,5,3,9,2,0], 7) == 5
256+
@test findprev(isodd, [2,4,5,3,9,2,0], 2) == 0
257+
end

0 commit comments

Comments
 (0)