Skip to content

Instantly share code, notes, and snippets.

View kazk's full-sized avatar

Kaz Yoshihara kazk

  • Andela
  • Los Angeles, CA
  • 08:20 (UTC -07:00)
View GitHub Profile
@kazk
kazk / roundRobin.swift
Created December 29, 2014 21:16
`roundRobin` function from D std.range in Swift.
// [roundRobin](http://dlang.org/phobos/std_range.html#roundRobin)
public func roundRobin<S:SequenceType>(sequences:S ...)
-> SequenceOf<S.Generator.Element>
{
func isDone(gs:[S.Generator?]) -> Bool {
for g in gs { if (g != nil) { return false } }
return true
}
return SequenceOf {_ -> GeneratorOf<S.Generator.Element> in
// MARK: - zip (longest)
// Zip sequences continue to the end of the longest sequence.
// Treats as if the shorter sequences have .None elements to fill the gap.
// MARK: ([A], [B]) -> [(A?, B?)]
/// Generalized zip2 longest, zipping with the given function instead of a tupling function.
public func zipLongest<A : SequenceType, B : SequenceType, R>
(sequenceA: A, sequenceB: B, with f: (A.Generator.Element?, B.Generator.Element?)->R)
-> SequenceOf<R>
{
@kazk
kazk / vForceFunctions.swift
Last active August 29, 2015 14:12
Wrappers of functions from vForce.h.
import Accelerate.vecLib.vForce
// Convenience functions wrapping functions from vForce.h.
//
// Function names are orignal name without `vv` prefix with few exceptions listed below.
// - trunc = vvint
// - round = vvnint
// - reciprocal = vvrec
//
// Arguments are adjusted to match standard <math.h> functions when applicable.
// accumulate (scanl)
public func accumulate<S : SequenceType, T>
(sequence: S, initial: T, combine: (T, S.Generator.Element)->T)
-> SequenceOf<T>
{
return SequenceOf {_ -> GeneratorOf<T> in
var g = sequence.generate()
var value:T? = initial
return GeneratorOf {
switch value {
// [D std.range.chain]: http://dlang.org/phobos/std_range.html#chain
/// Concatenates several sequences of same kind into a single one.
public func chain<S : SequenceType>(sequences: S...) -> SequenceOf<S.Generator.Element> {
return SequenceOf {_ -> GeneratorOf<S.Generator.Element> in
var ss = sequences.generate()
var g = ss.next()?.generate()
return GeneratorOf {
if let x = g?.next() { return x }
// Must be `while let` not `if let` to handle empty sequence in between
// 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
/// 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
// 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) {
@kazk
kazk / MathFunctionsType.swift
Created January 29, 2015 11:39
Type supporting functions defined in <math.h>
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
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 {