# Implied substitution?

**URL:** <https://discourse.julialang.org/t/implied-substitution/17200>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [November 6, 2018, 4:04am UTC](https://discourse.julialang.org/t/implied-substitution/17200 "2018-11-06T04:04:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 6, 2018, 4:04am UTC](https://discourse.julialang.org/t/implied-substitution/17200/1 "2018-11-06T04:04:03Z")

</div>

Make a reserved identifier for incoming values,  
Say that the reserved value were `_`,

```julia
map(x -> any(ismissing, x), itr)

```

would become

```julia
map(any(ismissing, _), itr)

```

or for piping

```julia
data = data |> (df -> categorical!(df, 1:3))

```

```julia
data = data |> categorical!(_, 1:3)

```

This is similar to tidyverse’s `.`. Any package that has an implementation?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 6, 2018, 4:50am UTC](https://discourse.julialang.org/t/implied-substitution/17200/2 "2018-11-06T04:50:45Z")

</div>

I think you’re looking for [this](https://github.com/JuliaLang/julia/pull/24990). 😉

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 6, 2018, 7:43am UTC](https://discourse.julialang.org/t/implied-substitution/17200/3 "2018-11-06T07:43:31Z")

</div>

I knew I had seen it somewhere. I think there was a package that attempted something like that as well.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [November 6, 2018, 10:07am UTC](https://discourse.julialang.org/t/implied-substitution/17200/4 "2018-11-06T10:07:53Z")

</div>

at least for piping there is [GitHub - FNj/Hose.jl: Hose.jl - when pipe is just not flexible enough](https://github.com/FNj/Hose.jl) and [https://github.com/oxinabox/Pipe.jl](https://github.com/oxinabox/Pipe.jl)

---

<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 6, 2018, 10:17am UTC](https://discourse.julialang.org/t/implied-substitution/17200/5 "2018-11-06T10:17:04Z")

</div>

There is

> **[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.

but I guess it needs updating. With MacroTools.jl, this is rather easy to write from scratch, too.

I am skeptical about the utility though, compare

```julia
@p f(x, ...)
# and
x -> f(x, ...)

```

which is just 2 more keystrokes, nests well, and is free of corner cases. But I am skeptical about the whole tidyverse approach, it is extremely convenient for small examples but does not compose well.

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [November 6, 2018, 1:47pm UTC](https://discourse.julialang.org/t/implied-substitution/17200/6 "2018-11-06T13:47:28Z")

</div>

> [@Tamas\_Papp](#):
>
> I am skeptical about the utility though, compare
> 
> ```julia
> @p f(x, ...)
> # and
> x -> f(x, ...)
> 
> ```
> 
> which is just 2 more keystrokes, nests well, and is free of corner cases.

Well, 5 more keystrokes if it became a proper language feature (as in the PR), which would be a third of the whole expression. I would actually really like to see this feature as it adds, I think, a lot of convenience when sticking to a more functional programming style.

---

<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:** [November 6, 2018, 1:54pm UTC](https://discourse.julialang.org/t/implied-substitution/17200/7 "2018-11-06T13:54:05Z")

</div>

This is only true in usage examples with a few short argument names—which is what people in the “numerical computing” side of the Julia community tend to encounter. On the “data analysis” side, which we have traditional not served as well, it’s very common to have more and longer names. For example:

```julia
(long_descriptive_name_1, long_descriptive_name_2, long_descriptive_name_3) -> f(long_descriptive_name_1, long_descriptive_name_2, long_descriptive_name_3)

```

verus

```julia
@p f(long_descriptive_name_1, long_descriptive_name_2, long_descriptive_name_3)

```

The point being that every time someone claims that this “only saves a few characters” they’re apparently willfully ignoring the fact that the savings is proportional to the number and length of the argument names, which is not fixed and will be much larger in some kinds of use cases than others.

---

<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 6, 2018, 1:57pm UTC](https://discourse.julialang.org/t/implied-substitution/17200/8 "2018-11-06T13:57:53Z")

</div>

> [@StefanKarpinski](#):
>
> willfully ignoring the fact that the savings is proportional to the number and length of the argument names

I may be missing something, but in a closure, the argument names are up to the user, aren’t they? Longer names can of course be more descriptive, but if that is desired, `_` and `_1` and the various other proposals for partial application syntax are probably not wanted anyway.

(Also, I am not _willfully_ ignoring anything, just disagree about a technical point.)

---

<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:** [November 6, 2018, 3:32pm UTC](https://discourse.julialang.org/t/implied-substitution/17200/9 "2018-11-06T15:32:39Z")

</div>

Even with one-letter names, the extra typing still scales with the number of arguments and there are many situations where one doesn’t particularly want to use single-letter names for clarity. Which is what makes the “it’s only two more keystrokes” response disingenuous—it’s only two extra keystrokes in the minimal case. It’s easily dozens of keystrokes in completely reasonable and realistic situations. There’s also the related issue of passing keyword arguments with the same name as the variable that’s passed, e.g. `f(x=x)` which is also dismissed as being “only two more keystrokes” but of course, that’s just the minimal case and it’s many more keystrokes in perfectly reasonable situations like:

```julia
f(keyword1 = keyword1, keyword2 = keyword2, keyword3 = keyword3)

```

versus

```julia
f(; keyword1, keyword2, keyword3)

```

Similarly for constructing named tuples with `(; name1, name2, name3)` syntax.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [November 6, 2018, 7:01pm UTC](https://discourse.julialang.org/t/implied-substitution/17200/10 "2018-11-06T19:01:19Z")

</div>

I was actually looking for this sort of thing last night.

This is a big deal in data science because I have to have multiple people read my scripts without having a great idea of how Julia works. I need the names to be very explicit so they at least now what I’m acting on. I could always just make a comment in my script but then whoever reads it would have to learn how I code certain variables and Julia syntax. Unlike other mathematics I can’t just use sigma and then everyone will know what I’m talking about

---

<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 7, 2018, 8:10am UTC](https://discourse.julialang.org/t/implied-substitution/17200/11 "2018-11-07T08:10:12Z")

</div>

> [@StefanKarpinski](#):
>
> there are many situations where one doesn’t particularly want to use single-letter names for clarity

I agree with this. But I still don’t understand how this goal meshes well with the `_` and `_1` syntax of partial application. It is my understanding that one can have either informative (longer) variable names, or the partial application syntax (for those variables), but not both at the same time. If I misunderstand, please clarify.

> [@StefanKarpinski](#):
>
> It’s easily dozens of keystrokes in completely reasonable and realistic situations.

You are right. I would frame it differently then: for the very simple cases which are most commonly used (closures with one or two arguments), the cost is small. For more complex cases, a the partial application syntax saves more keystrokes, but runs into other issues of complex/unclear semantics (as demonstrated by the discussion of [#24990](https://github.com/JuliaLang/julia/pull/24990) and the related PRs/issues).

Just to clarify: I was initially very enthusiastic about partial application syntax because think it would save quite a bit of typing. Then having carefully read all of the above discussions, I now think that all the proposed rules have to be quite complex because of various corner cases.

It _is_ possible to come up with well-defined semantics for these, but it would mean that I would need to remember some nontrivial rules about the `_` syntax. This would make me treat it like precedence: I know that the rules exist, I know that they are well-defined, but I still don’t rely on all them since I would have to keep looking them up. So I would probably limit myself to the simple cases (like `_ + y`), in which case a closure is not that much of a bother.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 7, 2018, 9:32am UTC](https://discourse.julialang.org/t/implied-substitution/17200/12 "2018-11-07T09:32:59Z")

</div>

I guess for some functions that don’t support one argument methods, they could `isequal(val)` it.

```julia
julia> magic(x, y) = abs2(x - y) - 2
magic (generic function with 1 method)

julia> magic(y) = (x -> magic(x, y))
magic (generic function with 2 methods)

julia> 3 |> (x -> magic(x, 2))
-1

julia> 3 |> magic(2)
-1

```
