# Can you avoid stating the arguments in an anonymous function?

**URL:** <https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795>\
**Category:** General Usage\
**Tags:** macros, dataframes\
**Created:** [October 15, 2021, 1:57am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795 "2021-10-15T01:57:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 15, 2021, 1:57am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/1 "2021-10-15T01:57:47Z")

</div>

Is it possible to avoid stating the arguments in an anonymous function?

```julia
x = (a=1,b=2)
( (a,b) -> a+b )(x...)

```

works fine, but could you do something like  
`( (_...) -> a+b )(x...)`

x would have to contain elements a and b. Otherwise there would be a run-time error.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [October 15, 2021, 2:08am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/2 "2021-10-15T02:08:16Z")

</div>

I think you can only do that with varags, and it’s not exactly what you want but

```julia
x = (a...) -> sum(a)
julia> x(1,2,3)
6

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 15, 2021, 2:11am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/3 "2021-10-15T02:11:28Z")

</div>

> [@Lincoln\_Hannah](#):
>
> ( (\_…) → a+b )(x…)

what does this mean? (i.e. what do you want it to do?) For example, what even is `a,b`? If you want to do something and don’t care about the argument, you should

```julia
julia> x
(a = 1, b = 2)

julia> values(x)
(1, 2)

julia> sum(x)
3

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 15, 2021, 2:19am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/4 "2021-10-15T02:19:03Z")

</div>

I’d like to use variables in the body of a function without listing them explicitly as function inputs.

(probably not possible. just thought I’d check)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 15, 2021, 2:27am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/5 "2021-10-15T02:27:58Z")

</div>

> [@Lincoln\_Hannah](#):
>
> in the body of a function without listing them explicitly as function inputs

how is that logically possible? how does the function know what variables to use then? maybe you’re thinking about closure?

```julia
julia> g(y0) = x-> x+y0
g (generic function with 1 method)

julia> g(3)(4)
7

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 15, 2021, 2:45am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/6 "2021-10-15T02:45:37Z")

</div>

Background –  
For DataFrames, I’d like an easy way to do row-level operations, and use the column names as variable names and have any created variables added automatically as new columns.

in this discussing a nice method was suggested

> [@Apply function By Row without re-stating column names](https://discourse.julialang.org/t/apply-function-by-row-without-re-stating-column-names/65765/18):
>
> The most complete docs are probably [here](https://dataframes.juliadata.org/stable/man/split_apply_combine/). But in general you want AsTable(slight_smile in the src and AsTable in the dest for a src =\> fun =\> dest command.

but it requires writing a function where you have to specify the column names as inputs and outputs.

```julia
function f(; a, b, ... )
    c = a + b
    d = a - b
    (; a, b, c, d) 
end

D = DataFrame( a=[1,2], b=[3.4) )

@chain D begin
       transform( AsTable(:) => ByRow(x->f(x...)) => AsTable )
end

```

I’d like to avoid re-specifying the column names at all. Something like:

```julia
@chain D begin
        ***drop to row level**** begin
            c = a + b
            d = a - b
            end
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 2:54am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/7 "2021-10-15T02:54:23Z")

</div>

Yes. DataFramesMeta.jl does exactly this.

```julia
julia> df = DataFrame(a = [1, 2, 3], b = [5, 6, 7]);

julia> @rtransform df begin
           :c = :a + :b
           :d = :a - :b
       end
3×4 DataFrame
 Row │ a b c d
     │ Int64 Int64 Int64 Int64
─────┼────────────────────────────
   1 │ 1 5 6 -4
   2 │ 2 6 8 -4
   3 │ 3 7 10 -4

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 15, 2021, 3:17am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/8 "2021-10-15T03:17:19Z")

</div>

I have DataFramesMeta installed but for some reason @rtransform isn’t recognised

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 3:19am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/9 "2021-10-15T03:19:20Z")

</div>

Check your version. Current version is 0.9.1 `@rtransform` should be in 0.9.0 and newer.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 15, 2021, 4:02am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/10 "2021-10-15T04:02:46Z")

</div>

works 🙂 thank you so much.

is there any way to avoid the colons in the variable names?

Related question - there are various DF filtering functions @where, @subset  
All seem to require vectorising the conditional statement eg :a .\> 3.

Is there a way of doing @rsubset so you don’t have to vectorise the statument e.g :a \> 3

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 6:37pm UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/11 "2021-10-15T18:37:00Z")

</div>

No, you can’t avoid the colons. Columns are referenced as `:x`.

The reason for this is that we need a way to distinguish, at parse time, the columns in a data frame from other variables. Without knowing anything about the data frame.

```julia
x = 1
@rtransform df :y = :x + x

```

Obviously it’s possible to make unquoted symbols, i.e. `x` column references and leave special syntax for everything else. But then you would hvae to apply lots of escaping rules.

```julia
@rtransform df y = begin 
    $x = 100
    x + z
end

```

This might get out of hand when people want to use `missing`, `map` with a function as the first argument, etc.

I would put a positive spin on this and say the use of `:x` makes code more readable because you can distinguish easily between columns and variables, which can get confusing in `dplyr`.

Finally, `@rsubset` exists in 0.9.0 and newer. `@where` is deprecated in favor of subset, so there is just one filtering function in DataFramesMeta.jl.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 16, 2021, 1:21am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/12 "2021-10-16T01:21:24Z")

</div>

@rsubset Awesome thank you 🙂

Last question. Within an @rtransform bloc new columns can’t depend on other new columns.  
Is there a way to have a row level block with multiple interdependent new columns?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 16, 2021, 1:28am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/13 "2021-10-16T01:28:29Z")

</div>

Yes! I’m releasing a new version tomorrow with this feature. Stay tuned for the announcement.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 18, 2021, 12:07am UTC](https://discourse.julialang.org/t/can-you-avoid-stating-the-arguments-in-an-anonymous-function/69795/14 "2021-10-18T00:07:21Z")

</div>

Had some more questions. Put them in a new topic with appropriate heading.

> [@DataFramesMeta questions](https://discourse.julialang.org/t/dataframesmeta-questions/69952):
>
> Could groupby have a @ macro version, to avoid parenthesis? and the ability to define a column name within a groupby statement. Also, for a multi-line macro in a @chain block, followed by another macro - would it be possible to drop the begin and end? and define the end as the next macro So this @chain DataFrame( A = 1:10 ) begin @rtransform :Mod3 = :A % 3 groupby( :Mod3 ) @combine begin :Asum = :A |\> sum :Amax = :A |\> maximum end @rsubset :Mod3…
