# Dot syntax for anonymous functions

**URL:** <https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581>\
**Category:** General Usage\
**Created:** [November 1, 2019, 12:30pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581 "2019-11-01T12:30:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [November 1, 2019, 12:30pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/1 "2019-11-01T12:30:07Z")

</div>

Hi,  
in mathematics we use this syntax of • to represent (anonymous) functions. For instance ||•|| is then equivalent to writing x |–\> ||x||.

My question is, is there such syntax in julia?

If not, I’d love to see the, for instance, bullet operator handle this shorthand in a future release. Do you agree?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 1, 2019, 12:31pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/2 "2019-11-01T12:31:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [November 1, 2019, 12:35pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/3 "2019-11-01T12:35:08Z")

</div>

Oh okay, thanks.

---

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [November 1, 2019, 2:24pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/4 "2019-11-01T14:24:55Z")

</div>

They seem to have problems regarding how the “\_” syntax should be used, more important, their representation does not seem well defined anymore, as for instance

```julia
A,B,C = rand(5), rand(5), rand(5)
maximum(_[1], [A,B,C])

```

could mean `maximum(x -> x[1], [A,B,C])`, but also `x -> maximum(x[1], [A,B,C])`, hence where `x` is itself is an array of functions.

Therefore I just wanted to implement my **own macro** as follows:

- the replacement would be achieved by writing `@x(:x + 3)`, meaning `x -> x + 3`
- similarly `@x(f(:x, :y, 7, :x))` means `(x,y) -> f(x, y, 7, x)`

We then can automatically create problem cases like the ones above and by using composition `∘` or `|>` we can create arbitrary complex functions, like `@x(f(:x, :y, 7)) |> @x(:x^2) |> @x(print("The solution is $(:x)."))`.

**How do I write such a macro and do you have a neater syntax idea?**

---

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [November 1, 2019, 2:51pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/5 "2019-11-01T14:51:29Z")

</div>

By writing

```julia
macro x(f)
    f.args = replace(f.args, :(:x) => :x)
    return :(x -> $f)
end

```

I got a little of the functionality. One can for instance do this

```julia
julia> rdim = @x(rand(5, :x)) # 5 × x random arrays

julia> rdim(1)
5×1 Array{Float64,2}:
 0.9939828371497994
 0.773338508907532
 0.8633776655992123
 0.22892379107363436
 0.39529414038536226

julia> rdim(3)
5×3 Array{Float64,2}:
 0.146573 0.108516 0.327287
 0.105098 0.111566 0.942984
 0.713338 0.69219 0.877718
 0.243916 0.561024 0.569137
 0.787741 0.970282 0.0878656

```

---

<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 1, 2019, 3:06pm UTC](https://discourse.julialang.org/t/dot-syntax-for-anonymous-functions/30581/6 "2019-11-01T15:06:18Z")

</div>

> [@ludwig-austermann](#):
>
> How do I write such a macro

You can find examples on this forum (search for partial application macro or something similar), and even some packages like

> **[GitHub - JobJob/Partial.jl: Yet Another Partial Function Application Macro](https://github.com/JobJob/Partial.jl)**
>
> Yet Another Partial Function Application Macro. Contribute to JobJob/Partial.jl development by creating an account on GitHub.

(I think there are others, but I can’t recall the names).
