Add function application to Swift's standard library

Personally, I see it as a method without a name. The OPs example of an 'apply' method on Collection can be rewritten to:

  • Be an extension on all types
  • Use no method name instead of 'apply'

This is not possible in Swift, and we probably don't want to make this possible in general, but if you look at it this way, it's equivalent to the following:

// Pseudo Swift
extension<T> {
    func _<U>(_ transform: (Self) -> U) -> U {
        transform(self)
    }
}

This works for the base syntax: value.(function), as well as the value.{doStuff} variant, which just uses trailing closure syntax. Another valid way to write this would be value.() {doStuff}.

2 Likes