This file contains 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
func boolify(_ throwable: () throws -> Void) -> Bool { | |
var success = true | |
do { | |
_ = try throwable() | |
} catch { | |
success = false | |
} | |
return success | |
} |
This file contains 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
extension Sequence { | |
func compactCast<T>(to type: T.Type) -> [T] { | |
return compactMap { $0 as? T } | |
} | |
} | |
// usage | |
let items = [1, 2, nil, 4].compactCast(to: Int.self) |
This file contains 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
protocol Instantiable { | |
static func make() -> Self | |
} | |
extension Instantiable where Self: UIViewController { | |
/// Instantiates controller from storyboard. | |
/// - example: | |
/// `let myViewController = MyViewController.make()` | |
/// - important: | |
/// Initial controller of the same type must exists in storyboard named as controller's class, otherwise will `fatalError()`. |
This file contains 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
extension UIStoryboard { | |
static func make<T: UIViewController>(_ type: T.Type) -> T { | |
let storyboard = UIStoryboard(name: String(describing: type), bundle: nil) | |
guard let instance = storyboard.instantiateInitialViewController() as? T else { fatalError() } | |
return instance | |
} | |
} |
This file contains 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
typealias iOS = OperatingSystemVersion | |
extension OperatingSystemVersion { | |
init(_ major: Int) { | |
self.init(majorVersion: major, minorVersion: 0, patchVersion: 0) | |
} | |
} | |
extension OperatingSystemVersion: Comparable { | |
public static func < (lhs: OperatingSystemVersion, rhs: OperatingSystemVersion) -> Bool { |
This file contains 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
@objc protocol TableControllerDataSource { | |
func tableController(_ controller: TableController, numberOfRowsInSection section: Int) -> Int | |
func tableController(_ controller: TableController, cellControllerForRowAt indexPath: IndexPath) -> TableViewCellController | |
} |
This file contains 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
/** | |
http://rosettacode.org/wiki/Flipping_bits_game | |
The game | |
Given an N by N square array of zeroes or ones in an initial configuration, | |
and a target configuration of zeroes and ones The task is to transform one to the other in as | |
few moves as possible by inverting whole numbered rows or whole lettered columns at once, as one move. | |
In an inversion any 1 becomes 0, and any 0 becomes 1 for that whole row or column. | |
The Task |
This file contains 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
/** | |
http://rosettacode.org/wiki/Trabb_Pardo–Knuth_algorithm | |
The TPK algorithm is an early example of programming chrestomathy. | |
It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report | |
The Early Development of Programming Languages. | |
The report traces the early history of work in developing computer languages in the | |
1940s and 1950s, giving several translations of the algorithm. | |
The task is to implement the algorithm: |
This file contains 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
/** | |
http://rosettacode.org/wiki/Ordered_words | |
Define an ordered word as a word in which the letters of the word appear in alphabetic order. Examples include 'abbey' and 'dirt'. | |
The task is to find and display all the ordered words in this dictionary that | |
have the longest word length. | |
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.) | |
The display needs to be shown on this page. | |
*/ |
This file contains 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
/** | |
http://rosettacode.org/wiki/Native_shebang#Swift | |
In short: Use the specimen language (native) for "scripting". | |
Usage: | |
./echo.swift Hello, world! | |
Output: | |
Hello, world! |
NewerOlder