Skip to content

Instantly share code, notes, and snippets.

@ilyannn
ilyannn / bangbang.md
Last active June 29, 2017 17:49 — forked from erica/bangbang.md

Instead of

assert(!array.isEmpty, "Array guaranteed to be non-empty because...")
let lastItem = array.last!

guard !array.isEmpty 
    else { fatalError("Array guaranteed to be non-empty because...") }

// ... etc ...
// TODO: insert guards to use open-source version of random() if necessary
func random(x: Int) -> Int {
return Int(arc4random_uniform(UInt32(endBoundary)))
}
extension CollectionType {
/// create a lazy succession of randomly ordered indices
///
@ilyannn
ilyannn / elvisOperatorStdLib.swift
Last active December 5, 2015 23:29 — forked from masters3d/elvisOperatorStdLib.swift
An experiment in removing tetriary expressions from stdlib (thanks to masters3d for original file)
//Filter.swift
public mutating func next() -> Base.Element? {
var n: Base.Element?
for/*ever*/;; {
n = _base.next()
if n != nil ? _predicate(n!) : true {
return n
}
}
@ilyannn
ilyannn / indents.swift
Last active November 24, 2015 19:05 — forked from erica/indents.swift
enum StringPaddingStyle {
case Left, Right
}
func padString(source: String,
with character: Character = " ",
fill target: Int,
style: StringPaddingStyle = .Left
) -> String {