Skip to content

Instantly share code, notes, and snippets.

@webstrand
webstrand / gist:46fb441e52a8663bced0
Last active August 29, 2015 14:17
Simple Rust Reverse Proxy
#![feature(io)]
#![feature(net)]
#![feature(core)]
extern crate hyper;
extern crate url;
static HOST: &'static str = "www.google.com";
macro_rules! ret_err(
($e:expr) => {{
@webstrand
webstrand / stream.js
Created August 5, 2016 14:56
Prototype pub-sub library
// Module streams
//
// This is effectively a pub-sub system with a few extra convenience methods.
// Fundamentally, observers are attached to a stream or property, and are called
// when the stream or property emits an event.
//
// By and large, most of the effort in this module is devoted to creating
// optimized streams and properties for passing around an arbitrary amount
// argument variables rather than wrapping up multi-part events into arrays.
//
@webstrand
webstrand / selectFields.ts
Created January 26, 2019 03:40
Guide on how to extract values from a map using keys from an array, but type-safely
type Thing<T> = {
thing: T;
}
const propertyTableFields = {
a: { thing: "A" },
b: { thing: 5 },
c: { thing: "C" },
};
@webstrand
webstrand / gist:0a7be6a60c01c5e619b49fa430203aa6
Created February 12, 2019 16:37
Typescript: An array of keys of some object extending some type.
type KeyofSubset<T, U> = Array<({ [P in keyof T]: T[P] extends U ? P : never })[keyof T]>;
@webstrand
webstrand / TypeFromSpec.ts
Created March 21, 2019 17:11
Derive a concrete type from a schematic type
type Spec = Atom | Dict | List;
type Atom = "bool" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "float32" | "float64" | "string" | "varuint" | "varint" | "buffer"
interface Dict { [key: string]: Spec }
interface List extends Array<Spec> { }
type TypeFromSpec<S extends Spec> = CondTypeFromAtom<S> | CondTypeFromDict<S> | CondTypeFromList<S>;
type TypeFromAtom<S extends Atom> = string;
type TypeFromDict<S extends Dict> = { [P in keyof S]: TypeFromSpec<S[P]> }
type TypeFromList<S extends List> = { [P in keyof S]: TypeFromSpec<S[P] extends Spec ? S[P] : never> }
type CondTypeFromAtom<S> = S extends Atom ? TypeFromAtom<S> : never;
@webstrand
webstrand / type-subtraction.ts
Last active April 5, 2019 14:17
Demonstration of Excluding<T> which emulates the subtractive type `object - T`
// Since typescript nominally types string based enums, we emulate
// the subtractive type `object - T` by making all the properties
// T optional and setting their types to a non-exported enum.
//
// The enum name is long and descriptive to help with debugging.
const enum CannotAssignToPropertyFromExcludedObject { ϕ = "ϕ" }
type Excluding<T extends object> = { [P in keyof T]?: CannotAssignToPropertyFromExcludedObject; } & { [prop: string]: any; }
// Note: If some overlap is acceptable, providing that the overlapping property extends T[P], use
// type Excluding<T extends object> = Partial<T> & { [prop: string]: any };
@webstrand
webstrand / dsteam.pl
Created February 28, 2016 16:07
Find and run Steam games from dmenu
#!/usr/bin/env perl
# Copyright (c) 2016
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
@webstrand
webstrand / util-lists.ts
Last active May 12, 2019 19:44
Utility types for manipulating and traversing lists.
// Find the first item of a list.
export type Head<List extends any[]> = List['length'] extends 0 ? never : List[0];
// Find the last item of a list.
export type Last<List extends any[]> = number extends List['length'] ? List[1e+309] : _Last<List>;
type _Last<
Tuple extends any[],
_I extends number = Tail<Tuple>['length']
> = Tuple extends [] ? never : _I extends keyof Tuple ? Tuple[_I] : never;
@webstrand
webstrand / mapping.ts
Last active May 26, 2019 15:52
Update key/value mapping from changes on another key/value mapping.
import {Multimap, ReadonlyMultimap} from "./multimap";
export type NoInfer<T> = T & { [K in keyof T]: T[K] };
function identityProjection<T>(value: any, primary: T) { return primary }
/**
* Project some sequence of values onto a Map using keys generated by
* {@link surrogate}. Values with the same surrogate key will overwrite, with
* the later insertion order overwriting the earlier.
@webstrand
webstrand / runtype.ts
Created May 21, 2019 18:33
Runtime type validation for typescript
/**
* A TypeScript type compatible runtime type-verification system.
*
* Each instance of {@link Runtype} is a complete type-verification system that
* can prove that an _unknown_ type is actually _known_ type. Runtypes can be
* composed via the convenience constructors {@link Union},
* {@link Intersection}, {@link Tuple}, and {@link struct}. Additionally, when
* constructing a new {@link Runtype}, a parent constraint may be specified in
* {@link RuntypeConfig}.
*