Last active
August 29, 2015 14:03
-
-
Save erica/6faee76937ab8cb76e5a to your computer and use it in GitHub Desktop.
Whompage
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 Cocoa | |
// See http://www.ruby-doc.org/core-2.1.1/Enumerable.html | |
extension Array { | |
var first : Element? {return self.count > 0 ? self[0] : nil} | |
var last : Element? {return self.count > 0 ? self[self.count - 1] : nil} | |
var random : Element? {return self.count > 0 ? self[Int(arc4random_uniform(UInt32(self.count)))] : nil} | |
mutating func scramble() { | |
let halfCount : Int = self.count / 2 | |
let count = self.count | |
for i in 0..halfCount { | |
var r = i + Int(arc4random_uniform(UInt32(count - i))) | |
swap(&self[i], &self[r]) | |
} | |
} | |
// MARK: Return the element that wins the test | |
func testBy<U : Comparable>( | |
testClosure : (testValue: U, currentValue: U) -> Bool, | |
processingClosure: (Element) -> U) -> Element? | |
{ | |
if self.count == 0 {return nil} | |
var winningValue : U = processingClosure(self[0]) | |
var winningElement = self[0] | |
for item : Element in self { | |
var itemValue : U = processingClosure(item) | |
if testClosure(testValue:itemValue, currentValue:winningValue){ | |
winningValue = itemValue | |
winningElement = item | |
} | |
} | |
return winningElement | |
} | |
func maxBy<U : Comparable>(processingClosure: (Element) -> U) -> Element? { | |
var test = {(a:U, b:U) -> Bool in return a > b} | |
return self.testBy(test, processingClosure) | |
} | |
func minBy<U : Comparable>(processingClosure: (Element) -> U) -> Element? { | |
var test = {(a:U, b:U) -> Bool in return a < b} | |
return self.testBy(test, processingClosure) | |
} | |
// MARK: Return the value that wins the test | |
func testValue<U : Comparable>( | |
testClosure : (testValue: U, currentValue: U) -> Bool, | |
processingClosure: (Element) -> U) -> U? | |
{ | |
if self.count == 0 {return nil} | |
var winningValue : U = processingClosure(self[0]) | |
for item : Element in self { | |
var itemValue : U = processingClosure(item) | |
if testClosure(testValue:itemValue, currentValue:winningValue){ | |
winningValue = itemValue | |
} | |
} | |
return winningValue | |
} | |
func maxValue<U : Comparable>(processingClosure: (Element) -> U) -> U? { | |
var test = {(a:U, b:U) -> Bool in return a > b} | |
return self.testValue(test, processingClosure) | |
} | |
func minValue<U : Comparable>(processingClosure: (Element) -> U) -> U? { | |
var test = {(a:U, b:U) -> Bool in return a < b} | |
return self.testValue(test, processingClosure) | |
} | |
// let b = list.reduce(0) { max($0, $1)} | |
// MARK: test across | |
func all(processingClosure: (Element) -> Bool) -> Bool { | |
if self.count == 0 {return false} // or should this be trivially true? | |
for item : Element in self { | |
if (!processingClosure(item)) {return false} | |
} | |
return true | |
} | |
func any(processingClosure: (Element) -> Bool) -> Bool { | |
if self.count == 0 {return false} | |
for item : Element in self { | |
if (processingClosure(item)) {return true} | |
} | |
return false | |
} | |
// I need to understand chunk better | |
// MARK: Choose, select | |
func collect(processingClosure: (Element) -> Bool) -> Element[] { | |
var results = Element[]() | |
for item : Element in self { | |
if (processingClosure(item)) {results += item} | |
} | |
return results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment