Skip to content

Commit

Permalink
Added a few markdown to the Closures and Collection Types chapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Antonio Karmy committed Oct 23, 2016
1 parent 5a2d823 commit 864431d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 63 deletions.
@@ -1,29 +1,37 @@
/*:
[Previous](@previous) | [Next](@next)
****
Copyright (c) 2016 Juan Antonio Karmy.
Licensed under MIT License
Official Apple documentation available at [Swift Language Reference](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/)
See Juan Antonio Karmy - [karmy.co](http://karmy.co) | [@jkarmy](http://twitter.com/jkarmy)
****
*/


/*:
# Collection Types
*/

// |=------------------------------------------------------=|
// Copyright (c) 2016 Juan Antonio Karmy.
// Licensed under MIT License
//
// See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ for Swift Language Reference
//
// See Juan Antonio Karmy - http://karmy.co | http://twitter.com/jkarmy
//
// |=------------------------------------------------------=|

import UIKit

//Collection Types
/*:
Arrays and dictionaries in swift use generics and can be mutable or immutable
depending on whether they are assigned to a var or let
Structs are VALUE types, which means that when working with mutating functions, you'll need to store them in "var".
Everytime the struct is "mutated", a new struct will be created and stored in that var.
*/

// Playground - noun: a place where people can play

import UIKit

// Arrays and dictionaries in swift use generics and can be mutable or immutable
// depending on whether they are assigned to a var or let
// Structs are VALUE types, which means that when working with mutating functions, you'll need to store them in "var". Everytime the struct is "mutated", a new struct will be created and stored in that var.

//Arrays
//: ## Arrays

var shoppingList: [String] = ["Eggs", "Pigs"]
var anotherShoppingList = ["Eggs", "Pigs"] //Both are the same
var anotherShoppingList = ["Eggs", "Pigs"] //Both are the same


