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/Run-length_encoding | |
Given a string containing uppercase characters (A-Z), | |
compress repeated 'runs' of the same character by storing the length of that run, | |
and provide a function to reverse the compression. | |
The output can be anything, as long as you can recreate the input with it. | |
*/ |
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
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
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
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
/** | |
http://rosettacode.org/wiki/Handle_a_signal | |
Most general purpose operating systems provide interrupt facilities, sometimes called signals. | |
Unhandled signals generally terminate a program in a disorderly manner. Signal handlers are | |
created so that the program behaves in a well-defined manner upon receipt of a signal. | |
For this task you will provide a program that displays a single integer on each line of output at | |
the rate of one integer in each half second. Upon receipt of the SigInt signal (often created by the user typing ctrl-C) | |
the program will cease printing integers to its output, print the number of seconds the program has run, |
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/Reverse_words_in_a_string | |
The task is to reverse the order of all tokens in each of a number of strings and display the result; | |
the order of characters within a token should not be modified. | |
Example: “Hey you, Bub!” would be shown reversed as: “Bub! you, Hey” | |
Tokens are any non-space characters separated by spaces (formally, white-space); | |
the visible punctuation forms part of the word within which it is located and should not be modified. |
NewerOlder