# Explanation of the @. syntax

**URL:** <https://discourse.julialang.org/t/explanation-of-the-syntax/70826>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 2, 2021, 7:25pm UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826 "2021-11-02T19:25:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsevo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsevo/32/30450_2.png) [@jsevo](https://discourse.julialang.org/u/jsevo)\
**Post date:** [November 2, 2021, 7:25pm UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/1 "2021-11-02T19:25:35Z")

</div>

I came across this piece of syntax that was not immediately obvious to me.

```julia

sum(@. (data > 0.5) & (data < 0.75)) / length(samples)

```

I worked out that this is “mapping” the dot operator over all the other operators. I.e. the same result is obtained by:

```julia

sum( (data .> 0.5) .& (data .< 0.75)) / length(samples)

```

I know this is related to meta programming, which I am not good at. Could someone explain the mechanism of this?

Thx

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 2, 2021, 7:30pm UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/2 "2021-11-02T19:30:40Z")

</div>

Basically `@.` goes through the expression and inserts dots on all the functions in the expression.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 2, 2021, 8:02pm UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/3 "2021-11-02T20:02:23Z")

</div>

It’s not really metaprogramming, other than that the person who _implemented_ `@.` was metaprogramming. Crash course:

A macro (`@something`) takes an expression as input, returns another expression and evaluates it.

So when you write `@. x^2 - 2 * y + f(x)`, you give `@.` the argument `:(x^2 - 2 * y + f(x))`. The internal logic of `@.` looks at each operator and dots it, and thus returns `:(x.^2 .- 2 .* y .+ f.(x))`, which is now _evaluated_, so `@. x^2 - 2 * y + f(x)` is equivalent to `x.^2 .- 2 .* y .+ f.(x)`.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [November 2, 2021, 9:33pm UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/4 "2021-11-02T21:33:49Z")

</div>

You can use `@macroexpand` to see the code generated by a macro.

```julia
julia> data = rand(4);

julia> @macroexpand sum(@. (data > 0.5) & (data < 0.75)) / length(data)
:(sum((&).((>).(data, 0.5), (<).(data, 0.75))) / length(data))

```

---

<div class="post-metadata">

**Author:** ![jsevo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsevo/32/30450_2.png) [@jsevo](https://discourse.julialang.org/u/jsevo)\
**Post date:** [November 4, 2021, 3:09am UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/5 "2021-11-04T03:09:31Z")

</div>

Thank you! Oscar for confirming my interpretation, John for the useful advice re: @macroexpand. Your replies were very helpful. I will mark Gustaphe’s reply as solution - I hadn’t realized! Appreciate it.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [November 4, 2021, 3:55am UTC](https://discourse.julialang.org/t/explanation-of-the-syntax/70826/6 "2021-11-04T03:55:26Z")

</div>

And just in case you (or anyone else) hasn’t seen it, this [blog post](https://julialang.org/blog/2017/01/moredots/) gives a really excellent explanation of _why_ you’d want to dot every operator in an expression.
