Skip to content

Swift3 samples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//: [Escaping closure](@previous)

struct Test {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test – bad name.

init() {
print("Test initialized")
}
}

func testFunc(isEnabled: Bool, closure: @autoclosure () -> Test) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testFunc – bad name.

if isEnabled {
closure()
}
}

testFunc(isEnabled: true, closure: Test())
testFunc(isEnabled: false, closure: Test())
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//: [Closure as function parameter](@previous)

import Foundation

struct FunctionStorage {

typealias ClosureType = (Int, Int) -> Int

private(set) var funcList: [ClosureType] = []

mutating func append(aFunction: @escaping ClosureType) {
Copy link

@GreatAndPowerfulKing GreatAndPowerfulKing Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for me, I'd replace aFunction with function.

funcList.append(aFunction)
}

/*: ### The functions append() accepts and adds to the array a closure declared outside the function. */

mutating func append(functions: @escaping ClosureType...) {
funcList.append(contentsOf: functions)
}

func apply(to value: Int,
and aValue: Int,
from initial: Int = 0, with action: ClosureType) -> Int {

return funcList.reduce(initial) { (res, fn) -> Int in
action(res, fn(value, aValue))
}
}
}

var storage = FunctionStorage()
storage.append(functions: (*), (+), (-))
storage.apply(to: 4, and: 2, with: (+))


/*: ### Default closure type is @noescape */

//: [Autoclosure](@next)



Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//: [Simple closure](@previous)

func repeatFunc(num: Int, f: () -> ()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much better to provide more meaningful external names and perhaps swap parameters. E. g. func repeatFunc(_ function: () -> (), times: Int). Maybe provide different external and internal names, It's up to you.

Swapping isn't required, 'cause it will make impossible to use this function with a trailing closure. But you should update function's signature to make it more meaningful.

for _ in 1...num {
f()
}
}
repeatFunc(num: 5) {
print("Hello world")
}



var square: (Int) -> (Int) = { x in
return x * x
}

let myArray = [0,1,2,3,4]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Styling mark: I don't like names like myArray and I'll advice to add spaces between array's items (like [0, 1, 2...])

func through(array arr: [Int], f: (Int) -> (Int)) -> [Int] {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f – bad external parameter name.

var result: [Int] = []
for i in arr {
result.append(f(i))
}
return result
}


let result = through(array: myArray) {
square($0)
}


//: [Escaping closure](@next)


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var hiThere: () -> (String) = {
return "Hi there!"
}
hiThere()


var square: (Int) -> (Int) = { x in
return x * x
}

/*: ###
Without explicitly declaring "x", you can use $0 as parameter:
*/
var squareAgain: (Int) -> (Int) = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Inquisitor0 More imagination please. E. g. you may replace squareAgain with multiply.

$0 * $0
}
square(10)

var newSquare = square

newSquare(5)

//: [Closure as function parameter](@next)
9 changes: 9 additions & 0 deletions Swift/SwiftClosures.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='macos' display-mode='raw'>
<pages>
<page name='Simple'/>
<page name='In function'/>
<page name='Escaping'/>
<page name='Autoclosure'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.