Skip to content

Instantly share code, notes, and snippets.

View jakubpetrik's full-sized avatar
:octocat:

Jakub Petrík jakubpetrik

:octocat:
View GitHub Profile
@jakubpetrik
jakubpetrik / boolify.swift
Created January 11, 2019 15:54
Convert throwable to bool
func boolify(_ throwable: () throws -> Void) -> Bool {
var success = true
do {
_ = try throwable()
} catch {
success = false
}
return success
}
@jakubpetrik
jakubpetrik / Sequence+CompactCast.swift
Created April 17, 2018 06:31
Sequence+CompactCast
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)
@jakubpetrik
jakubpetrik / Instantiable.swift
Last active February 20, 2019 10:05
UIViewController+Instantiable
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()`.
@jakubpetrik
jakubpetrik / UIStoryboard+MakeController.swift
Last active April 9, 2018 14:24
UIStoryboard+MakeController
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
}
}
@jakubpetrik
jakubpetrik / Available.swift
Last active April 4, 2018 08:06
iOS version available (target iOS devices lower than a specific OS)
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 {
@objc protocol TableControllerDataSource {
func tableController(_ controller: TableController, numberOfRowsInSection section: Int) -> Int
func tableController(_ controller: TableController, cellControllerForRowAt indexPath: IndexPath) -> TableViewCellController
}
@jakubpetrik
jakubpetrik / flipping-bits-game.swift
Created October 10, 2015 16:14
Flipping bits game in Swift
/**
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
@jakubpetrik
jakubpetrik / tpk.swift
Created October 2, 2015 08:46
Trabb─Pardo─Knuth algorithm in Swift 2.0
/**
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:
@jakubpetrik
jakubpetrik / ordered-words.swift
Created October 1, 2015 13:29
Ordered words in Swift
/**
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.
*/
@jakubpetrik
jakubpetrik / echo.swift
Last active February 27, 2016 06:02
Native shebang - shell scripting in Swift (using Swift REPL)
/**
http://rosettacode.org/wiki/Native_shebang#Swift
In short: Use the specimen language (native) for "scripting".
Usage:
./echo.swift Hello, world!
Output:
Hello, world!