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
  • VA ProdOps and Data Quality Engineering
    VA ProdOps and Data Quality Engineering
    See more: VA ProdOps and Data Quality Engineering
    Small orange arrow symbol
  • Where the future is going: sniffing out all of Apple’s clues
    Where the future is going: sniffing out all of Apple’s clues
    See more: Where the future is going: sniffing out all of Apple’s clues
    Small orange arrow symbol
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