Skip to content

Instantly share code, notes, and snippets.

@bradparker
bradparker / circle.js
Last active January 3, 2016 13:29
simple x and y cords for a circle
function circle (radius, x, y, nPoints) {
var circle = [];
var nPoints = nPoints || 360;
var wedge = 2 * Math.PI / nPoints;
for (var angle = wedge; angle < 2 * Math.PI; angle += wedge) {
circle.push({
x: x + radius * Math.cos(angle),
y: y + radius * Math.sin(angle)
});
const range = (start, end) =>
Array.from({
length: end - start
}).map((_, index) => (index + start))
const sum = nums =>
nums.reduce((total, num) => total + num)
sum(range(20, 100))
@bradparker
bradparker / collection-compare.js
Created September 12, 2014 00:28
Collection comparison helpers
function allInCollection (collection) {
return function (subCollection) {
return subCollection.every(function (subItem) {
return collection.some(function (item) {
return item.id === subItem.id;
});
});
};
}
@bradparker
bradparker / tuning-fork.js
Created September 21, 2014 04:59
Tuning Fork
var context = new AudioContext(),
gainNode = context.createGain(),
oscillator = context.createOscillator();
gainNode.connect(context.destination);
oscillator.frequency.value = 440;
oscillator.connect(gainNode);
oscillator.start();
// ..
// gainNode.gain.value = 0;
@bradparker
bradparker / anthropology
Last active August 29, 2015 14:06
Anthropology - Charlie Parker
|-------------------------|-------------------------|-------------------------|-------------------------|
|-------------------------|-------------------------|-------------------------|-------------------------|
|----7--------8--6--7--10-|----------8-----10-8-----|----7--8--7-----------7--|-------------------------|
|-8-----10-9--------------|-------------------------|-------------10-7--8-----|-10----8--7-----------8--|
|-------------------------|-------------------------|-------------------------|----10-------------------|
|-------------------------|-------------------------|-------------------------|-------------------------|
| + - + - + - + - | + - + - + - + - | + - + - + - + - | + - + - + - + - |
|-------------------------|-------------------------|-------------------------|-------------------------|
|-------------------------|-------------------------|----------------9-----7--|-8-----------------------|
@bradparker
bradparker / unit-matcher.js
Created November 20, 2014 22:32
Unit Matcher
// Take "3mm", "-3inches", or "3.45 cm" and return ['3', 'mm'], ['-3', 'inches'], ["3.45", "cm"] etc
function unitParse (str) {
return (str.match(/([-\d.]+)\s+?([A-Za-z]+)/) || []).slice(1)
}
@bradparker
bradparker / line-slope-intercept.js
Created December 31, 2014 03:33
Line, slope intercept form
function Line (slope, intercept) {
this.slope = slope
this.intercept = intercept
}
function getX (y) {
y = y || 0
return (y - this.intercept) / this.slope
}
Line.prototype.getX = getX;
@bradparker
bradparker / cons.js
Last active August 29, 2015 14:20
Cons!
const cons = (a, b) =>
(member) => {
if (member === 1) {
return a
} else if (member === 2) {
return b
}
}
const car = (pair) =>
@bradparker
bradparker / alternative.hs
Last active May 25, 2018 04:28
Haskell Buzz
module Main where
import Control.Applicative
import Data.Monoid
fizz n
| n `mod` 3 == 0 = Just "Fizz"
| otherwise = Nothing
buzz n
@bradparker
bradparker / index.html
Created August 30, 2015 12:32
JS Scratch Pad
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Scratch pad</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.min.css" />
</head>
<body>
<div id="mocha"></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.2.0/chai.min.js"></script>