Skip to content

Instantly share code, notes, and snippets.

View plumhead's full-sized avatar

Andy Calderbank plumhead

View GitHub Profile
@plumhead
plumhead / gist:a4955117bbda5f6bab6f
Last active August 29, 2015 14:02
Swift Fib Pipe
// a copy of the f# fib implementation
func fib(n : Int) -> Int {
if n == 0 || n == 1 {
return n
}
else {
return fib(n - 1) + fib(n - 2)
}
}
@plumhead
plumhead / gist:9ac80e348c2c4533b761
Last active August 29, 2015 14:02
FSharp Fib Pipeline
/// Fibonacci Number formula
let rec fib n =
match n with
| 0 | 1 -> n
| _ -> fib (n - 1) + fib (n - 2)
// Print even fibs
[1 .. 10]
|> List.map fib
|> List.filter (fun n -> (n % 2) = 0)
operator infix |> {associativity left precedence 140}
func |> <T,U> (left: @auto_closure () -> T,right: T -> U) -> U {
return right(left())
}
func add(n1 : Int)(n2: Int) -> Int {return n1 + n2}
let addone = add(1)
let addtwo = add(2)
let a1 = addone(n2:10) |> addone |> addtwo
@plumhead
plumhead / gist:c4597bdda147073cf791
Created June 6, 2014 22:01
Hiding GCD behing Async
//
// Async.swift
// TestSwiftUI
//
// Created by Plumhead on 06/06/2014.
//
// There are many holes in this which will be ironed out over time - put up as a starting point for ideas
//
import Foundation
@plumhead
plumhead / gist:bf359b6fc3833ebd3cd2
Created June 8, 2014 21:09
Partition and Choose
operator infix |> {associativity left precedence 140}
func |> <T,U> (left: @auto_closure () -> T,right: T -> U) -> U {
return right(left())
}
func partition<T>(f : T -> Bool)(a : Array<T>) -> (left: Array<T>, right: Array<T>) {
var (leftpartition,rightpartition) = (Array<T>(),Array<T>())
for (index, value) in enumerate(a) {
@plumhead
plumhead / gist:f5947ab7cf42107bb87e
Last active August 29, 2015 14:02
Swift Singletons
// Playground - noun: a place where people can play
import UIKit
import Foundation
import Swift
class MySingleton {
var amount : Float!
operator infix |> {associativity left precedence 140}
func |> <T,U> (left: @auto_closure () -> T,right: T -> U) -> U {
return right(left())
}
func fold<S,T>(f : (S,T) -> S,acc: S)(s : Array<T>) -> S {
if s.isEmpty {
return acc
@plumhead
plumhead / gist:07ec4be10c5c968304c4
Last active August 29, 2015 14:02
Pipe and Chains!
func getOrElse<S>(f : @auto_closure () -> S)(a : S?) -> S {
if let r = a {
return r
}
else {
return f()
}
}
class Times2Generator : Generator {
typealias Element = (original: Int,timestwo: Int)?
func next() -> Element? {
if source.count > 0 {
let result = self.source[0]
source = Array(source[1..source.count])
return (result,result * 2)
}
else {
@plumhead
plumhead / gist:f8452beed6be9c224771
Created September 7, 2014 12:00
Swift Function Delegates
import UIKit
// Investigations on Protocols and function delegates
/*
The accepted way of doing things
*/
protocol ProtoDelegate {