Skip to content

Instantly share code, notes, and snippets.

// determines the state of a given Connect Four board
// http://www.codewars.com/kata/529962509ce9545c76000afa/train/javascript
function connectFour(board) {
horizontal = straight(board, true);
if (horizontal) return horizontal;
vertical = straight(board, false);
if (vertical) return vertical;
//: Brute force Sudoku solver in Swift
import Foundation
// storage
struct Puzzle {
var array: [[Int]]
}
// accessors and a setter
@getaaron
getaaron / JavaScript Sudoku Solver.js
Last active July 11, 2022 01:15
Solve Sudoku puzzles in O(N²) time
function sudoku(puzzle) {
while (!isSolved(puzzle)) {
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
puzzle[y][x] = digit(puzzle, x, y);
}
}
}
return puzzle;
func measure(block : () -> ()) -> NSTimeInterval {
var averages = [NSTimeInterval]()
for _ in 1...1000 {
let date = NSDate()
block()
averages.append(NSDate().timeIntervalSinceDate(date))
}
return averages.reduce(0, combine: +) / Double(averages.count)
}
func mean(numbers: [Double]) -> Double {
return numbers.reduce(0, combine: +) / Double(numbers.count)
}
func mean(numbers: Double...) -> Double {
return mean(numbers)
}
mean(1, 2, 3, 4, 5) // 3
mean([1,2,3,4,5]) // 3
@getaaron
getaaron / SaveContexts.swift
Last active August 29, 2015 14:25
Attempts to call save() on each NSManagedObjectContext in a given array. Rolls back saved changes and throws the provided error if a save fails.
/* Attempts to call save() on each NSManagedObjectContext in a given array. Rolls back saved changes and throws the provided error if a save fails. */
func saveContexts(contexts: [NSManagedObjectContext]) throws {
var successfulContextSaves = [NSManagedObjectContext]()
for context in contexts where context.hasChanges {
do {
try context.save()
successfulContextSaves += [context]
@getaaron
getaaron / take_while.swift
Last active August 29, 2015 14:22
Ruby's take_while ported using a Swift 2.0 protocol extension
var source = [1, 2, 3, 4, 5, 0]
extension CollectionType {
func takeWhile(@noescape condition: (Self.Generator.Element) -> Bool) -> [Self.Generator.Element] {
var returnArray : [Self.Generator.Element] = []
for x in self {
guard condition(x) else { break }
returnArray.append(x)

CocoaPods only supports Swift on OS X 10.9 and newer, and iOS 8 and newer.

Here's why:

  • Swift is supported on OS X 10.9 / iOS 7 and newer, as stated by Apple numerous times.
  • There is no support for building static archives with Swift.
  • Dynamic frameworks are supported on all versions of OS X.
  • Dynamic frameworks are unsupported on iOS versions prior to 8:

> ld: warning: embedded dylibs/frameworks only run on iOS 8 or later.

typealias BOOLAEN = Bool
typealias LIKEON68K = UInt16
typealias STRING = String
typealias STRINGINDEX = String.Index
typealias BOOL = Bool
typealias UNICODESCALER = UnicodeScalar
typealias GONADS = String
typealias NUMBERSWITHDOTS = Double
typealias CH = Character
typealias AMANA = Range<STRINGINDEX>