Skip to content

Instantly share code, notes, and snippets.

View oisdk's full-sized avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
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 { }
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))))
}
#include <stdio.h>
#include <math.h>
int
main(void) {
void rotate(int *mat, int side);
#define Side 4
extension LazySequence {
/**
First element of the sequence.
:returns: First element of the sequence if present
*/
func first()-> S.Generator.Element? {
var g = self.generate()
import Foundation
struct SpellChecker {
var knownWords: [String:Int] = [:]
init?(contentsOfFile file: String) {
if let text = String(contentsOfFile: file, encoding: NSUTF8StringEncoding, error: nil)?.lowercaseString {
@oisdk
oisdk / fib.swift
Last active August 29, 2015 14:21
var (a, b) = (1, 0)
var fibs = lazy(
GeneratorOf<Int> {
(b, a) = (a, b + a)
return b
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])
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]? {
extension SequenceType where Generator.Element: Hashable {
var uniques: [Generator.Element] {
var past = Set<Generator.Element>()
return self.filter {
element in
if past.contains(element) {
return false
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
}
}