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 / rle.swift
Last active February 5, 2021 09:01
Run-length encoding in Swift.
/**
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.
*/
@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 / 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 / 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 / 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 {
@jakubpetrik
jakubpetrik / handle-signal.swift
Created September 23, 2015 21:34
Signal handling in Swift
/**
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,
@objc protocol TableControllerDataSource {
func tableController(_ controller: TableController, numberOfRowsInSection section: Int) -> Int
func tableController(_ controller: TableController, cellControllerForRowAt indexPath: IndexPath) -> TableViewCellController
}
@jakubpetrik
jakubpetrik / reverse-words.swift
Created September 24, 2015 06:04
Reverse words in a string.
/**
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.