This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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: - Curry | |
/// Currying for functions with 2 arguments. | |
/// | |
/// :param: fn A function with 2 arguments, (A,B)→R | |
/// :returns: A curried function (A)→(B)→R | |
public func curry<A,B,R>(fn:(A,B)->R) -> (A)->(B)->R { | |
return {a in {b in fn(a,b)}} | |
} | |
/// Currying for functions with 3 arguments. |
This file contains hidden or 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: - Adjoin | |
public func adjoin<T,A,B>(a:(T)->A, b:(T)->B) -> (T)->(A,B) { | |
return {(x) in (a(x), b(x))} | |
} | |
public func adjoin<T,A,B,C>(a:(T)->A, b:(T)->B, c:(T)->C) -> (T)->(A,B,C) { | |
return {(x) in (a(x), b(x), c(x))} | |
} | |
public func adjoin<T,A,B,C,D>(a:(T)->A, b:(T)->B, c:(T)->C, d:(T)->D) -> (T)->(A,B,C,D) { |
This file contains hidden or 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: - Compose | |
public func compose<T,A,B>(a:(B)->A, b:(T)->B) -> (T)->A { | |
return {(x) in a(b(x))} | |
} | |
public func compose<T,A,B,C>(a:(B)->A, b:(C)->B, c:(T)->C) -> (T)->A { | |
return {(x) in a(b(c(x)))} | |
} | |
public func compose<T,A,B,C,D>(a:(B)->A, b:(C)->B, c:(D)->C, d:(T)->D) -> (T)->A { |
This file contains hidden or 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
/// | |
public func pipe<A,B,R>(a:(A)->B, b:(B)->R) -> (A)->R { | |
return {(x) in b(a(x))} | |
} | |
/// | |
public func pipe<A,B,C,R>(a:(A)->B, b:(B)->C, c:(C)->R) -> (A)->R { | |
return {(x) in c(b(a(x)))} | |
} |
This file contains hidden or 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: - Flip | |
/// Flips the order of function arguments. | |
/// | |
/// :param: fn Function (A,B)→R | |
/// :returns: Function (B,A)→R | |
public func flip<A,B,R>(fn:(A,B)->R) -> (B,A)->R { | |
return {(b,a) in fn(a,b)} | |
} | |
/// Flips the order of function arguments. |