This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
int | |
main(void) { | |
void rotate(int *mat, int side); | |
#define Side 4 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension LazySequence { | |
/** | |
First element of the sequence. | |
:returns: First element of the sequence if present | |
*/ | |
func first()-> S.Generator.Element? { | |
var g = self.generate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct SpellChecker { | |
var knownWords: [String:Int] = [:] | |
init?(contentsOfFile file: String) { | |
if let text = String(contentsOfFile: file, encoding: NSUTF8StringEncoding, error: nil)?.lowercaseString { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var (a, b) = (1, 0) | |
var fibs = lazy( | |
GeneratorOf<Int> { | |
(b, a) = (a, b + a) | |
return b | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
OlderNewer