Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / Color.swift
Created January 9, 2015 08:21
Fun with default arguments in Swift initialisers
import UIKit
struct Color {
let color: UIColor
init(r: CGFloat = 0.0,
g: CGFloat = 0.0,
b: CGFloat = 0.0,
a: CGFloat = 1.0)
{
color = UIColor(red: r, green: g, blue: b, alpha: a)
@pyrtsa
pyrtsa / A001761.hs
Created December 21, 2014 09:51
An exploration of https://oeis.org/A001761 in Haskell.
import Text.Printf (printf)
import Data.List (insertBy, delete)
-- Problem
-- ========
--
-- Using numbers `[1...n]`, form all sequences of length `2 * n` such that:
--
-- 1. Every number appears exactly twice.
-- 2. For any x < y, both occurrences of x must either precede or follow the
@pyrtsa
pyrtsa / Heap.swift
Last active August 29, 2015 14:10
Min-heap in Swift
/// Push `value` into the min-heap `xs`, as defined by the less-than
/// comparison `comp`.
public func pushHeap<T>(inout xs: [T], value: T, compare: (T, T) -> Bool) {
xs.append(value)
siftUp(&xs, xs.startIndex, xs.endIndex, compare, xs.count)
}
/// Push `value` into the min-heap `xs`, as defined by `<`.
public func pushHeap<T: Comparable>(inout xs: [T], value: T) {
pushHeap(&xs, value) {a, b in a < b}
@pyrtsa
pyrtsa / README.md
Last active August 29, 2015 14:08 — forked from otiose/README.md

Intro

All the Java code here comes from Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.

The Swift code was written by myself. Although it is mainly a direct port of the Java code :)

The goal was to see how fast Swift would compare to a language such as Java in a somewhat non-trivial case.

The input used was largeUF.txt, also provided by the Algorithms book. It is about 27mb, so I left it out of the gist :)

@pyrtsa
pyrtsa / Decompose.swift
Created October 10, 2014 08:55
Decomposing any Sliceable into the first element and the rest in Swift.
/// Split the `Sliceable` collection `coll` into its head and tail.
/// Returns `nil` if the collection is empty.
func decompose<S : Sliceable>(coll: S)
-> (S.Generator.Element, S.SubSlice)?
{
let start: S.Index = coll.startIndex
let end: S.Index = coll.endIndex
return start == end ? nil
: (coll[start], coll[start.successor()..<end])
}
@pyrtsa
pyrtsa / NSDateMath.swift
Last active August 29, 2015 14:07
An example of dealing with NSDate in Swift
import Foundation
// MARK: Comparable instance for NSDate
public func <(a: NSDate, b: NSDate) -> Bool {
return a.compare(b) == .OrderedAscending
}
public func ==(a: NSDate, b: NSDate) -> Bool {
return a.compare(b) == .OrderedSame
}
@pyrtsa
pyrtsa / JSON-playground.swift
Last active August 29, 2015 14:07
Playing with JSON input on the Swift playground
import Foundation
extension String {
func utf8() -> NSData? { return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding) }
}
extension Optional {
func flatMap<U>(transform: T -> U?) -> U? {
if let x = self {
return transform(x)
@pyrtsa
pyrtsa / Managing-optionals-in-Swift.swift
Last active March 7, 2017 18:39
Managing optionals in Swift
// Hello, fellow Swift programmer!
//
// Here's a demonstration of a few ways of cleaning up the unwrapping of
// optionals in Swift.
//
// Swift is known to have both OO and functional background. Thus, there is
// probably some middle ground programming style that is *more* functional than
// Objective-C, and probably less so than Haskell. This playground tries to
// compare their differences in the pretty common scenario of dealing with
// errors or missing input.
@pyrtsa
pyrtsa / StringSplit.swift
Created October 4, 2014 13:45
Splitting strings by a separator string in Swift
import Foundation
extension String {
public func split(separator: String) -> [String] {
if separator.isEmpty {
return map(self) { String($0) }
}
if var pre = self.rangeOfString(separator) {
var parts = [self.substringToIndex(pre.startIndex)]
while let rng = self.rangeOfString(separator, range: pre.endIndex..<endIndex) {
@pyrtsa
pyrtsa / GroupBy.swift
Created October 4, 2014 13:17
Grouping the elements of a sequence by a key function. (This may not be the fastest implementation out there.)
func groupBy<C: CollectionType, K: Hashable>
(xs: C, key: C.Generator.Element -> K) -> [K:[C.Generator.Element]]
{
var gs: [K:[C.Generator.Element]] = [:]
for x in xs {
let k = key(x)
var ys = gs[k] ?? []
ys.append(x)
gs.updateValue(ys, forKey: k)
}