Skip to content

Instantly share code, notes, and snippets.

Donnacha Oisín Kidney oisdk

View GitHub Profile
View protextension.swift
public protocol Occupiable {
var isEmpty: Bool { get }
var isNotEmpty: Bool { get }
}
public extension Occupiable {
public var isNotEmpty: Bool { return !isEmpty }
}
extension CollectionType where Self : Occupiable { }
View RandomListOfSum.swift
import Foundation
func listSum(n: Int, tot: Int) ->[Int] {
return n == 1 ? [tot] :
{[$0, tot - $0] + listSum(n - 1, tot - $0)} (Int(arc4random_uniform(UInt32(tot - n))))
}
View MatrixRot.c
#include <stdio.h>
#include <math.h>
int
main(void) {
void rotate(int *mat, int side);
#define Side 4
View LazySequence.swift
extension LazySequence {
/**
First element of the sequence.
:returns: First element of the sequence if present
*/
func first()-> S.Generator.Element? {
var g = self.generate()
View airspeedSpellcheck.swift
import Foundation
struct SpellChecker {
var knownWords: [String:Int] = [:]
init?(contentsOfFile file: String) {
if let text = String(contentsOfFile: file, encoding: NSUTF8StringEncoding, error: nil)?.lowercaseString {
View fib.swift
var (a, b) = (1, 0)
var fibs = lazy(
GeneratorOf<Int> {
(b, a) = (a, b + a)
return b
View mergesort.swift
import Foundation
private func merge<T: Comparable> (a: ArraySlice<T>, b: ArraySlice<T>, mergeInto acc: ArraySlice<T> = []) -> ArraySlice<T> {
if let aF = a.first, bF = b.first {
return aF < bF ?
merge(dropFirst(a), b, mergeInto: acc + [aF]) :
merge(dropFirst(b), a, mergeInto: acc + [bF])
View Sequence.Swift
extension SequenceType {
var headTail: (Generator.Element, AnySequence<Generator.Element>)? {
var g = self.generate()
return g.next().map{($0, AnySequence{g})}
}
func scan<U>(initial: U, combine: (U, Generator.Element) -> U) -> [U] {
var prev = initial
return self.map{prev = combine(prev, $0); return prev}
}
func scan(combine: (Generator.Element, Generator.Element) -> Generator.Element) -> [Generator.Element]? {
View forCStyle.swift
func forCStyle<T>(var start: T, _ condition: T -> Bool, _ increment: T -> T) -> AnyGenerator<T> {
return anyGenerator {
defer { start = increment(start) }
return condition(start) ? start : nil
}
}
View funcSequences.Swift
extension SequenceType {
func prefixWhile(condition: Generator.Element -> Bool) -> [Generator.Element] {
var g = self.generate()
return Array( anyGenerator {
if let n = g.next() where condition(n) {
return n
} else {
You can’t perform that action at this time.