Skip to content

Instantly share code, notes, and snippets.

@cmstead
cmstead / memoized-lazy-id.js
Created October 11, 2017 06:45
Lazy ID with memoization
const nullFn = (fn) => typeof fn === 'function' ? fn : () => null;
function dict(key, value, struct) {
return (function (localStruct) {
return (ref) => ref === key ? value : localStruct(ref)
})(nullFn(struct));
}
function memoize(fn) {
let store = null;
@cmstead
cmstead / multiply-then-divide-composition.js
Created October 1, 2017 20:46
MultiplyThenDivide composition
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;
const multiplyThenDivide = (a, b, c) => divide(multiply(a, b), c);
@cmstead
cmstead / quickspec-multiplyThenDivide.js
Last active October 1, 2017 19:56
Example of testing composition of multiply and divide with Quickspec
describe('runMultiplyThenDivide', function() {
it('should properly evaluate the composition of multiply and divide', function () {
const specSet = [
{
name: 'Zero-multiplication',
setupValues: { a: 0, b: 1, c: 2 },
expectedValue: 0
},
{
@cmstead
cmstead / Tests for multiplyThenDivide
Last active October 1, 2017 19:53
Example of testing composite multiplyThenDivide, many cases
describe('multiplyThenDivide', function() {
it('should return 0 when multiplying by 0, dividing by 2', function() {
assert.equal(multiplyThenDivide(0, 1, 2), 0);
});
it('should return half of value when multiplying by 5, dividing by 2', function() {
assert.equal(multiplyThenDivide(1, 5, 2), 2.5);
});
@cmstead
cmstead / testSnippets.json
Last active May 3, 2017 16:03
My test snippets for VS Code
{
"Mocha describe": {
"prefix": "mdesc",
"body": [
"describe('$1', function () {",
"\t$2",
"});"
],
@cmstead
cmstead / extended-simple-ui-test-runner.js
Last active February 7, 2017 06:48
Extended test runner for UI tests
(function (testRunner) {
'use strict';
function logAndTestUi(sample, expectedMean, expectedStandardDeviation) {
var message = 'Testing -- ' +
'sample: ' + JSON.stringify(sample) + '; ' +
'expected mean: ' + expectedMean + '; ' +
'expected standard deviation: ' + expectedStandardDeviation;
console.log(message);
@cmstead
cmstead / simple-ui-test-runner.js
Last active February 7, 2017 06:45
Simple UI test runner for stats example program
var testRunner = (function () {
'use strict';
function isMatch (expectedValue, actualValue) {
var equalMatch = expectedValue === actualValue;
var NaNMatch = Number.isNaN(expectedValue) && Number.isNaN(actualValue);
return equalMatch || NaNMatch;
}
@cmstead
cmstead / stats-computer.html
Last active February 7, 2017 06:47
User view for simple stats API example
<html>
<body>
<input type="text" value="" name="sample" id="sample" />
<button id="compute">Compute Mean and Standard Deviation</button>
<h4>Mean:</h4>
<p id="mean">0</p>
<h4>Standard Deviation:</h4>
@cmstead
cmstead / view-helper.js
Created February 5, 2017 21:12
Simple helper for Stats API example code
var viewHelper = (function () {
'use strict';
function parseNumberValues (valueStr) {
return valueStr.trim().split(/[\s\,]+/).map(parseFloat);
}
return {
parseNumberValues: parseNumberValues
};
@cmstead
cmstead / stats-api.js
Created February 5, 2017 21:11
Simple stats API example
var statsApi = (function () {
'use strict';
var square = pow(2);
var squareRoot = pow(0.5);
function getStandardDeviation (values) {
var xBar = getMean(values);
var computedValues = values.map(subtractMeanAndSquare(xBar));
return squareRoot(getMean(computedValues));