-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//: [Escaping closure](@previous) | ||
|
||
struct Test { | ||
init() { | ||
print("Test initialized") | ||
} | ||
} | ||
|
||
func testFunc(isEnabled: Bool, closure: @autoclosure () -> Test) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for me, I'd replace |
||
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: () -> ()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Styling mark: I don't like names like |
||
func through(array arr: [Int], f: (Int) -> (Int)) -> [Int] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Inquisitor0 More imagination please. E. g. you may replace |
||
$0 * $0 | ||
} | ||
square(10) | ||
|
||
var newSquare = square | ||
|
||
newSquare(5) | ||
|
||
//: [Closure as function parameter](@next) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test
– bad name.