Skip to content

Instantly share code, notes, and snippets.

@srikumarks
srikumarks / pitch_class.js
Created December 23, 2015 06:54
Pitch class calculator
var sharpLabels = ["A", "A♯", "B", "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯"];
var flatLabels = ["A", "B♭", "B", "C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭"];
function pitchClass(freq_Hz, labels) {
var cents = Math.round(1200 * Math.log(freq_Hz/440) / Math.log(2));
var semitones = Math.round(cents / 100);
var pclass = (((semitones % 12) + 12) % 12);
var octave = 4 + Math.floor(semitones / 12);
@srikumarks
srikumarks / elm-for-js.md
Last active December 21, 2015 19:29
Elm for Javascripters

Basics

  • Elm code is written as expressions. The end goal of an Elm programm is to specify a main expression which will be a Signal of Html values.
  • Line comments in Elm are marked using -- and are equivalent to // in JS.
  • Block comments in Elm are marked using {- and -} which are equivalent to /* and */ in JS.

Modules

module ModuleName where
@srikumarks
srikumarks / sneaky-leak.js
Created September 21, 2015 19:58
Sneaky memory leak in Javascript
function makeBigObject() {
var big = new Float64Array(10000000); // Allocate big object.
return {
one: function () { return big.length; }, // Has ref to big object.
two: function () { return "hello"; } // Doesn't have ref to big object.
};
}
function leak() {
var l = makeBigObject(); // l has ref to big object via "one".
@srikumarks
srikumarks / startapp-automaton.elm
Created June 19, 2015 07:29
Elm StartApp as Automaton
module StartAppAutomaton where
import Graphics.Element exposing (..)
import Window
import Time
import Automaton as Auto
dt = Time.fps 60
type alias Model = {count : Int}
@srikumarks
srikumarks / elm-virtual-dom-diff-failure.elm
Created May 3, 2015 03:51
Virtual DOM diffing failure when mixing Graphics.Element and Html.
module VirtualDomFailure where
import Window
import Time
import Html exposing (..)
import Html.Attributes exposing (..)
import Graphics.Element exposing (..)
import Signal exposing (..)
googleLogo = "https://www.google.co.in/images/srpr/logo11w.png"
@srikumarks
srikumarks / currying-sum.js
Last active April 11, 2017 14:30
Currying sum hack
// The problem is to write sum() such that it produces the following output -
// console.log(sum(2,3)); // Produces 5
// console.log(sum(2)(3)); // Produces 5
function sum() {
var s = Array.prototype.reduce.call(arguments, function (x, y) { return x + y; }, 0);
var f = function () {
var a = Array.prototype.slice.call(arguments);
a.push(s);
return sum.apply(null, a);
@srikumarks
srikumarks / stddev.js
Last active August 29, 2015 14:09
Standard deviation in js
function stddev(values) {
return Math.sqrt(mean(values.map(square)) - square(mean(values)));
}
function mean(values) {
return values.reduce(add, 0) / values.length;
}
function add(x, y) {
return x + y;
@srikumarks
srikumarks / func_fm.js
Last active August 29, 2015 14:05
Functional frequency modulation ...
// Here is a possible pure-functional implementation of stateful signals.
function makeSound(signal, currState, result, endCondition) {
return endCondition(currState) ? reverse(result, null) : signal(currState, function (nextState, val) {
return makeSound(signal, nextState, pair(val, result), endCondition);
});
}
// A simple implementation of pair
function pair(val, list) {
@srikumarks
srikumarks / rename_sweet.js
Last active August 29, 2015 13:58
A conservative algorithm to minimally rename the sweetjs hygienified variables at file scope.
// Takes a file produced by sweet.js and minimally renames
// all the identifiers that have been hygienified by the macro
// expansion. Run using -
// node /path/to/rename_sweet.js file1.js > fileout.js
var fs = require('fs');
var code = fs.readFileSync(process.argv[2], 'utf8');
var idRE = /[A-Za-z_][A-Za-z0-9_\$]*/g;
@srikumarks
srikumarks / resolveCtx.js
Created March 29, 2014 02:28
sweetjs's resolveCtx rewritten recursively
// (Syntax) -> String
function resolveCtx(originalName, ctx, stop_spine, stop_branch) {
if (ctx instanceof Mark) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
if (ctx instanceof Def) {
if (stop_spine.indexOf(ctx.defctx) !== -1) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
return resolveCtx(originalName, renames(ctx.defctx, ctx.context, originalName), stop_spine, unionEl(stop_branch, ctx.defctx));