# Generalizing the broadcasting \`f.(x)\`-like syntax

**URL:** https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860
**Category:** Internals & Design
**Tags:** question, proposal
**Created:** [December 19, 2017, 6:43pm UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860 "2017-12-19T18:43:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![flipgthb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flipgthb/32/29080_2.png) [@flipgthb](https://discourse.julialang.org/u/flipgthb)
#### Post date: [December 19, 2017, 6:43pm UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860/1 "2017-12-19T18:43:57Z")

</div>

Hi,

Forgive me if this question is out of place or dumb, I’m not used to get involved in open source develpment and I’m not a programmer.

I like the the syntax sugar for broadcastin a function, i. e. `f.(x)`, and was wondering if the idea couldn’t be generalized to other desired function behaviors.  
For instance, could it be possible to implement a `!` operator to force function to operate inplace whenever possible, or a `?` to work with nullable types?

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [December 19, 2017, 8:24pm UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860/2 "2017-12-19T20:24:15Z")

</div>

I think the issue is that ` f.(x)` is just syntactic sugar for `broadcast(f,x)`, which means you don’t need to know anything about `f` or `x` to do the transformation.

What would `f!(x)` be transformed into ? if the function `f` starts with `out = similar(x)` you can’t go in and change it.

You could do something like `f!(x) -> f(Reference(x))` and write `f` such that it adapts its behavior depending on its input type, but that wouldn’t be very general.

Good news is that you can do that with macros, e.g. `@! f(x)` , so you can experiment with anything you can think about, and if by chance you find a pattern that is super useful it could then be turned into a special syntax.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 19, 2017, 8:34pm UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860/3 "2017-12-19T20:34:54Z")

</div>

For an example of what @jonathanBieler is talking about with macros, see [https://github.com/lindahua/Devectorize.jl](https://github.com/lindahua/Devectorize.jl) which introduced the `@devec` macro to perform many of the same transformations that later went on to become the built-in `f.(x)` syntax in Julia v0.5 and v0.6.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 20, 2017, 4:21am UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860/4 "2017-12-20T04:21:26Z")

</div>

> [@flipgthb](#):
>
> For instance, could it be possible to implement a ! operator to force function to operate inplace whenever possible,

You can do `x .= f.(x)` or `@. x = f(x)` to replace `x[i]` with `f(x[i])` in-place already (assuming the types are compatible).

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [December 20, 2017, 11:21am UTC](https://discourse.julialang.org/t/generalizing-the-broadcasting-f-x-like-syntax/7860/5 "2017-12-20T11:21:32Z")

</div>

In theory you can already turn one of the available unicode operators into a higher-order function. I once played with making `↓` (`\downarrow <tab>`) be a reduction operator, e.g. `reduce(f, x)` could be written `f ↓ x` or something. I think @c42f was suitably horrified at the time, and we agreed APL wasn’t what we were aiming for 🙂

OTOH there’s probably only a small handful of needed operators (`map`/`broadcast`, `reduce`/`reducedim`/`accumulate`, `filter`, `zip`, `product`) to make a relatively complete higher-order programming interface (I think that list is already sufficient for relational algebra, matrix multiplication, etc).
