Anonymous functions with tuple arguments

I have a feeling this is a stupid question, but here it is anyway:

Suppose I have a collection of tuples.
Would it be possible to write anonymous function (x,y,…)->… where arguments are the tuple elements?

An example:

A = [ (x,y) for x=1:5, y=1:5 ]

I can extract and use the tuple elements directly in for loop:

for (x,y) in A
    println(x,y)
end

For an anonymous function I can write something like:

filter(t -> t[1]==1 || t[2]==1, A)

but I feel I want to write this as:

filter((x,y) -> x==1 || y==1, A)
1 Like

I don’t think that’s currently possible, but it’s been discussed: https://github.com/JuliaLang/julia/issues/6614

One other option would be:

filter(A) do xy
  x, y = xy
  x == 1 || y == 1
end

Note that this is now possible to do as follows:

filter(((x,y),) -> x==1 || y==1, A)
8 Likes