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 ...
def fold(s, p = 0): | |
for i in [i+1 for (i, c), n in zip(enumerate(s), s[1:]+' ') if c != n]: p = print(s[p], i-p, sep="", end="") or i |
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 | |
/// |
//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 | |
} | |
} |
enum StringPaddingStyle { | |
case Left, Right | |
} | |
func padString(source: String, | |
with character: Character = " ", | |
fill target: Int, | |
style: StringPaddingStyle = .Left | |
) -> String { | |
// For https://gist.github.com/erica/5477d5caccfbff04a802 | |
// MARK: Generic collections | |
import Foundation | |
import Swift | |
infix operator ➜ { associativity left } | |
func ➜(source: [String], f: (String) -> ()) -> () { | |
for element in source { |
// ------------------------- | |
// General-purpose functions. Created by Alice. | |
import Foundation | |
func <(lhs:NSDate, rhs:NSDate) -> Bool { | |
// This should be in the standard library. - Alice. | |
return lhs.compare(rhs) == .OrderedAscending | |
} |
#!/usr/bin/env xcrun swift -O3 | |
// | |
// utf.swift | |
// | |
// | |
// Created by Ilya Nikokoshev on 8/22/14. | |
// | |
// | |
import Foundation |
// A different style for an example code by David Owens. | |
// https://github.com/owensd/json-swift/blob/master/src/Error.swift | |
import Foundation | |
/** Creates a new type that is used to represent error information in Swift. | |
This is a new Swift-specific error type used to return error information. | |
The primary usage of this object is to return it as a `Failable` or | |
`FailableOf<T>` from function that could fail. |
// Demonstrates some rather elementary points about code points vs elements | |
import Foundation | |
import Swift | |
extension String { | |
func canonize() -> [Character] { | |
return Array(precomposedStringWithCanonicalMapping) | |
} | |
static func decanonize(chars: [Character]) -> String { |