Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am JadenGeller on github.
  • I am jaden (https://keybase.io/jaden) on keybase.
  • I have a public key whose fingerprint is CD7E 9E0B AE92 1CDB EBD9 3AFB 5989 E3D5 A174 FDD4

To claim this, I am signing this object:

@JadenGeller
JadenGeller / Swift Attempt Function.swift
Last active August 29, 2015 14:16
Swift Attempt Function
/*
* attempt takes a function that does not accept optional values
* and returns a new function that will work as usual with non-optional values
* and return nil when passed an optional value
*/
func attempt<T,U>(f: T -> U)-> T? -> U? {
return { y in
if let x = y {
return f(x)
@JadenGeller
JadenGeller / Swift Chain Function.swift
Last active June 6, 2018 05:37
Swift Chain Function
/*
* chain takes in any number of 1-argument functions as arguments
* and returns a function that executes each function in order
*/
func chain<T>(all: T -> () ...) -> T -> () {
return { x in
for f in all { f(x) }
}
}
@JadenGeller
JadenGeller / Swift Coalesce Function.swift
Last active August 29, 2015 14:16
Swift Coalesce Function
/*
* coalesce takes in a list of optional values
* and returns the first one that is not nil
*/
func coalesce<T>(all: @autoclosure () -> T? ...) -> T? {
for f: () -> T? in all {
if let x = f() { return x }
}
return nil
@JadenGeller
JadenGeller / Swift Unwrap Function.swift
Last active August 29, 2015 14:16
Swift Unwrap Function
/*
* Returns the default value whenever the primary value is nil
* Otherwise returns the primary value
*/
func unwrap<T>(primaryValue: T?, defaultValue: T) -> T {
if let value = primaryValue { return value }
else { return defaultValue }
}
// Examples
@JadenGeller
JadenGeller / Random Class.m
Last active August 29, 2015 14:16
Random Objective-C Class Returning Function
#import <objc/runtime.h>
Class randomClass(){
int numClasses = objc_getClassList(NULL, 0);
Class *classes = calloc(sizeof(Class), numClasses);
objc_getClassList(classes, numClasses);
Class randomClass = *(classes + arc4random_uniform(numClasses));
free(classes);
return randomClass;
}
@JadenGeller
JadenGeller / Swift Offset Generator.swift
Last active August 29, 2015 14:16
Swift Offset Generator
/*
* Allows you to offset offset a generator
* and loop back around to the initial values
*/
struct OffsetGenerator<G : GeneratorType> : GeneratorType {
var generator: G
var skipped = Array<G.Element>()
init(generator: G, var offset: Int){
self.generator = generator
@JadenGeller
JadenGeller / ParingGenerator.swift
Last active May 20, 2016 12:14
Swift Pair Generator
/*
* Allows iteration over pairs of a series
* This is useful for comparing or performing actions on
* adjacent items in an array
*/
struct PairingGenerator<Generator: GeneratorType>: GeneratorType {
var generator: Generator
var previous: Generator.Element?
let first: Generator.Element?
@JadenGeller
JadenGeller / Lambda Wrapping.swift
Last active August 29, 2015 14:16
Lambda Wrapping to Guard Evaluation in Swift
// Imagine we have a function that curries
// That is, multiply takes in an integer a
// and returns a fuction that takes in another
// integer b that will return a * b
func multiply(a: Int) -> Int -> Int {
println("I got the first thing")
return { b in
println("I got the second thing")
return a * b
}
@JadenGeller
JadenGeller / Swift Y Function.swift
Last active December 30, 2018 09:31
Swift Y Function
// We define a Y function such that we can use recursion within an anonymous closure
// Note that this function Y is not a combinator because it recursively refers to itself
// There's not much reason to have an actual combinator in Swift, so we're not going to
// worry about it, it's just as useful.
/*
* Takes as input a function that takes in a function and returns a function of the same type
* and returns a function of that return type by feeding that function back into itself
* infinite times. Note that we use a closure to guard looping due to applicative order evaluation.