Skip to content

Instantly share code, notes, and snippets.

View BrendanEich's full-sized avatar

Brendan Eich BrendanEich

View GitHub Profile
let nums = [1, 53, 0, 42, 3, 99, 2, 67, 4];
// Suppose we're in a function here...
let strs = nums.map(function (x) {
switch (x) {
case 1: return x + 'st';
case 2: return x + 'nd';
case 3: return x + 'rd';
default: return x + 'th';
}

From Fabrice Bellard, with minor name change (umulh):

// return the high 32 bit part of the 64 bit addition of (hi0, lo0) and (hi1, lo1)
Math.iaddh(lo0, hi0, lo1, hi1)

// return the high 32 bit part of the 64 bit subtraction of (hi0, lo0) and (hi1, lo1)
Math.isubh(lo0, hi0, lo1, hi1)

// return the high 32 bit part of the signed 64 bit product of the 32 bit numbers a and b
@BrendanEich
BrendanEich / gist:5753666
Created June 11, 2013 00:36
ES6 version of Peter Norvig's Sudoku solver originally written in Python
// XXX should be standard (and named clone, after Java?)
Object.prototype.copy = function () {
let o = {}
for (let i in this)
o[i] = this[i]
return o
}
// Containment testing for arrays and strings that should be coherent with their iterator.
Array.prototype.contains = String.prototype.contains = function (e) {
@BrendanEich
BrendanEich / ta_vs_nt
Created August 17, 2011 23:20
typed array vs. number type
js> b = new ArrayBuffer(64)
({})
js> a = new Uint32Array(b)
({0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 14:0, 15:0})
js> a[0]
0
js> a[0] = Math.pow(2,32)
4294967296
js> a[0]
0
@BrendanEich
BrendanEich / minimalist-classes.js
Created November 1, 2011 22:48 — forked from jashkenas/minimalist-classes.js
less minimalism, richer leather
// A response to jashkenas's fine proposal for minimalist JavaScript classes.
// Harmony always stipulated classes as sugar, so indeed we are keeping current
// JavaScript prototype semantics, and classes would only add a syntactic form
// that can desugar to ES5. This is mostly the same assumption that Jeremy
// chose, but I've stipulated ES5 and used a few accepted ES.next extensions.
// Where I part company is on reusing the object literal. It is not the syntax
// most classy programmers expect, coming from other languages. It has annoying
// and alien overhead, namely colons and commas. For JS community members who