Edit fiddle - JSFiddle - Code Playground

PHOTO EMBED

Tue Apr 18 2023 13:41:24 GMT+0000 (Coordinated Universal Time)

Saved by @Sree #undefined

import UIKit

​

// Closures lesson // https://www.udemy// // //.com/course/intermediate-ios--advance-your-skills-xcode--swift-3/learn/lecture/6#notes

// Long way 1. Write a func eg. doMath() that takes in 2 ints and another func as a type eg. takes in 2 integers then performs the input func on it

​
6
// 2. Then write a separate func that will be passed in. eg multiply

// 3. Call the first func and pass in the 2nd func
8
// 4. Use a closure instead of passing in th func, by hitting enter at the func param part
9
​
10
​

// Long way 1. Write a func eg. doMath() that takes in 2 ints and another func as a type eg. takes in 2 integers then performs the input func on it

​

func doMath(a: Int, b: Int, mathFunc: (Int, Int) -> Int) -> Int {

    return mathFunc(a, b)

}

​

// 2. Then write a separate func that will be passed in. eg multiply

func multiply (c: Int, d: Int) -> Int {

    return c * d
20
}

​

// 3. Call the first func and pass in the 2nd func

print(doMath(a: 5, b: 6, mathFunc: multiply(c:d:)))

​

// 4. Use a closure instead of passing in th func, by hitting enter at the func param part

doMath(a: 4, b: 6) { a, b in

    return a * b

}

​

print(doMath(a: 5, b: 5) { $0 * $1})

​

// Closure lesson Complete

​

// Higher Order funcs and typealias

// 1. Create an array of some stuff and name it

// 2. Write a func that takes the array and converts it to uppercase.

// 3. Write a func that takes the array and converts it to double
38
// 4. Call both funcs and see results

// 5. Both of the above functions are doing pretty mych the same thing. Creat a higher order function

// that takes in the string array and also another func that tells what to do each thing in that string array

// 6. Call the func for uppercasing - use enter to get the closure format

// 7. Replace the innerworking of the uppercaseArray and doubleArray with the changeArray func

// 8. Use shorthand $0 and $1 using curly braces {}

// 9. TypeAlias - allows to take an existing type and turn it into something else. It makes it easier to refer to a particular func type. You can give a func a name without refering to the func type eg. typealias changeValue = (String) -> String, replace the params for the edit func wiht it

​

// 1. Create an array of some stuff and name it

​

let myFam = ["Sree", "Ajit", "Krish", "Gaurav"]

​

// 2. Write a func that takes the array and converts it to uppercase.

func uppercase(_ name: [String]) -> [String] {

    var tempArray:[String] = []

​

    for str in name {

        tempArray.append(str.uppercased())

    }

    return tempArray

}

​

// 3. Write a func that takes the array and converts it to double

func doubleArray(_ name: [String]) -> [String] {

    var tempArray:[String] = []

​

    for str in name {

        tempArray.append(str.uppercased() + str.uppercased())

    }

    return tempArray

}

​

// 4. Call both funcs and see results

uppercase(myFam)

doubleArray(myFam)

​

// 5.Both of the above functions are doing pretty mych the same thing. Creat a higher order function

// that takes in the string array and also another func that tells what to do each thing in that string array.

// Note that the func behavior is not specified in the func, it is specified at the time of calling it

​

func changeArray(name: [String], theEditFunc: changeArrayValueFuncType) -> [String] {

    var tmpArray: [String] = []

​

    for str in name {

        tmpArray.append(theEditFunc(str))

    }

    return tmpArray

}

​

// 6. Call the func for uppercasing - use enter to get the closure format

print(changeArray(name: myFam) { $0.uppercased() })

changeArray(name: myFam) { (str) -> String in

    return str.lowercased()

}

​

// 7. Replace the innerworking of the uppercaseArray and doubleArray with the changeArray func

func uppercase2(_ str: [String]) -> [String] {

    return changeArray(name: str) { (string) -> String in

        return string.uppercased()

    }

}

​

func doubleArray2(_ name: [String]) -> [String] {

    return changeArray(name: name) { (string) -> String in

        return (string + string)

    }

}

​

uppercase2(myFam)

doubleArray2(myFam)

// 8. Use shorthand $0 and $1 using curly braces {}

func doubleArray3(_ name: [String]) -> [String] {

    return changeArray(name: name) { "Hey " + $0 }

}

doubleArray3(myFam)

// 9. TypeAlias - allows to take an existing type and turn it into something else. It makes it easier to refer to a particular func type. You can give a func a name without refering to the func type eg. typealias changeValue = (String) -> String, replace the params for the edit func wiht it

typealias changeArrayValueFuncType = (String) -> String

​

doubleArray3(myFam)

​

​
content_copyCOPY

https://jsfiddle.net/skutty/ws1jnbkf/