Skip to content

Instantly share code, notes, and snippets.

@kourge
kourge / unit.ts
Created January 19, 2017 02:45
Type-safe units in TypeScript
namespace TypeBranding {
// This incurs no runtime cost. It is a purely compile-time construct.
type Meters = number & { __metersBrand: any };
type Miles = number & { __milesBrand: any };
function Meters(i: number): Meters { return i as Meters; }
function Miles(i: number): Miles { return i as Miles; }
let a: Meters;
@kourge
kourge / fsa.ts
Last active September 15, 2016 22:21
Type Safe FSAs
export interface Meta<T> {
meta: T;
}
export type AnyType = string | symbol;
export interface Action<Type extends AnyType, Payload> {
type: Type;
payload: Payload;
error?: boolean;
@kourge
kourge / davvero.sh
Created July 22, 2016 18:18
wraps around `test` and echoes "true" or "false" depending on exit status
#!/bin/sh
test "$@"
RESULT=$?
if [[ $RESULT -eq 0 ]]; then
echo true
else
echo false
fi
func chunk<T : Sliceable where T.Index : Strideable>(
s: T,
#from: T.Index,
#to: T.Index,
#by: T.Index.Stride
) -> GeneratorOf<T.SubSlice> {
var g = stride(from: from, to: to, by: by).generate()
return GeneratorOf<T.SubSlice> {
if let start = g.next() {
enum Quadrant {
case BottomLeft
case TopLeft
case BottomRight
case TopRight
static func forPoint(point: CGPoint) -> Quadrant {
switch (point.x, point.y) {
case (let x, let y) where x >= 0 && y >= 0:
return .TopRight
@kourge
kourge / gist:38c1d3a07a5dee9407c0
Created June 4, 2014 21:19
Fun with double arrow / hash rocket in Swift
operator infix => { associativity left }
func => <K : Hashable, V>(key: K, value: V) -> (K, V) {
return (key, value)
}
let pairs: (String, Int)[] = [
"e" => 10,
"t" => 7,
"i" => 2
]
operator prefix ~ {}
@prefix func ~(pattern: String) -> NSRegularExpression! {
var error: NSError?
return NSRegularExpression.regularExpressionWithPattern(
pattern,
options: nil,
error: &error)
}
@kourge
kourge / context_manager.php
Last active April 15, 2025 03:40
A simple user-space PHP `with` implementation that mimics Python context managers.
<?php
class Context {
private $args = array();
private $func = NULL;
public function __construct() {
$args = func_get_args();
$func = array_pop($args);
@kourge
kourge / es6-generators-stopiteration.js
Created June 6, 2013 17:59
The ES6 spec states that calling the send method on a generator should result in an object. So long as the generator is yielding more values, the object is { value: v, done: false } where v is the value yielded. When the generator stops, the object is { value: undefined, done: true }. Firefox has had a preliminary implementation of generators th…
this.Generator && (function(g, global) {
var __send = g.prototype.send;
g.prototype.send = function send(value) {
var result = __send.call(this, value);
if (result.done) {
throw new StopIteration();
}
return result.value;
};
#!/usr/bin/env macruby
framework 'CoreFoundation'
class Range
def to_core_range
location = self.begin
length = (self.exclude_end? ? self.end : self.end.succ) - location
CFRange.new(location, length)
end