if shoppingList.isEmpty { //Checks if count == 0
Expand Down Expand Up @@ -54,7 +62,7 @@ var reversedShoppingList: [String] = shoppingList.reversed()
reversedShoppingList.removeLast() // Removes last item. Remove the first with removeFirst(). No returned value.
reversedShoppingList.popLast() // Pops the last item, removing it from the array and also returning it. Note that if the array is empty, the returned value is nil.

//Dictionaries
//: ## Dictionaries

var airports: [String: String] = ["JFK": "John F. Kennedy", "SCL": "Arturo Merino Benitez"]
airports = ["JFK": "John F. Kennedy", "SCL": "Arturo Merino Benitez"] //Also valid
Expand Down Expand Up @@ -93,16 +101,18 @@ numbers = [:] //Both do the same
numbers[16] = "sixteen"


// NOTE

// You can use your own custom types as dictionary key types by making
// them conform to the Hashable protocol from Swift’s standard library.
// Types that conform to the Hashable protocol must provide a
// gettable Int property called hashValue, and must also provide an
// implementation of the “is equal” operator (==). The value returned by a
// type’s hashValue property is not required to be the same across different
// executions of the same program, or in different programs.
//: ### NOTE

// All of Swift’s basic types (such as String, Int, Double, and Bool) are hashable by default
/*:
You can use your own custom types as dictionary key types by making
them conform to the Hashable protocol from Swift’s standard library.
Types that conform to the Hashable protocol must provide a
gettable Int property called hashValue, and must also provide an
implementation of the “is equal” operator (==). The value returned by a
type’s hashValue property is not required to be the same across different
executions of the same program, or in different programs.
All of Swift’s basic types (such as **String**, **Int**, **Double**, and **Bool**) are hashable by default
*/


@@ -1,49 +1,61 @@
/*:
[Previous](@previous) | [Next](@next)
****
Copyright (c) 2016 Juan Antonio Karmy.
Licensed under MIT License
Official Apple documentation available at [Swift Language Reference](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/)
See Juan Antonio Karmy - [karmy.co](http://karmy.co) | [@jkarmy](http://twitter.com/jkarmy)
****
*/

// |=------------------------------------------------------=|
// Copyright (c) 2016 Juan Antonio Karmy.
// Licensed under MIT License
//
// See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ for Swift Language Reference
//
// See Juan Antonio Karmy - http://karmy.co | http://twitter.com/jkarmy
//
// |=------------------------------------------------------=|

/*
===============
INLINE CLOSURES
===============
/*:
# CLOSURES
*/


/*:
## INLINE CLOSURES
*/

var array = ["John", "Tim", "Steve"]

var reversed = array.sorted(by: {(s1: String, s2: String) -> Bool in return s1 > s2})

//Using type inference, we can omit the params and return types. This is true when passing closures as params to a function.
//: Using type inference, we can omit the params and return types. This is true when passing closures as params to a function.
reversed = array.sorted(by: {s1, s2 in return s1 > s2})

//In case of single-expression closures, the return value is implicit, thus the return expression can be omitted.
//: In case of single-expression closures, the return value is implicit, thus the return expression can be omitted.
reversed = array.sorted(by: {s1, s2 in s1 == s2})

//In the previous examples, the names of the closure's params were explicit. You can use the $X variables to refer to params for the closure.
//This eliminates the need for the first params list, which makes the body the only relevant part.
/*: In the previous examples, the names of the closure's params were explicit.
You can use the $X variables to refer to params for the closure
This eliminates the need for the first params list, which makes the body the only relevant part.
*/
reversed = array.sorted(by: {$0 == $1})

//We can even take this to an extreme. String defines its own implementation for the ">" operator, which is really all the closure does.
//: We can even take this to an extreme. String defines its own implementation for the ">" operator, which is really all the closure does.
reversed = array.sorted(by: >)

/*##### TRAILING CLOSURES #####*/


//: ## TRAILING CLOSURES
func someFunctionThatTakesAClosure(closure: () -> ()) {
// function body goes here
}

//Closures which are too long to be defined inline.
// here's how you call this function without using a trailing closure:
/*:
Closures which are too long to be defined inline.
Here's how you call this function without using a trailing closure:
*/
someFunctionThatTakesAClosure(closure: {
// closure's body goes here
})

// here's how you call this function with a trailing closure instead:
//: Here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
// trailing closure's body goes here
Expand All @@ -52,7 +64,7 @@ someFunctionThatTakesAClosure() {
//Sorting function with inline closure:
array.sort() {$0 == $1}

//Note: In case the function doesn't take any params other than a trailing closure, there's no need for parenthesis.
//: **Note**: In case the function doesn't take any params other than a trailing closure, there's no need for parenthesis.

let digitNames = [
0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
Expand All @@ -71,13 +83,9 @@ let stringsArray = numbers.map {
return output
}

/*
============
AUTOCLOSURES
============
*/
//: ## AUTOCLOSURES

/*
/*:
An autoclosure is a closure that is automatically created to wrap an expression that’s being passed as an argument to a function.
It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it.
This syntactic convenience lets you omit braces around a function’s parameter by writing a normal expression instead of an explicit closure.
Expand All @@ -88,13 +96,13 @@ let stringsArray = numbers.map {
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
let customerProvider = { customersInLine.remove(at: 0) }

// Traditional way
//: Traditional way
func serve(customer customerProvider: () -> String) {
print("Now serving \(customerProvider())!") //The closure is called here!
}
serve(customer: { customersInLine.remove(at: 0) } )

// @autoclosure way
//: **@autoclosure** way
func serve(customer customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())!")
}
Expand Down
2 changes: 1 addition & 1 deletion The Swift Summary Book.playground/contents.xcplayground
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios' display-mode='raw'>
<playground version='6.0' target-platform='ios' display-mode='rendered'>
<pages>
<page name='01 The Basics'/>
<page name='02 Basic Operators'/>
Expand Down

0 comments on commit 864431d

Please sign in to comment.