Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / Finite.swift
Last active August 29, 2015 14:16
Swift Finite Sequence
/*
* Allows you to limit the count of a sequence
*/
extension SequenceType {
func take(count: Int) -> FiniteSequence<Generator.Element> {
return FiniteSequence(backing: AnySequence(self), count: count)
}
}
@JadenGeller
JadenGeller / Streams.swift
Last active June 9, 2016 03:09
An Implementation of Streams in Swift
// We're going to show off some examples first--implementation later!
// Generates a stream of integers starting at n
func IntegersFrom<I : IntegerType>(n: I) -> Stream<I> {
return Stream(head: n, tail: IntegersFrom(n + 1))
}
// One potential implementation of a fibonacci stream; initial input (1,1)
func MakeFibonacci(a: Int, b: Int) -> Stream<Int> {
return Stream<Int>(head: a, tail: MakeFibonacci(b, a + b))
@JadenGeller
JadenGeller / Swift Callback.swift
Last active February 12, 2016 13:37
Swift Callback Function Transformation
/*
* Takes in a function that transform T to U and
* a callback that takes arguments of type T and U
* and returns a function that performs f but calls
* the callback before returning
*/
func callback<T,U>(f: T -> U, c: (T, U) -> ()) -> T -> U{
return { x in
let r = f(x)
c(x,r)
@JadenGeller
JadenGeller / Exception Handling.swift
Last active November 7, 2015 14:53
Try/Catch Exception Handling in Swift
// A very basic implementation of try/catch blocks in Swift
// Note that this is not thread safe
// Also note that this does not support nested try/catch statements
// We will start with an example
func test(x: Bool) {
try{
println("HI")
if x { throw() }
@JadenGeller
JadenGeller / Swift Restart Skip Block.swift
Last active August 29, 2015 14:16
Swift Restart Skip Block
// Let's start with an example
var c = 0
// Inside a jump block, calling the restart function starts over
// and calling the skip function exists the block
jump { restart, skip in
for i in 1...4 {
println("c: \(c); i: \(i)")
if c == 4 { skip() }
else if i == 3 { c++; restart() }
@JadenGeller
JadenGeller / Swift Long Jump Wrapper.swift
Last active August 29, 2015 14:16
Swift Long Jump Wrapper
// We will show how long jumps can be used within Swift
// Create jump and set it's position
let start = Jump()
setjmp(start.line) // Sets start's line to the current line
println("HI") // -> "Hi" -> "Hi" -> "Hi" -> ... (prints forever)
longjmp(start.line, 0) // Jumps back to start's line, e.g. setjmp(start.line)
@JadenGeller
JadenGeller / Swift Array Remove Matching.swift
Last active August 29, 2015 14:16
Swift Array Remove Matching
extension Array {
/*
* Similiar to filter, but removes the results that match
* in addition to returning them
*/
mutating func removeMatching(removeElement: T -> Bool) -> [T] {
var removed = [T]()
for index in stride(from: self.endIndex-1, through: 0, by: -1) {
@JadenGeller
JadenGeller / Swift Ambiguous.swift
Last active August 29, 2015 14:16
Swift Ambiguous
// Let's start with an example!
// We specify three anbiguous values a, b, c each which can be any integer [1,7]
let a = Ambiguous(1,2,3,4,5,6,7)
let b = Ambiguous(1,2,3,4,5,6,7)
let c = Ambiguous(1,2,3,4,5,6,7)
// We try to satisfy on a,b,c pythagorean's theorem, and we print the side lengths is some values a,b,c satisfy
if satisfy(a,b,c)({ (c.choice * c.choice == a.choice * a.choice + b.choice * b.choice) && (a.choice > b.choice)}) {
println("We can have a triangle with sides lengths \(a.bound!), \(b.bound!), \(c.bound!).")
@JadenGeller
JadenGeller / Swift Unless_When.swift
Last active May 21, 2019 16:11
Swift Unless/When
// Basically, these are if and else statements respectively
// without the opposite clause
func when(test: @autoclosure () -> Bool, action: () -> ()) {
if test() { action() }
}
func unless(test: @autoclosure () -> Bool, action: () -> ()) {
if !test() { action() }
}
@JadenGeller
JadenGeller / Swift Atomic Operators.swift
Created March 17, 2015 01:38
Swift Atomic Operators
import Foundation
infix operator +=! { associativity right precedence 90 } // Int32 or Int64
infix operator -=! { associativity right precedence 90 } // Int32 or Int64
postfix operator ++! {} // Int32 or Int64
postfix operator --! {} // Int32 or Int64
infix operator |=! { associativity right precedence 90 } // UInt32
infix operator &=! { associativity right precedence 90 } // UInt32