# Using macros from Query with pipes

**URL:** <https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137>\
**Category:** General Usage\
**Created:** [November 4, 2018, 2:55pm UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137 "2018-11-04T14:55:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Staab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter_staab/32/5874_2.png) [@Peter\_Staab](https://discourse.julialang.org/u/Peter_Staab)\
**Post date:** [November 4, 2018, 2:55pm UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/1 "2018-11-04T14:55:55Z")

</div>

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

```julia
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()` ?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 4, 2018, 6:02pm UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/2 "2018-11-04T18:02:56Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Peter\_Staab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter_staab/32/5874_2.png) [@Peter\_Staab](https://discourse.julialang.org/u/Peter_Staab)\
**Post date:** [November 5, 2018, 3:14am UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/3 "2018-11-05T03:14:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Staab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter_staab/32/5874_2.png) [@Peter\_Staab](https://discourse.julialang.org/u/Peter_Staab)\
**Post date:** [November 5, 2018, 3:14am UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/4 "2018-11-05T03:14:22Z")

</div>

However, here’s a related question with this.

```julia
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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 5, 2018, 5:57am UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/5 "2018-11-05T05:57:43Z")

</div>

There have been proposals for a more compact syntax, eg

[https://github.com/JuliaLang/julia/pull/24990](https://github.com/JuliaLang/julia/pull/24990)

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).

---

<div class="post-metadata">

**Author:** ![Peter\_Staab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter_staab/32/5874_2.png) [@Peter\_Staab](https://discourse.julialang.org/u/Peter_Staab)\
**Post date:** [November 5, 2018, 9:14pm UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/6 "2018-11-05T21:14:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 12, 2018, 12:32am UTC](https://discourse.julialang.org/t/using-macros-from-query-with-pipes/17137/7 "2018-11-12T00:32:36Z")

</div>

> [@Peter\_Staab](#):
>
> df |\> @orderby(\_.B) |\> DataFrame |\> dff-\>head(dff,4)

Just for this case, you can also use the `@take` query operator from [Query.jl](https://github.com/queryverse/Query.jl):

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

```
