Tuple Member Extraction in Closure Parameters


(Dennis Weissmann) #1

Hi swift-users,

Is it possible to “extract” the `tuple` tuple in the following code directly into two `Int` variables (`tuple` is of type `(Int, Int)`)?

let a = [0,1,2,3,4,5,6,7,8,9]
let b = [0,1,2,3,4,5,6,7,8,9]

let c = zip(a,b).reduce(0) { acc, tuple in
  acc + tuple.0 + tuple.1
}

Like so (does not compile):

let d = zip(a,b).reduce(0) { acc, (value1, value2) in
  acc + value1 + value2
}

My problem with the current approach is that is makes the calculation very unreadable / not comprehensible (i.e. `.0` and `.1` - my actual calculation and parameters are a bit more complicated). A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

Is this intentionally not supported?

Thanks,

- Dennis


(Krzysztof Siejkowski) #2

A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
let value1 = tuple.0
let value2 = tuple.1
return acc + value1 + value2
}
You can also get away with one assignment:

let ok = zip(a,b).reduce(0) { acc, tuple in
let (value1, value2) = tuple
return acc + value1 + value2
}

Best,
Krzysztof


(Dennis Weissmann) #3

Thanks Krzysztof, you’re right! I totally forgot about that neat feature.

However, there’s no way to do this directly in the parameter list, right?

Do you think such feature should exist?

- Dennis

···

On May 5, 2016, at 4:19 PM, Krzysztof Siejkowski <krzysztof@siejkowski.net> wrote:

A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

You can also get away with one assignment:

let ok = zip(a,b).reduce(0) { acc, tuple in
    let (value1, value2) = tuple
    return acc + value1 + value2

}

Best,
Krzysztof


(Matthew Johnson) #4

Destructuring of parameters would be pretty cool. If it gets added to Swift it should be a general feature for all functions, not just on closures. There are a lot of design directions and subtleties that would need to be explored. I doubt it is something that will be considered in the Swift 3 timeframe.

···

On May 5, 2016, at 9:43 AM, Dennis Weissmann via swift-users <swift-users@swift.org> wrote:

Thanks Krzysztof, you’re right! I totally forgot about that neat feature.

However, there’s no way to do this directly in the parameter list, right?

Do you think such feature should exist?

- Dennis

On May 5, 2016, at 4:19 PM, Krzysztof Siejkowski <krzysztof@siejkowski.net <mailto:krzysztof@siejkowski.net>> wrote:

A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

You can also get away with one assignment:

let ok = zip(a,b).reduce(0) { acc, tuple in
    let (value1, value2) = tuple
    return acc + value1 + value2

}

Best,
Krzysztof

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Sweeris) #5

Seems like a nice feature, though. I think it'd be worth starting a thread on the evolution list. I don't know if it would be done in time for 3.0, but it doesn't hurt to try.

- Dave Sweeris

···

On May 5, 2016, at 09:54, Matthew Johnson via swift-users <swift-users@swift.org> wrote:

Destructuring of parameters would be pretty cool. If it gets added to Swift it should be a general feature for all functions, not just on closures. There are a lot of design directions and subtleties that would need to be explored. I doubt it is something that will be considered in the Swift 3 timeframe.

On May 5, 2016, at 9:43 AM, Dennis Weissmann via swift-users <swift-users@swift.org> wrote:

Thanks Krzysztof, you’re right! I totally forgot about that neat feature.

However, there’s no way to do this directly in the parameter list, right?

Do you think such feature should exist?

- Dennis

On May 5, 2016, at 4:19 PM, Krzysztof Siejkowski <krzysztof@siejkowski.net> wrote:

A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

You can also get away with one assignment:

let ok = zip(a,b).reduce(0) { acc, tuple in
    let (value1, value2) = tuple
    return acc + value1 + value2

}

Best,
Krzysztof

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Dennis Weissmann) #6

Thanks Dave and Matthew, I’ll start a thread on swift-evolution later today or tomorrow.

- Dennis

···

On May 5, 2016, at 5:09 PM, David Sweeris <davesweeris@mac.com> wrote:

Seems like a nice feature, though. I think it'd be worth starting a thread on the evolution list. I don't know if it would be done in time for 3.0, but it doesn't hurt to try.

- Dave Sweeris

On May 5, 2016, at 09:54, Matthew Johnson via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Destructuring of parameters would be pretty cool. If it gets added to Swift it should be a general feature for all functions, not just on closures. There are a lot of design directions and subtleties that would need to be explored. I doubt it is something that will be considered in the Swift 3 timeframe.

On May 5, 2016, at 9:43 AM, Dennis Weissmann via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Thanks Krzysztof, you’re right! I totally forgot about that neat feature.

However, there’s no way to do this directly in the parameter list, right?

Do you think such feature should exist?

- Dennis

On May 5, 2016, at 4:19 PM, Krzysztof Siejkowski <krzysztof@siejkowski.net <mailto:krzysztof@siejkowski.net>> wrote:

A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

You can also get away with one assignment:

let ok = zip(a,b).reduce(0) { acc, tuple in
    let (value1, value2) = tuple
    return acc + value1 + value2

}

Best,
Krzysztof

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users