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

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?

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.

4 Likes

For an example of what @jonathanBieler is talking about with macros, see 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.

3 Likes

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

2 Likes

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 :slight_smile:

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

4 Likes