I have this piece of code which works fine:
var discoverEvents = events.map { event in
userEvents.find { $0.eventID == event.eventID } ?? event
}
I wonder if there is any way to reference event
by position inside find
somehow like that:
var discoverEvents = events.map { userEvents.find { $$0.eventID == $0.eventID } ?? $0 }
to make it one-liner?
No, it's not possible. The inner $0
shadows the outer one, and there is no other way to refer to the $0
in the outer scope.
As @DavidBerry mentions, that syntax is intended for short anonymous functions. Things could get very confusing otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With