UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Can someone please clarify this? - If a method has the same signature as a closure parameter, we can use the method instead.

Forums > 100 Days of SwiftUI

This was in one of the answers in the Day 31 review questions.

If a method has the same signature as a closure parameter, we can use the method instead.

I basically get that methods are functions like closures and it seems like Paul is saying if a method has the same type as a closure paramater, we can use a method instead, but does someone have an example to illustrate this point?

3      

Sorting an array is probably the easiest way to demonstrate this.

import Foundation

let names = ["Charlotte", "Shauna", "Mildred", "Linton", "Jack", "Sonny"]

//sorted(by:) takes a closure (String, String) -> Bool
let sortedNames1 = names.sorted(by: { name1, name2 in
    name1 < name2
})
print(sortedNames1)
//["Charlotte", "Jack", "Linton", "Mildred", "Shauna", "Sonny"]

//function signature here is (String, String) -> Bool
func namesInAscendingOrder(name1: String, name2: String) -> Bool {
    name1 < name2
}
//we can pass the function instead of a closure
let sortedNames2 = names.sorted(by: namesInAscendingOrder)
print(sortedNames2)
//["Charlotte", "Jack", "Linton", "Mildred", "Shauna", "Sonny"]

3      

Great, thanks.

3      

Here's another example. This shows how a function requires you to provide:

  1. Two integers
  2. A function

What function does it require? The signature doesn't specify what the function needs to do. It only specifies the function needs to accept two integer, then return an integer. You can provide any function you want. It just has to conform to these rules.

But this example also shows several ways to provide that required function!

Three examples show how the function is passed in as a parameter. The other two examples show how to pass in the function using trailing closure syntax.

Paste this code into Playgrounds. See if this adds to the excellent answer posted above.

func applyFunction(to lhs: Int, and rhs: Int, thisFunction someFunction: (Int, Int) -> Int ) -> Int {
    // pass in the left hand side and right hand side values.
    // then perform the function with those values passed in.
    // I have no idea what the function does, but I'll run it anyway.
    return someFunction(lhs, rhs) //
}

func sekretFunction(x: Int, y: Int) -> Int { x * y } // shhhh! sekret!

let answer01 = applyFunction(to: 22, and: 20, thisFunction: { $0 + $1 } )    // provide a closure via the initializer
let answer02 = applyFunction(to: 40, and:  2) { $0 * 2 + $1 } // provide a closure using trailing closure syntax
let answer03 = applyFunction(to: 44, and:  2, thisFunction: sekretFunction ) // provide a function via initializer
let answer04 = applyFunction(to: 20, and:  5) { sekretFunction(x: $0, y: $1) } // provide a function via closure
let answer05 = applyFunction(to: 60, and: 20, thisFunction: / ) // Divide! it's a function that takes two ints and returns an int

3      

Different Signatures

Not totally related to your question. But take a look at readability! If an application requires you to write a function that applies another function to two integers, you might consider rearranging the variables in the signature.

Put the function first, followed by the two integers. In my example, you lose the ability to use trailing closure syntax. However, do you agree the final version reads much more like an English sentence?

func sekretFunction(x: Int, y: Int) -> Int { x * y } // shhhh! sekret!

// Reorder the parameters. 
func applyFunction(_ someFunction: (Int, Int) -> Int , to lhs: Int, and rhs: Int) -> Int {
    someFunction(lhs, rhs) // apply the function to the two variables.
}

// Do you agree? This reads much more like a sentence?
let answer01 = applyFunction(sekretFunction, to: 10, and: 30)  // Alternate signature. 

4      

Thanks for the additional examples:

This was the most interesting: let answer05 = applyFunction(to: 60, and: 20, thisFunction: / ) // Divide! it's a function that takes two ints and returns an int

I hadn't thought of / as a function.

And yes, the last line in the last post definitely makes it easier to read.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.