Skip to content

Instantly share code, notes, and snippets.

View rbuckton's full-sized avatar

Ron Buckton rbuckton

View GitHub Profile
declare function binarySearch<T>(array: T[], value: T, comparer?: (v1: T, v2: T) => number, offset?: number): number;
declare function insertItemAt<T>(array: T[], offset: number, value: T): void;
declare function orderedRemoveItemAt<T>(array: T[], index: number): void;
declare function lastOrUndefined<T>(array: T[]): T;
interface TextRange { pos: number, end: number }
class IntervalTree<T extends TextRange> {
public count: number = 0;
@rbuckton
rbuckton / cancellation-strawman.md
Last active April 8, 2017 11:09
ECMAScript Cancellation Strawman

Overview

Cancellation follows a source -> sink model and consists of three components: Source, Sink, and Signal.

  • Source - Created by the caller of an asynchronous operation, a Source is a Signal producer.
    • Represented in this proposal as CancellationSource.
  • Sink - Provided by the caller to an asynchronous operation, a Sink is a Signal consumer.
    • A Source and its Sink are entangled.
    • A Sink can only be used to consume or observe a cancellation Signal.
  • Represented in this proposal as a CancellationToken.
var __construct = (this && this.__construct) || (typeof Reflect !== "undefined" && Reflect.construct
? function (self, target, args) { return self !== null && Reflect.construct(target, args, self.constructor) || self; }
: function (self, target, args) { return self !== null && target.apply(self, args) || self; });
var PatchedPromise = (function (_super) {
__extends(PatchedPromise, _super);
function PatchedPromise(executor) {
var _this = this;
_this = __construct(this, _super, [executor]);
return _this;
'use strict';
class MapAsyncIterable<T, R> implements AsyncIterable<R> {
private _source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>;
private _selector: (value: T) => R | PromiseLike<R>;
constructor(source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>, selector: (value: T) => R | PromiseLike<R>) {
this._source = source;
this._selector = selector;
}
@rbuckton
rbuckton / git-checkout-pr
Last active June 1, 2017 23:22
Performs a fetch and checkout of a github pull request.
#!/bin/sh
source "$(git --exec-path)/git-sh-setup"
USAGE="<pull> [<branch>]"
function _pr() {
if [[ -n $(git rev-parse --verify --quiet $2) ]]; then
git checkout $2
else
git fetch origin pull/$1/head:$2
@rbuckton
rbuckton / example.ts
Last active June 15, 2017 22:32 — forked from mattpodwysocki/example.ts
Reimagining events as async iterables
const EventEmitter = require('events').EventEmitter;
const fromEventPattern = require('ix/asynciterable/fromeventpattern').fromEventPattern;
// Get Async Iterable
const e = new EventEmitter();
const ai = fromEventPattern(
h => e.addListener('data', h),
h => e.removeListener('data', h)
);

High-Order Type Relationships for promised T

Definitions

  1. Definition - A type T is promise-Like iff T has a callable then method that accepts a function as its first argument.

  2. Definition - The fulfillment type of T is the type V in T.then((value: V) ⇒> {}).

  3. Definition - The promised type of T (nee. promised T) is the promised type of the fulfillment type of T iff T is promise-like; otherwise, T.

const obj = {
  y: 1,
  method(x) => x + this.y,
  get prop() => this.y,
  set prop(v) => this.y = v
}

class C {
 y = 1;