Question: swift language [Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and String type) by
[Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and String type) by applying a user-defined condition (via a closure). The function will return true if the condition is met; otherwise, false will be returned. Here is the specification of the function. The function named compare Function will take three input parameters: obj1: Int? obj2: String? cond: (int, Int) -> Bool The function has Boolean as its return type. The parameter cond is a closure defining the condition (specified by users) used to compare two Int objects. You function needs to do the following. 1. Convert the obj2 object from String into an Int type. Let's assume that obj2 only contain numeric characters so the String.tolnt() always works. 2. If the input object (obj1 or obj2) is null, initialize it as 0 before passing to the closure. Please only use the operators P., ??, and / to accomplish this. 3. Invoke the closure with the objects obtained from step 1 and 2. 4. Return the value which the closure returns from step 3. To test your function, use the code snippet like below. val result = compareFunction(null, "4") (x, y -> Bool in x >= y In addition to testing the scenarios where obji or obj2 might be null, you also need to test all possible conditions to compare two Int objects such as 1. The first int is greater than the second one 2. The first int is less than the second one 3. The first one equals to the second one [Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and String type) by applying a user-defined condition (via a closure). The function will return true if the condition is met; otherwise, false will be returned. Here is the specification of the function. The function named compare Function will take three input parameters: obj1: Int? obj2: String? cond: (int, Int) -> Bool The function has Boolean as its return type. The parameter cond is a closure defining the condition (specified by users) used to compare two Int objects. You function needs to do the following. 1. Convert the obj2 object from String into an Int type. Let's assume that obj2 only contain numeric characters so the String.tolnt() always works. 2. If the input object (obj1 or obj2) is null, initialize it as 0 before passing to the closure. Please only use the operators P., ??, and / to accomplish this. 3. Invoke the closure with the objects obtained from step 1 and 2. 4. Return the value which the closure returns from step 3. To test your function, use the code snippet like below. val result = compareFunction(null, "4") (x, y -> Bool in x >= y In addition to testing the scenarios where obji or obj2 might be null, you also need to test all possible conditions to compare two Int objects such as 1. The first int is greater than the second one 2. The first int is less than the second one 3. The first one equals to the second one
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
