Consider the Following – Part 2

Van Simmons

|

3 min read
Clouds that make the shape of the Swift logo
  • a plain function declaration
  • an `init` on the return type
  • a computed `var` on the argument type
func doubler(_ anInt: Int) -> String {
    "    \(anInt * 2)    "
}

extension String {
    init(doubler anInt: Int) {
        self = "    \(anInt * 2)    "
    }
}

extension Int {
    var doubler: String {
        let anInt = self
        return "    \(anInt * 2)    "
    }
}
let arr = [1, 2 , 3, 4]

let result1 = arr.map(doubler)
print(result1)
let result2 = arr.map(String.init(doubler:))
print(result2)
let result3 = arr.map(\.doubler)
print(result3)

 ["    2    ", "    4    ", "    6    ", "    8    "]

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or “points”) on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments.

let result4 = arr.map { anInt in "    \(anInt * 2)    " }
print(result4)
Newsletter Form
Abstract graffiti-style artwork with vivid colors and dynamic shapes.
Simple Form

Connect with our team.

Error

An error has occured, please try again.

Try again