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
// Defines zipping functions for types conforming to `SequenceType`. | |
// - zip (overloaded up to zip7) | |
// - unzip (overloaded up to unzip7) | |
// - zipLongest (overloaded up to zipLongest4) | |
// | |
// References: | |
// - [Convolution (computer science)]: http://en.wikipedia.org/wiki/Convolution_(computer_science) | |
// MARK: - zip (shortest) |
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
/// :param: f A function which takes a function, and returns a function with same signature. | |
/// The returned function can be made recursive by calling the given function. | |
/// | |
/// :returns: A recursive function. The least fixed point of the function returned by `f`. | |
public func fix<T, R>(f: ((T)->R) -> ((T)->R)) -> (T)->R { | |
return { f(fix(f))($0) } | |
} | |
// Reimplementation of `memoize` from [Advanced Swift], using `fix` |
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
// MARK: unfoldr :: (T, (T)->(U, T)?) -> [U] | |
// http://hackage.haskell.org/package/base-4.7.0.2/docs/Data-List.html#v:unfoldr | |
public func unfoldr<T, U>(var seed: T, f: (T)->(U, T)?) -> SequenceOf<U> { | |
return SequenceOf {_ -> GeneratorOf<U> in | |
return GeneratorOf { | |
if let (a, b) = f(seed) { | |
seed = b | |
return a | |
} | |
return nil |
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
// ([T], (T)->[U]) -> [U] | |
public func flatMap<S : SequenceType, R : SequenceType> | |
(sequence: S, f: (S.Generator.Element)->R) | |
-> SequenceOf<R.Generator.Element> | |
{ | |
return SequenceOf {_ -> GeneratorOf<R.Generator.Element> in | |
var g = sequence.generate() | |
var ss = GeneratorOf<R> { g.next().map(f) } | |
var rG = ss.next()?.generate() | |
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
// Equality operator for Tuples with elements conforming to `Equatable`. (Up to 6) | |
func ==<A : Equatable, B : Equatable>(lhs: (A, B), rhs: (A, B)) -> Bool { | |
return (lhs.0 == rhs.0) && (lhs.1 == rhs.1) | |
} | |
func !=<A : Equatable, B : Equatable>(lhs: (A, B), rhs: (A, B)) -> Bool { | |
return !(lhs == rhs) | |
} | |
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 | |
private let DefaultChunkSize: Int = 256 | |
private let DefaultEndOfLine: String = "\n" | |
/// Class for reading file line by line. | |
/// | |
/// Based on DDFileReader by Dave DeLong. | |
/// [How to read data from NSFileHandle line by line?](http://stackoverflow.com/a/3711079/666371) | |
public class FileReader { |
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 Darwin | |
// MARK: - MathFunctionsType - | |
/// Type supporting functions defined in <math.h> | |
public protocol MathFunctionsType { | |
// MARK: Basic operations | |
/// Computes absolute value of a floating-point value (|x|) | |
class func fabs(x: Self) -> Self | |
/// Computes remainder of the floating-point division operation |
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
// MARK: unwrapped | |
public func unwrapped<A, B>(a: A?, b: B?) -> (A, B)? { | |
switch (a, b) { | |
case let (.Some(a), .Some(b)): return (a, b) | |
default: return nil | |
} | |
} | |
public func unwrapped<A, B, C>(a: A?, b: B?, c: C?) -> (A, B, C)? { | |
switch (a, b, c) { |
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
/// memoize function from Advanced Swift. | |
/// https://developer.apple.com/videos/wwdc/2014/?id=404 | |
/// | |
/// let fib = memoize {(fib:(Int)->Int, x:Int) in (x <= 1) ? 1 : fib(x - 1) + fib(x - 2)} | |
/// | |
/// let 𝜑 = Double(fib(45)) / Double(fib(44)) // ≅ 1.61803398874989... | |
public func memoize<A : Hashable, R>(body: ((A)->R, A)->R) -> (A)->R { | |
var memo:[A: R] = [:] | |
var memoized: ((A)->R)! | |
memoized = {(a) in |
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
// Creates a sequence that consists of a single element repeated n times, | |
// or an infinite sequence repeating that element indefinitely. | |
// [D std.range.repeat]: http://dlang.org/phobos/std_range.html#repeat | |
public func repeat<T>(x: T) -> SequenceOf<T> { | |
return SequenceOf {_ -> GeneratorOf<T> in GeneratorOf { x }} | |
} | |
public func repeat<T>(x: T, count: Int) -> SequenceOf<T> { | |
return SequenceOf {_ -> GeneratorOf<T> in | |
var i = 0 |