This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!).") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |