Filtering Data Made Easy: Tips and Tricks in Julia

I have published a post here : Filtering Data Made Easy: Tips and Tricks in Julia
Can you suggest additional/efficient methods for filtering lists/tuples?

A useful function for filtering data is… filter:

julia> using BenchmarkTools

julia> test = rand(1_000_000);

julia> f(x) = [i for i ∈ x if i < 0.1]
f (generic function with 1 method)

julia> g(x) = filter(<(0.1), x)
g (generic function with 1 method)

julia> @btime f($test);
  2.679 ms (12 allocations: 1.83 MiB)

julia> @btime g($test);
  1.273 ms (3 allocations: 7.63 MiB)
5 Likes

What does $ sign in $test stands for. Why g(test) is not being used.

How to use multiple conditions on filter :
b = filter(<(0) && >(-1),data_list) ERROR: TypeError: non-boolean (Base.Fix2{typeof(<), Int64}) used in boolean context Stacktrace: [1] top-level scope @ REPL[4]:1

Use an anonymous function.

1 Like

https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions

1 Like
b = filter(x-> -1<x<0, data_list)
1 Like

Updated