Using macros from Query with pipes


#1

I’m been using the macros from Query with the pipe |>. If I do (with the Query and Statistics packages)

collect(1:10) |> @map(_^2) |> @filter(_%2==0) |> a->mean(a)

everything works as expected (finding the mean of the squares of all even squares up to 100), especially the last, which takes the mean of the resulting array. However, a->mean(a) isn’t a clean as the other terms. Is there a shortcut to the current array? So I can do at the end mean(curr_array) or just mean() ?


#2

a->mean(a) is equivalent to mean, so

using Query, Statistics
collect(1:10) |> @map(_^2) |> @filter(_%2==0) |> mean

#3

Yes. That works. I realized I tried it as mean() at the end instead of as shown.


#4

However, here’s a related question with this.

using DataFrames, Query
df = DataFrame(A=collect(1:10),B=rand(10))
df |> @orderby(_.B) |> DataFrame |> head

gives the top 6 rows, but if you want a different number of rows, then we need

df |> @orderby(_.B) |> DataFrame |> dff->head(dff,4)

and by more general question is there a shortcut to the current object, so we don’t need to write an anonymous function.


#5

There have been proposals for a more compact syntax, eg

but I think it is best to do it like you did. It is clear, readable, and handles nesting unambiguously (for examples on why that matters, see the discussion I linked).


#6

Thanks for the link. And after the interesting discussion. I agree, the dff->head(dff, 4) is more readable.


#7

Just for this case, you can also use the @take query operator from Query.jl:

df |> @orderby(_.B) |> @take(4)