Skip to content

Instantly share code, notes, and snippets.

Jaden Geller JadenGeller

Block or report user

Report or block JadenGeller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JadenGeller
JadenGeller / Finite.swift
Last active Aug 29, 2015
Swift Finite Sequence
View Finite.swift
/*
* 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 Jun 9, 2016
An Implementation of Streams in Swift
View Streams.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 Feb 12, 2016
Swift Callback Function Transformation
View Swift Callback.swift
/*
* 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 Nov 7, 2015
Try/Catch Exception Handling in Swift
View Exception Handling.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() }
View Swift Restart Skip Block.swift
// 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() }
View Swift Long Jump Wrapper.swift
// 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)
View Swift Array Remove Matching.swift
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) {
View Swift Ambiguous.swift
// 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!).")
View Swift Unless_When.swift
// 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() }
}
View Swift Atomic Operators.swift
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
You can’t perform that action at this time.