This file contains hidden or 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 Array { | |
mutating func shuffle() { | |
if self.count < 2 { return } | |
for i in 0..<self.count { | |
let j = Int(arc4random_uniform(UInt32(self.count - i))) + i | |
if i != j { | |
swap(&self[i], &self[j]) | |
} | |
} | |
} |
This file contains hidden or 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 CollectionType { | |
func indexBy<Key: Hashable>(keyFunction: (Generator.Element) -> Key) -> [Key: [Generator.Element]] { | |
var map: [Key: [Generator.Element]] = [:] | |
for element in self { | |
let key = keyFunction(element) | |
var elements = map[key] ?? [] | |
elements.append(element) | |
map[key] = elements | |
} | |
return map |
This file contains hidden or 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 Dictionary { | |
mutating func merge(other: [Key: Value], by combine: (Value, Value) -> Value) { | |
for (key, newValue) in other { | |
if let oldValue = self[key] { | |
self[key] = combine(oldValue, newValue) | |
} else { | |
self[key] = newValue | |
} | |
} | |
} |
This file contains hidden or 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 PromiseKit | |
extension CollectionType { | |
func traverse<T>(transform: Generator.Element -> Promise<T>) -> Promise<[T]> { | |
return Promise { fulfill, reject in | |
var accumulator: [T] = [] | |
var generator = self.generate() | |
func recurse() { | |
guard let next = generator.next() else { | |
fulfill(accumulator) |
This file contains hidden or 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 permutations<Sequence: SequenceType>(items: Sequence) -> [[Sequence.Generator.Element]] { | |
return items.reduce([[]]) { (perms, item) in | |
return perms.flatMap { perm in | |
return (0 ... perm.count).map { i in | |
var copy = perm | |
copy.insert(item, atIndex: i) | |
return copy | |
} | |
} | |
} |
This file contains hidden or 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 power<Sequence: SequenceType>(items: Sequence) -> [[Sequence.Generator.Element]] { | |
return items.reduce([[]]) { (arrays, item) in | |
return arrays + arrays.map { (var array) in | |
array.append(item) | |
return array | |
} | |
} | |
} |
This file contains hidden or 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
#!/usr/bin/env ruby -w | |
require "rubygems" | |
require "nokogiri" | |
require "open-uri" | |
PROGRAM_BASE = File.basename $PROGRAM_NAME | |
def usage | |
puts <<-USAGE |
This file contains hidden or 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 NSComparisonResult { | |
init() { | |
self = .OrderedSame | |
} | |
var isAscending: Bool { | |
return self == .OrderedAscending | |
} | |
var isDescending: Bool { |
This file contains hidden or 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
#!/bin/sh | |
git add --update -- "$@" | |
git commit --message="trash -- $(date)" -- "$@" | |
git reset --soft HEAD^ | |
git reset --quiet -- "$@" | |
git checkout -- "$@" |
This file contains hidden or 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
cd $(seq -f .. -s / 1 ${1:-1}) | |
cd $(jot -b .. -s / ${1:-1}) | |
# using jot(1) is shorter, and doesn't add a trailing / | |
# but as far as I recall, jot(1) isn't part of GNU userland |