Skip to content

Instantly share code, notes, and snippets.

@g-k
g-k / coffeescript-unexpected-math.md
Last active October 12, 2015 07:18
Coffeescript - Unexpected MATH

Trying to test for spaces in a string using a regex. Ran into unexpected math in an if block. Nice to see that the coffeescript lexer is annotated though: http://coffeescript.org/documentation/docs/lexer.html#section-14

» coffee --version
CoffeeScript version 1.4.0
» coffee
coffee> / /
Error: In repl, Parse error on line 1: Unexpected 'MATH'
    at Object.parseError (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:477:11)
    at Object.parse (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:554:22)
@g-k
g-k / factor-dance.js
Last active October 13, 2015 05:17
Animation of Jason Davies' d3.js implementation of Brent Yorgey's factorization diagrams
// http://www.jasondavies.com/factorisation-diagrams/
var primeFactors = function (n) {
var factors = [],
f;
while (n > 1) {
factors.push(f = factor(n));
n /= f;
}
return factors;
@g-k
g-k / Makefile
Created December 26, 2012 18:18
fizzbuzz in GNU make
include gmsl-1.1.2/gmsl
# ^ Thank you Mr. Make
three := $(call int_encode,3)
five := $(call int_encode,5)
start := $(call int_encode,1)
stop := $(call int_encode,100)
@g-k
g-k / shopping-notes-and-book-review.md
Last active December 10, 2015 14:38
Learning GDB Notes

Shopping Notes and Book Review

Note: Purely Hypothetical

Purchasing

Not too long ago I went to a bookstore to buy a book:

@g-k
g-k / shuffle.js
Created January 30, 2013 04:49
Fisher-Yates or Knuth Shuffle
var swap = function (list, i, j) {
// swap items at i-th and j-th entries of list
// console.log(swap(items, 0, 2)); == [3, 2, 1, 4, 5]
var tmp = list[i];
list[i] = list[j];
list[j] = tmp;
return list;
};
var randomInt = function(max) {
@g-k
g-k / number-valueOf.js
Last active December 14, 2015 04:19
Mask Boxed JS number with valueOf
// Boxed JS number with valueOf method can mask its value.
> three = new Number(3); // Box 3 as a Number object
Number {}
> three.valueOf = function () { return 1; };
function () { return 1; }
> 2 + three
3
> 2 + 3
5
> 2 + +three.toString()
@g-k
g-k / index.html
Created March 6, 2013 02:47
Old server-sent event hello world timer test.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script>
var source = new EventSource('/events');
source.onmessage = function(e) {
document.body.innerHTML += e.data + '<br>';
@g-k
g-k / js-new-this.md
Last active December 15, 2015 20:09
JS new this
var A = function () {
  console.log('calling A with this:', this);
  this.name = 'A';
  return new this;
};
var B = function () {
  console.log('calling B with this:', this);
  this.name = 'B';
};
@g-k
g-k / vega-notes.md
Created April 6, 2013 05:15
Notes on initial vega.js usage

Vega provides a layer on top of d3.js for describing visualizations with a JSON spec.

It makes it easier to try new visualizations like a chloropleth map or small multiples, frees co-workers from needing to see terrible things I've done with d3 and JS, and provides a canvas renderer for easy backwards compatibility on legacy browsers (note: canvas is the default; to use SVG if it's available do something like: chart({ ..., renderer: (Modernizr.svg ? 'svg' : 'canvas') })).

A couple things caught me off-guard:

  1. The tutorial covers the visualization spec. Getting started directions are on the runtime wiki page. I missed the part where it said "Runtime - Deploying and using the browser-based Vega runtime."

  2. Axes labels are a separate text mark?

@g-k
g-k / mdfind.md
Created April 17, 2013 02:26
mdfind util for osx notes

Recently an mdworker process was pegging my CPU and cooking my lap.

From its man page description: "mdworker is the metadata server worker process. It is used by mds to scan and index files as a volume is mounted or a file changes."

What's mds? "mds is the metadata server. It serves all clients of the metadata APIs, including Spotlight."

mdfind uses the builtin metadata index and can be a faster alternative to recursive grep or find and an easy way to search all the files on a mac (assuming the index is up to date):

$ find . -name '*.js'