# \circ and parantheses

The main thing that causes me to not use the `\circ` operator in julia as much as i do the mathematica equivalent (they use `@`) is, that I need to wrap everything in parantheses.
I can see that this relates nicely to mathematical notation but what is the most common usecase for me, is that i wrote a function call like `sqrt(-2)` and then i notice “ahh… I need an `abs` there”.
If i were to just insert a `\circ abs` between `sqrt` and `(`, that would go easily, but putting additional parantheses before `sqrt` and after `abs` killing the gain.
Long story short: couldn’t `sqrt \circ abs(-2)` be parsed or lowered as `(sqrt \circ abs)(-2)`? That would also make it more similar to its brother `|>`.

1 Like

It could, but that would be a breaking change so it couldn’t be made until Julia 2.0.

1 Like

I’m really surprised that it doesn’t do that.

Whats the current behaviour useful for?

`abs(-2)` could return a function?

I don’t see `\circ` as that useful for cases like these, where `sqrt(abs(-2))` is at least as good. It’s nice for creating anonymous functions, though. `sqrt ∘ abs` is much better than `x->sqrt(abs(x))`.

5 Likes

Ah, to pass to map and such. Yeah I see.

Why would that be breaking? The current syntax would be still valid just not mandatory.

Because it changes the meaning of existing code.

1 Like

@DNF already suggested this, but let me elaborate: the current behavior is useful for working with higher order functions. The docstring for `∘` mentions

``````julia> map(uppercase∘first, ["apple", "banana", "carrot"])
3-element Array{Char,1}:
'A'
'B'
'C'
``````

and one can find other examples in the standard library, eg in `Pkg`

``````        for (name, uuids) in sort!(collect(unresolved), by=lowercase ∘ first)
``````

It is emphatically not for `(f∘g)(x)`, for that `f(g(x))` is much more readable. The same applies to `!` for functions, eg `!f` is fine but `!f(x)` is preferred to `(!f)(x)`.

@BeastyBlacksmith: whenever you are wondering about a design choice or a use case for something in Julia, it is good practice to look at usage in `Base` and the standard library. Not everything is perfect, but many things are the result of a conscious decision and form a coherent whole.

1 Like

You can easily define your own operator which does this:

``````julia> (<|)(f, x) = f(x)
<| (generic function with 1 method)

julia> sqrt <| abs <| 2
1.4142135623730951
``````
2 Likes