Skip to content

Instantly share code, notes, and snippets.

View ChrisWhealy's full-sized avatar
🏠
Working from home (funnily enough...)

Chris Whealy ChrisWhealy

🏠
Working from home (funnily enough...)
View GitHub Profile
@ChrisWhealy
ChrisWhealy / makeArrayOn.js
Created November 19, 2019 09:27
Turn an array into an object based on an array element's property name
Array.prototype.makeObjectOn = function(prop) {
return this.reduce((acc, el, idx) => {
acc[el[prop] || idx] = el
return acc
})
}
@ChrisWhealy
ChrisWhealy / partitionWith.js
Last active November 9, 2019 13:32
JavaScript function to partition an array by passing each element through a predicate function
/***********************************************************************************************************************
* Partition an array into two arrays based on passing each element to a predicate function.
*
* This function is designed to extend the Array prototype, but only by the explicit action of the consuming library.
* E.G. Assuming the function below belongs to a "utils" module, you must write something like the following:
*
* const utils = require('./utils.js')
* Array.prototype.partitionWith = utils.partitionWith
*
* This function must be defined using a traditional "function" declaration instead of an arrow function. If an arrow
@ChrisWhealy
ChrisWhealy / csv_to_obj_array.js
Last active August 13, 2018 22:43
Read a CSV file (with optional header line) and transform it into an object array
const compose = fn1 => fn2 => val => fn2(fn1(val))
// Remove only the start and end double quotes in a string
const stripQuotes = str => str.replace(/(^")|("$)/g,"")
// Remove commas that might occur inside names
const removeCommas = str => str.replace(/,/g,"")
const cleanTxt = compose(stripQuotes)(removeCommas)
-module(beersong).
-export([sing/0]).
-define(LIMIT, 99).
-define(TEMPLATE_0, "~s of beer on the wall, ~s of beer.~nGo to the store and buy some more, 99 bottles of beer on the wall.~n").
-define(TEMPLATE_N, "~s of beer on the wall, ~s of beer.~nTake one down and pass it around, ~s of beer on the wall.~n~n").
print_verse(0) -> io:format(?TEMPLATE_0, phrase(0));
print_verse(Bottle) -> io:format(?TEMPLATE_N, phrase(Bottle)).
@ChrisWhealy
ChrisWhealy / mindshift19_11.js
Created April 29, 2016 17:13
Mindshift: Part 19, sample 11
div_result(DIV(EIGHT)(FIVE)); // -> {isPos: true, quot : 1, rem : 3}
div_result(DIV(MINUS_EIGHT)(FIVE)); // -> {isPos: false, quot : 1, rem : 3}
div_result(DIV(EIGHT)(MINUS_FIVE)); // -> {isPos: false, quot : 1, rem : 3}
div_result(DIV(MINUS_EIGHT)(MINUS_FIVE)); // -> {isPos: true, quot : 1, rem : 3}
@ChrisWhealy
ChrisWhealy / mindshift19_10.js
Created April 29, 2016 17:12
Mindshift: Part 19, sample 10
// Helper functions to reify the NUMBER function returned by DIV
var magnitude_quot = numFn => magnitude(HEAD(TAIL(numFn)));
var magnitude_rem = numFn => magnitude(TAIL(TAIL(numFn)));
var div_result = numFn =>
({ isPos : sign(numFn),
quot : magnitude_quot(numFn),
rem : magnitude_rem(numFn)
});
@ChrisWhealy
ChrisWhealy / mindshift19_09.js
Created April 29, 2016 17:11
Mindshift: Part 19, sample 9
var DIV = dividend => divisor =>
NUMBER(NOT(XOR(SIGN(dividend))
(SIGN(divisor))))
(Y(do_div_magnitudes)
(ZERO_MAGNITUDE)
(PAIR(ABS(dividend))(ABS(divisor))));
@ChrisWhealy
ChrisWhealy / mindshift19_08.js
Created April 29, 2016 17:10
Mindshift: Part 19, sample 8
var DIV = val1 => val2 => Y(do_div)(ZERO)(PAIR(val1)(val2));
@ChrisWhealy
ChrisWhealy / mindshift19_07.js
Created April 29, 2016 17:09
Mindshift: Part 19, sample 7
var IS_MAGNITUDE_LTE =
qtyFn1 =>
qtyFn2 =>
IS_ZERO_MAGNITUDE(SUBTRACT_MAGNITUDES(qtyFn1)(qtyFn2));
var do_div_magnitudes =
div_next =>
count =>
pair =>
// Inner function
@ChrisWhealy
ChrisWhealy / mindshift19_06.js
Created April 29, 2016 17:09
Mindshift: Part 19, sample 6
var DIV = val1 => val2 => Y(do_div)(ZERO)(PAIR(val1)(val2));