Skip to content

Instantly share code, notes, and snippets.

View kazk's full-sized avatar

Kaz Yoshihara kazk

  • Andela
  • Los Angeles, CA
  • 06:39 (UTC -07:00)
View GitHub Profile
// 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)
}
// ([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()
// 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
@kazk
kazk / fix+memoize.swift
Last active August 29, 2015 14:15
Reimplementation of `memoize` function from "Advanced Swift", using the least fixed point.
/// :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`
@kazk
kazk / zip.swift
Last active August 29, 2015 14:15
Defines zipping functions for types conforming to `SequenceType`.
// 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)
// 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.
// 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) {
// 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 {
///
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)))}
}
// 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.