Skip to content

Instantly share code, notes, and snippets.

View kazk's full-sized avatar

Kaz Yoshihara kazk

  • Andela
  • Los Angeles, CA
  • 00:10 (UTC -07:00)
View GitHub Profile
@kazk
kazk / kube_test_k3d.rs
Last active June 11, 2021 23:05
Test helper to create a temporary k3d cluster for kube-rs
use std::{convert::TryFrom, process::Command};
use kube::{config::Kubeconfig, Client, Config};
#[derive(Debug, Default)]
pub struct TestEnv {
// Unique name.
name: String,
// Kubeconfig of the temporary cluster.
kubeconfig: Kubeconfig,
@kazk
kazk / smoothsort.swift
Last active March 26, 2017 15:52
Smoothsort in Swift
// An implementation of [Smoothsort] algorithm invented by Edsger Dijkstra,
// which I didn't get until reading [Smoothsort Demystified] by Keith Schwarz.
//
// Some optimizations like the chained swaps and corner case elimination were
// derived from [smoothsort.c] by Martin Knoblauch Revuelta.
public func smoothsort<T : Comparable>(inout a: [T]) {
smoothsort(&a) { $0 < $1 }
}
public func smoothsort<T : Comparable>(inout a: [T], range: Range<Int>) {
// Vladimir Yaroslavskiy's Dual-pivot quick sort.
// Optimization based on [Dart implementation].
public func dualPivotQuickSort<T : Comparable>(inout a: [T]) {
dualPivotQuickSort(&a) { $0 < $1 }
}
public func dualPivotQuickSort<T>(inout a: [T], isOrderedBefore: (T, T)->Bool) {
dualPivotQuickSort(&a, indices(a), isOrderedBefore)
}
/// Sorts 5 inputs by 7 comparisons.
/// Uses Minimum-Comparison Sorting Algorithm (H. B. Demuth, 1956).
func sort5<T : Comparable>
(inout a: T, inout b: T, inout c: T, inout d: T, inout e: T)
{
// 1. Setup conditions. (3 comparisons)
// b → d
// ↑ ↑ a < b < d, c < d
// a c e
if b < a { (a, b) = (b, a) } // a < b
import Darwin
// import Darwin.Mach.mach_time // (mach_timebase_info, mach_absolute_time)
// import Darwin.Mach.clock_types // (NSEC_PER_*)
public func timeBlockNSec(block: ()->()) -> Double { // in nanoseconds
var info = mach_timebase_info(numer: 0, denom: 0)
let kr = mach_timebase_info(&info)
if kr != KERN_SUCCESS {
fatalError("Initialization of `mach_timebase_info` failed with code \(kr).")
}
if info.denom == 0 {
// 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.
///
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: - 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 {
// 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: - 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.