Created
May 8, 2017 23:12
-
-
Save jamesrochabrun/8720e2c020dc4ec084ea7bef2e7a6f40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//swift functions are first class citizens which means that we can use them in many of the same operations which we use native types in. | |
//FUNCTIONS AS PROPERTIES | |
func sayHi(_ greeting: String) { | |
print(greeting) | |
} | |
let greeting = sayHi | |
greeting("Hello world!") | |
//FUNCTIONS AS PARAMETERS Closures | |
func displayString(usingFunction function: @escaping (String) -> ()) { | |
let str = "I am a great competion" | |
function(str) | |
} | |
displayString(usingFunction: sayHi) | |
displayString { (string) in | |
print(string) | |
} | |
extension Int { | |
func applyOperation(_ operation: (Int) -> Int) { | |
operation(self) | |
} | |
} | |
func closestMultipleOfSix(_ value: Int) -> Int { | |
for x in 1...100 { | |
let multiple = x * 6 | |
if multiple - value < 6 && multiple > value { | |
return multiple | |
} else if multiple == value { | |
return value | |
} | |
} | |
return 0 | |
} | |
32.applyOperation(closestMultipleOfSix) | |
//RETURNING A FUNCTION | |
func gameCounter() -> (Int) -> () { | |
//capturing the state of the variable | |
//closures are reference types | |
//closures capture variables from the surounding context and store reference to them | |
//A combination of a function and an environment of captured variables is called a | |
var localCounter = 0 | |
func increment(_ i: Int) { | |
localCounter += i | |
print("integer passed: \(localCounter)") | |
} | |
return increment | |
} | |
let counter = gameCounter() | |
counter(8) | |
//CODING QUIZ | |
extension String { | |
func transform(_ completion: (String) -> String) -> String { | |
return completion(self) | |
} | |
} | |
extension Character { | |
var isVowel: Bool { | |
let vowels: [Character] = ["a", "e", "i", "o", "u"] | |
return vowels.contains(self) | |
} | |
} | |
func removeVowels(from : String) -> String { | |
var noVowels: String = "" | |
for v in from.characters { | |
if v.isVowel { | |
noVowels += String(v) | |
} | |
} | |
return noVowels | |
} | |
let amazonas = "amazonas" | |
amazonas.transform(removeVowels) | |
//MAP CLOSURE EXPRESSIONS | |
func double(_ value: Int) -> Int { | |
return value * 2 | |
} | |
let numbers = [1, 2, 3, 4] | |
let doubledNumbers = numbers.map(double) | |
//closure expression syntax | |
let triple = numbers.map({(value: Int) -> Int in | |
return value * 3 | |
}) | |
//let triple = numbers.map{$0 * 3} | |
triple | |
//trailing closures | |
//only if the closure is the last argument | |
numbers.map() { n -> Int in | |
if n % 2 == 0 { | |
return n / 2 | |
} | |
return n / 3 | |
} | |
//CHALLENGE | |
let doubler = double | |
let doubledValues = [1,2,3,4].map{$0 * 2} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment