# Would the Scala convention for anonymous function arguments be feasible?

**URL:** <https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659>\
**Category:** Internals & Design\
**Created:** [November 30, 2016, 8:32pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659 "2016-11-30T20:32:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [November 30, 2016, 8:32pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/1 "2016-11-30T20:32:02Z")

</div>

In [Scala](https://www.scala-lang.org/) there is a convention that the function argument to functional programming operations like `map` can be an expression that uses an underscore. The convention is that the anonymous function in a Julia call

```nohighlight
sum(x -> x < 1.0e-5, rand(10000))

```

could be written as

```nohighlight
sum(_ < 1.0e-5, rand(10000))

```

without the need to write `_ -> ` at the beginning of the expression.

I’m just throwing this idea out there. I write a lot of anonymous functions in such functional programming constructs and it would be convenient to use expressions like this instead. However, I have no idea how difficult it would be to implement this or how offensive such a convention might be to others.

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [November 30, 2016, 9:55pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/2 "2016-11-30T21:55:47Z")

</div>

See [Julia issue #5571](https://github.com/JuliaLang/julia/issues/5571) for some old discussion on this idea.

I would like to see it happen.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 5, 2016, 11:31pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/3 "2016-12-05T23:31:51Z")

</div>

I’m still sympathetic to this idea (don’t think @jeff.bezanson cares for it) – the main hang up is to determine how far the expression that is the body of the anonymous function extends. There was some discussion in that thread, but no one really had a plausible proposal. IIRC, Scala uses types to determine this, which is not possible for Julia and arguably not a great idea in the first place. Other languages with similar features have some kind of syntactic indication of where the function expression stops. My best attempt at proposing an answer to that question starts [here](https://github.com/JuliaLang/julia/issues/5571#issuecomment-157424665).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 10, 2016, 6:27pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/4 "2016-12-10T18:27:26Z")

</div>

I had a conversation yesterday where @johnmyleswhite brought up how [Hack’s pipeline `|>` operator](https://docs.hhvm.com/hack/operators/pipe-operator) works. Once we disallow `_` as a normal r-value so that it can be treated specially as an l-value (i.e. it discards its value like `~` as an l-value in Matlab), then we could use it like Hack’s `$$` with the `|>` operator, i.e.:

```julia
f() |> g(_) |> h(5, _) # h(5, g(f()))
f() |> g1(_) + g2(_) |> h(5, _) # x = f(); h(5, g1(x) + g2(x))

```

Using `_` like this doesn’t have a problem with knowing how far the expression extends since the expressions are explicitly delimited by `|>` operators. When there are no instances of `_` in an expression on the RHS of a `|>` it could continue to mean to apply the value of that expression to the LHS value, which would make this change backwards compatible with the current meaning (except for code that has overloaded `|>` to mean something else), which makes the first example even shorter:

```julia
f() |> g |> h(5, _) # h(5, g(f()))

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [December 10, 2016, 6:30pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/5 "2016-12-10T18:30:15Z")

</div>

Am I right to think that implementing a pipeline operator using `_` would preclude using it in anonymous functions?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 10, 2016, 6:32pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/6 "2016-12-10T18:32:06Z")

</div>

Not entirely, since one behavior applies when the `|>` operator appears and the other where it doesn’t, but having two such special meanings would be confusing. In any case, once `_` can’t be used as an r-value, any of these are up for grabs, so we can take our time deciding what meaning (if any) we’d like to have. The advantage of the Hack-like proposal is that it’s already pretty well fleshed out, whereas no one has a really compelling idea for how the Scala-like thing would work.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [December 10, 2016, 8:23pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/7 "2016-12-10T20:23:30Z")

</div>

Note that this is exactly how chaining works in ChainMap.jl. I’ve disallowed in-line versions for now; there’s just block syntax which I think is easier to read

```julia
@chain begin
    f()
    g
    h(5, _)
end

```

Also, `@chain` attempts to be smart; you can apply it to whole blocks of global code and it searches through for `begin` blocks where chaining would be feasible and chains only them.

I think if I was going to make it into Base, I’d suggest syntax like this:

```julia
chain
    f()
    g
    h(5, _)
end

```

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [December 10, 2016, 11:13pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/8 "2016-12-10T23:13:17Z")

</div>

Also, to implement dmbates’ proposal in ChainMap syntax:

```julia
@chain begin
    10000
    rand
    @over ~_ < 1e-5
    sum
end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 11, 2016, 12:13am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/9 "2016-12-11T00:13:59Z")

</div>

I really like the block syntax of `@chain`, though I see that people who use this often probably would like the inline syntax. But for the inline case, I find that using an operator like `|>` makes it clear that chaining is going on, whereas the Scala-like syntax I find to be a little opaque. But that’s just my perspective as someone who doesn’t chain functions / do functional programming often (if at all).

Also, something to keep in mind, if I just saw random `_`s around, I would not know where to look in the documentation for that. But `|>` and `@chain` are easy to Google search. Making something not only easy to read but also easy to find documentation is something I think is crucial if it’s a niche part of the syntax.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [December 11, 2016, 10:26am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/10 "2016-12-11T10:26:59Z")

</div>

I am continuing to maintain a [summary post](https://github.com/JuliaLang/julia/issues/5571#issuecomment-205754539) in Function chaining issue:  
Quoted Below:

> So our current list of various efforts etc  
> I think it is worth people checking these out, (ideally before opinioning, but w/e)  
> they are all slightly different.  
> (I am attempting to order chronologically).

> ## Packages
> 
> - MikeInnes’s [Lazy.jl](https://github.com/MikeInnes/Lazy.jl).
> - rened’s [FunctionalData.jl](https://github.com/rened/FunctionalData.jl)
> - oxinabox’s [Pipe.jl](https://github.com/oxinabox/Pipe.jl)
> - bramtayl’s [ChainMap.jl](https://github.com/bramtayl/ChainMap.jl)

> ## Nonpackage Prototypes
> 
> - kmsquires’s [composition marco](https://github.com/JuliaLang/julia/issues/5571#issuecomment-33428798)
> - porterjamesj’s [threading macro](https://gist.github.com/porterjamesj/7952882)
> - Oxinabox’s [Terse Lambdas](http://nbviewer.ipython.org/gist/oxinabox/50a1e17cfb232a7d1908) – from @StefanKarpinski’s [https://github.com/JuliaLang/julia/issues/5571#issuecomment-157424665](https://github.com/JuliaLang/julia/issues/5571#issuecomment-157424665)
> - fcard’s [GeneratedPartials](https://gist.github.com/fcard/b48513108a32c13a49a387a3c530f7de) – [example](https://github.com/JuliaLang/julia/issues/5571#issuecomment-227071959)

> ## Related:
> 
> - andyferris’s [function compsition operator, PR](https://github.com/JuliaLang/julia/pull/17184)
> - MikeInnes’s [Flow.jl](https://github.com/MikeInnes/Flow.jl)

> * * *

> Perhaps this should be editted in to one of the top posts.

> updated: 1/10/2016

* * *

Personally I really like the way @StefanKarpinski was going with [https://github.com/JuliaLang/julia/issues/5571#issuecomment-157424665](https://github.com/JuliaLang/julia/issues/5571#issuecomment-157424665)

Which is way I implemented a macro-based prototype with it.

> **[Jupyter Notebook Viewer](https://nbviewer.org/gist/oxinabox/50a1e17cfb232a7d1908)**
>
> Check out this Jupyter notebook!

Which I hoped would let people expriment with it and get a feel for it, and see if it was a nice syntax.  
However no further discussion emerged from that prototype.

---

<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 12, 2016, 7:03am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/11 "2016-12-12T07:03:15Z")

</div>

This will probably be unpopular, but to me it would seem clearer if the `_` became mandatory in chained expressions (rather than backwards compatible)., e.g. the middle `(_)` becomes mandatory in `f() |> g(_) |> h(5, _)`.

Yes, less backward compatible and less concise, but more importantly (to me), less _magical_. Otherwise the expression jumps back and forwards between different conventions, and it’s a bit jarring.

Another idea floated was to have `_` defined like `ans` is in the REPL right now - it is a binding to the output of the previous expression. So you could just type

```julia
f(); g(_); h(5,_)

```

without the `|>` (or write it over multiple lines, if you like). Then change REPL `ans` to `_`, and there’s less for users to learn, and less magic all around.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [December 12, 2016, 3:30pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/12 "2016-12-12T15:30:47Z")

</div>

How does the `ans` part interact with nesting? What is the behavior of the following code?

```julia
f(); g(h1(_), h2(_));

```

To me the problem with `ans` is that it involves mutation of global state, so the order of execution of complex sub-expressions becomes a defining property of every program.

---

<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 12, 2016, 9:21pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/13 "2016-12-12T21:21:41Z")

</div>

Sorry, I’m a bit confused what you are getting at. Don’t you have the _exact_ same problem with function chaining (and Julia in general) currently?

E.g… what does this do?

```julia
f() |> _ -> g(h1!(_), h2!(_))

```

or even:

```julia
x1 = f()
x2 = g(h1!(x1), h2!(x1)) # I've never seen anyone write such a thing

```

I definitely see this syntax as being clearer in a functional paradigm, where `_` or `ans` isn’t being mutated, and it is perfectly clear. But you would have this same problem using `ans` at the REPL. Julia does pretty well at encouraging functional approaches, and (just speculating here) having more convenient syntax like this might only make that easier and more common.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [December 12, 2016, 10:13pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/14 "2016-12-12T22:13:29Z")

</div>

I think there’s an important distinction between `|>` setting `_` and `ans` becoming `_`: in function chaining, we agree that `_` is invariantly bound to the value of the left-hand side of that operator, whereas the `ans` solution requires you to clarify what the “last top-level (sub-)expression” means.

It’s a problem of ambiguity about what binds to what exacerbated by actual global state in the REPL. Of course you could remove that ambiguity, but it’s hard to imagine that any rule you invent could be more clear than how `|>` works.

---

<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 12, 2016, 11:40pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/15 "2016-12-12T23:40:24Z")

</div>

> requires you to clarify what the “last top-level (sub-)expression” means.

I think it might be simpler than you are worrying about.

Because `return` is optional, Julia users have to learn these rules anyway. It already _almost_ feels like a part of the language that the last expression has some meaning because of the way functions terminate, so in many ways this would just be formalizing this somewhat. I think it is pretty easy to understand what the last expression is (I think all you need to know is this: `for` / `while` loops always return `nothing` so you don’t have to parse _inside_ of an immediately proceeding loop; blocks (such as with `begin`) return their last expression just like a function and so can be considered to be “flattened”; and finally there are similar rules for `if` that make it compatible with the ternary. Beyond these, there is no “subexpression” in the sense that `a+b+c` should rebind `_` in the middle of its operation to `a+b` or something - expressions (in this context) end wherever you can insert a semicolon `;` with no effect).

> It’s a problem of ambiguity about what binds to what exacerbated by actual global state in the REPL. Of course you could remove that ambiguity, but it’s hard to imagine that any rule you invent could be more clear than how |\> works.

I agree that `|>` is perfectly clear and logical the way proposed here. I just think automatically binding to `_` uses the same rules that Julia users have to learn anyway to understand functions. I’m suggesting that this wouldn’t cause any additional ambiguities or global statefulness concerns beyond what already exists.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [December 13, 2016, 12:40am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/16 "2016-12-13T00:40:15Z")

</div>

That’s all fair.

On my end, it seems like your point is that every sequence of expressions can be viewed as a function chain in which most of the time the previous expression’s value isn’t being passed forward – but you could always pass it forward as `ans` if `ans` were set to the value of the last executed expression. I agree with that interpretation, but think that the resulting syntax wouldn’t really clarify the flow of data over what we have today.

For example, I’d be pretty unhappy if people wrote code like the following:

```julia
# Do step 1
foo(1)

# [...]
# [...]
# Now, after some long deliberation, we're going to do step 2.
bar(ans)

```

Of course, you can write ugly code using `|>` as well. Ultimately my preference for Hack’s `|>` is based on a subjective sense that the syntax suggests data flow in a way that the use of `ans` wouldn’t.

---

<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 13, 2016, 12:54am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/17 "2016-12-13T00:54:12Z")

</div>

I think we all agree that it has potential to be misused. 🙂

For my use cases, I rarely use `|>`, but I would love to see function chaining become more powerful. I’m sure that both approaches could make sense (separately or together), but I was worried that `|>` with optional `_` means there is a bit more desugaring happening by the parser that users need to understand (for some reason complex desurgaring irritates me, e.g. the very powerful `do` syntax is useful but annoys me the way it inserts things into functions, similar to current behaviour of `|>`, while mandatory `_` would be clearer IMO. A similar trick to explicitly label where `do` inserts the function would be nice, but this might be personal preference).

---

<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 13, 2016, 1:00am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/18 "2016-12-13T01:00:57Z")

</div>

Getting rather off-topic, sorry, but here’s a `do` idea that combines some of the above:

```julia
# existing syntax
map(array) do element
   2*element
end

# replace with <| (need to be defined)
map(_, array) <| function (element)
    2*element
end

# and make `do` be sugar for the above
map(_, array) do element
    2*element
end

```

(note that having `<|` doesn’t combine with `_` as a REPL-like `ans`, since it reverses the direction in a strange way)

The advantage here is it’s clearer what kind of method of `map` is being called, and that `_` doesn’t have to be the first argument.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [December 13, 2016, 6:15am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/19 "2016-12-13T06:15:53Z")

</div>

Here’s a short macro that implements the `_` as `ans` syntax:

```julia
using MacroTools

store_line(e) = 
    isexpr(e, :line) ?
        e
        :( _ = $e )
    
store_block(e) = 
    isexpr(e, :block) ?
        MacroTools.walk(e, store_line, identity) :
        e
        
macro store(e)
    esc( MacroTools.prewalk( store_block, e) )
end

2 == @store begin
    function a(b)
        b
        _ + 1
    end
    1
    a(_)
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 13, 2016, 4:34pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/20 "2016-12-13T16:34:58Z")

</div>

I’m concerned about the impact that having a potential implicit data dependency between an expression and the next would have on optimization and code analysis. But then I suppose that you can tell pretty easily if the value of a top-level expression in a block needs to be captured or not: does the following expression in the block have `_` anywhere in it. This approach would also make multiple streams of computation fairly clear:

```julia
f1(), f2()
g1(_[1]), g2(_[2])
h(_...) # h(_[1], _[2])
k1(_), k2(_)

```

Definitely something to ponder.

[Next page](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659.md?page=2)
