Skip to content

Instantly share code, notes, and snippets.

@ilyannn
ilyannn / bound.swift
Last active August 29, 2015 14:03
How to encapsulate NSError** in Swift
enum State {
case Success(value:String)
case Error(error:NSError)
}
class Bound:Printable {
let pointer:NSErrorPointer
var state:State
var step:Int = 0
@ilyannn
ilyannn / Unwrapper.swift
Created July 15, 2014 13:13
Unwrapping a bunch of optionals
class Unwrapper<T, R> {
let failed:R?
let values:[T]
init(values: [T] = [], failed:R? = nil) {
self.values = values
self.failed = failed
}
class func unwrap(value:T?, elseReturn safe:R) -> Unwrapper<T, R> {
@ilyannn
ilyannn / Universe-crashes.swift
Last active August 29, 2015 14:04
Enumerating enum values – a universal approach
// This new version seems to be syntactically correct,
// but it crashes together with Xcode as of beta 3.
// (radar link should appear here)
// Find smallest N such that f(N) = nil.
func exhaust<T>(fun: Int -> T?) -> Int {
var index = 0
while fun(index) { index++ }
return index
}
@ilyannn
ilyannn / StringElements.swift
Last active August 29, 2015 14:04
String elements in Swift
// 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 {
@ilyannn
ilyannn / Error.swift
Last active August 29, 2015 14:05
An example of Swift style
// 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.
@ilyannn
ilyannn / utf8.swift
Last active August 29, 2015 14:05
UTF8 conversion routines test
#!/usr/bin/env xcrun swift -O3
//
// utf.swift
//
//
// Created by Ilya Nikokoshev on 8/22/14.
//
//
import Foundation
@ilyannn
ilyannn / Timer.swift
Last active August 29, 2015 14:06
Timer with Alice, Bob, Carol, Daniel and Eva.
// -------------------------
// 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
}
@ilyannn
ilyannn / ShowPrices.swift
Last active August 29, 2015 14:22
Erica's code challenge
// 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 {
@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 {
@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
}
}