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 / BindThemAll.swift
Created March 18, 2015 21:17
Shows how you can use `bind` and `reduce` to short circuit a bunch of mapping
// Playground - noun: a place where people can play
import UIKit
public class Box<T> {
let unbox: T
init(_ value: T) {
unbox = value
}
}
@jarsen
jarsen / db.swift
Last active August 29, 2015 14:15
Database facade in front of realm that translates to and from our immutable structs
extension RLMRealm {
public func write(@noescape transaction: ()->()) {
self.beginWriteTransaction()
transaction()
self.commitWriteTransaction()
}
}
public struct Patient {
public let info: ModelInfo
// Playground - noun: a place where people can play
import UIKit
import CoreGraphics
public enum Result<T> {
case Success(@autoclosure () -> T)
case Failure(String)
init(_ value:T) {
@jarsen
jarsen / weird.swift
Created January 29, 2015 16:09
why does this crash?
func eratosthenes(n: Int) -> [Int]{
return map(enumerate(Array(1...n))) { (index, number) in
if number % index == 0 {
return -1
}
else {
return number
}
}
}
@jarsen
jarsen / ScoreKeeper.swift
Last active September 6, 2019 02:59
example of copy constructor with default values for immutability
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
@jarsen
jarsen / json.swift
Last active August 29, 2015 14:13
Parsing JSON Playground with Result type. Inspired by Haskell, Swiftz, and http://robots.thoughtbot.com/efficient-json-in-swift-with-functional-concepts-and-generics
// 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>
@jarsen
jarsen / List.swift
Created December 24, 2014 20:12
Functional linked list implemented in Swift. Inspired by functional data structure chapter in Functional Programming in Swift, and had to look at Swiftz to figure out it needed the @autoclosure... haven't quite figured out why but otherwise it seems to get into an infinite loop.
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 {
@jarsen
jarsen / composition.swift
Last active August 29, 2015 14:12
swift function composition using UTF-8 ring operator
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)) }
}
@jarsen
jarsen / reverseInt.hs
Created December 24, 2014 00:16
haskell reverseInt implementation
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)
@jarsen
jarsen / itoa.hs
Created December 24, 2014 00:15
haskell itoa implementation
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"