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 Int { | |
class func add1(x: Int) -> Int { | |
return x + 1 | |
} | |
func add2() -> Int { | |
return self + 2 | |
} | |
} |
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 Cocoa | |
class CSVDoubleSequence: SequenceType { | |
typealias GeneratorType = IndexingGenerator<Array<Double>> | |
let path: String | |
let values: [Double] | |
init(path: String) { | |
self.path = path | |
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 update<K,V>(var dictionary: [K:V], key: K, value: V) -> [K:V] { | |
dictionary[key] = value | |
return dictionary | |
} | |
func increment<T>(dictionary: [T:Int], key: T) -> [T:Int] { | |
return update(dictionary, key, map(dictionary[key]) {$0 + 1} ?? 1) | |
} | |
func histogram<T>(var s: [T]) -> [T:Int] { |
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
itoa :: (Integral a) => a -> String | |
itoa x | |
| x == 0 = "0" | |
| x == 1 = "1" | |
| x == 2 = "2" | |
| x == 3 = "3" | |
| x == 4 = "4" | |
| x == 5 = "5" | |
| x == 6 = "6" | |
| x == 7 = "7" |
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
reverseInt :: (Integral a) => a -> a | |
reverseInt x = reverseInt' x 0 | |
reverseInt' :: (Integral a) => a -> a -> a | |
reverseInt' n r | |
| n == 0 = r | |
| otherwise = reverseInt' (quot n 10) (10 * r + n `mod` 10) |
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
infix operator ∘ {} | |
func ∘<A,B,C>(f:B->C, g:A->B) -> (A->C) { | |
return compose(f, g) | |
} | |
func compose<A,B,C>(f:B->C, g:A->B) -> (A->C) { | |
return { x in f(g(x)) } | |
} |
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
enum List<T> : Printable { | |
case Empty | |
case Node(@autoclosure() -> T, Box<List<T>>) | |
init(_ head: T, _ tail: Box<List<T>>) { | |
self = .Node(head, tail) | |
} | |
var description: String { | |
get { |
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
// Playground - noun: a place where people can play | |
import Foundation | |
infix operator >>- { associativity left precedence 150 } // Bind | |
infix operator <^> { associativity left } // Functor's fmap (usually <$>) | |
infix operator <*> { associativity left } // Applicative's apply | |
public typealias JSON = AnyObject | |
public typealias JSONObject = Dictionary<String, JSON> |
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
struct Scorekeeper { | |
let runningScore: Int | |
let climbingScore: Int | |
let bikingScore: Int | |
let swimmingScore: Int | |
init(runningScore: Int = 0, climbingScore: Int = 0, bikingScore: Int = 0, swimmingScore: Int = 0) { | |
self.runningScore = runningScore | |
self.climbingScore = climbingScore | |
self.bikingScore = bikingScore |
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 eratosthenes(n: Int) -> [Int]{ | |
return map(enumerate(Array(1...n))) { (index, number) in | |
if number % index == 0 { | |
return -1 | |
} | |
else { | |
return number | |
} | |
} | |
} |