Skip to content

Instantly share code, notes, and snippets.

View jarsen's full-sized avatar

Jason Larsen jarsen

  • Salt Lake City, Utah
View GitHub Profile
@jarsen
jarsen / histogram.swift
Created December 12, 2014 20:49
Functional Swift — Counting Frequencies
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] {
@jarsen
jarsen / regression.swift
Created October 10, 2014 20:24
Univariate Linear Regression
import Cocoa
class CSVDoubleSequence: SequenceType {
typealias GeneratorType = IndexingGenerator<Array<Double>>
let path: String
let values: [Double]
init(path: String) {
self.path = path
@jarsen
jarsen / mfunc.swift
Last active August 29, 2015 14:07
decisions, decisions...
extension Int {
class func add1(x: Int) -> Int {
return x + 1
}
func add2() -> Int {
return self + 2
}
}
@jarsen
jarsen / mse.swift
Last active August 29, 2015 14:07
how can I bind to a tuple in the closure?
func meanSquaredError(predictions: [Double], values: [Double]) -> Double {
let sum = reduce(Zip2(predictions, values), 0.0) { (acc, y) in
let diff = y.0 - y.1
return acc + (diff * diff)
}
return sum / Double(predictions.count)
}
// what I want is to bind the arguments in the closure to (acc, (yHat, y))
@jarsen
jarsen / PotentialFields.swift
Last active August 29, 2015 14:06
Potential Fields in Swift
// Playground - noun: a place where people can play
import UIKit
typealias Vector2D = (x: Float, y: Float)
typealias Point2D = Vector2D
func -(lhs: Vector2D, rhs: Vector2D) -> Vector2D {
return (lhs.x - rhs.x, lhs.y - rhs.y)
}
@jarsen
jarsen / take.swift
Created September 5, 2014 19:42
infinite sequences via generators & take
class EvenNaturalNumbers: SequenceType {
typealias GeneratorType = EvenNaturalNumbersGenerator
func generate() -> EvenNaturalNumbersGenerator {
return EvenNaturalNumbersGenerator()
}
}
class EvenNaturalNumbersGenerator : GeneratorType {
var current = 2
@jarsen
jarsen / error.swift
Last active August 29, 2015 14:05
Exploring error patterns in swift
enum Fallible<T> {
case Error(NSError)
case Some(T)
init(value: T) {
self = .Some(value)
}
init(error: NSError) {
self = .Error(error)
@jarsen
jarsen / enumcrap.swift
Created August 15, 2014 17:27
Making an enum out of your own classes. Not sure if this is a great idea, but it works and is cool.
class Name : StringLiteralConvertible, Equatable {
var firstName: String
var lastName: String
required init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
class func convertFromStringLiteral(value: StringLiteralType) -> Self {
@jarsen
jarsen / tricks.swift
Last active January 16, 2016 14:17
Swift Tricks
// also see http://oleb.net/blog/2014/07/swift-strings/
// Mark: Strings
// if you want to stringByReplacingOccurrencesOfString without using a nil range, you need to convert to NSString first
var str = "some junk"
let tmpString: NSString = str
str = tmpString.stringByReplacingOccurrencesOfString(" ", withString: "+", options: nil, range: NSMakeRange(1, 6))
// find the length of a swift string
@jarsen
jarsen / TinyWebOO.swift
Created June 12, 2014 19:17
TinyWeb program designed to run in a playground. Adapted from the OO java example in Functional Programming Patterns in Scala and Clojure (Pragmatic Bookshelf)
// TinyWeb OOP(ish) Style
import Foundation
class HTTPResponse {
let body: String
let responseCode: Int
init(body: String, responseCode: Int) {
self.body = body