Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
@stefanfrede
stefanfrede / left_variadic_decorator_recipe.js
Created June 12, 2016 11:23
leftVariadic is a left-variadic function which has one or more fixed arguments, and gathers the rest into the leftmost argument.
/**
* Gathering arguments from the right is easy in ES6 with the spread operator
*/
const abccc = (a, b, ...c) => {
console.log(a);
console.log(b);
console.log(c);
};
abccc(1, 2, 3, 4, 5)
@stefanfrede
stefanfrede / left_gather_decorator_recipe.js
Created June 10, 2016 05:35
leftGather gathers excess arguments into it from the left.
/**
* Gathering arguments from the right is easy in ES6 with the spread operator
*/
const [first, ...butFirst] = ['why', 'hello', 'there', 'little', 'droid'];
first
//=> 'why'
butFirst
//=> ["hello","there","little","droid"]
@stefanfrede
stefanfrede / once_combinator_recipe.js
Last active June 10, 2016 05:36
Once ensures that a function can only be called once.
/**
* You pass it a function, and you get a function back.
* That function will call your function once, and
* thereafter will return undefined whenever it is
* called.
*/
const once = (fn) => {
let done = false;
return function () {
@stefanfrede
stefanfrede / maybe_decorator_recipe.js
Last active June 9, 2016 04:44
Maybe takes a function and checks for nothing.
/**
* Do nothing when given nothing.
*/
const maybe = (fn) =>
function (...args) {
if (args.length === 0) {
return;
} else {
for (let arg of args) {
if (arg == null) {
@stefanfrede
stefanfrede / tap_combinator_recipe.js
Created June 7, 2016 06:03
The “K Combinator".
/**
* tap takes a value and returns a function that
* always returns the value, but if you pass it a
* function, it executes the function for
* side-effects.
*/
const tap = (value, fn) => {
const curried = (fn) => (
typeof(fn) === 'function' && fn(value),
@stefanfrede
stefanfrede / unary_decorator_recipe.js
Created June 7, 2016 05:40
Unary takes any function and turns it into a function taking exactly one argument.
const unary = (fn) =>
fn.length === 1
? fn
: function (something) {
return fn.call(this, something);
};
/**
* Example use case
*
@stefanfrede
stefanfrede / partial_application_recipes.js
Last active June 27, 2016 05:34
Quickly and simply apply a single argument.
// leftmost
const callFirst = (fn, larg) =>
function (...rest) {
return fn.call(this, larg, ...rest);
}
// rightmost
const callLast = (fn, rarg) =>
function (...rest) {
return fn.call(this, ...rest, rarg);
@stefanfrede
stefanfrede / anki_code_js.txt
Last active January 27, 2018 08:20
Latex snippet for Anki to preview JavaScript code.
// Inline
[latex]\mintinline[bgcolor=black]{js}|...code goes here...|[/latex]
// Multi line
[latex]
\begin{minted}
[
frame=lines,
framesep=2mm,
@stefanfrede
stefanfrede / array_deduplicate.js
Created February 8, 2016 09:58
How to remove duplicate elements, of different data types, from an Array.
/**
* Primitives
*/
// If an Array only contains primitive values, we can deduplicate it by only using the filter and indexOf methods.
var deduped = [ 1, 1, 'a', 'a' ].filter(function (el, i, arr) {
return arr.indexOf(el) === i;
});
console.log(deduped); // [ 1, 'a' ]
@stefanfrede
stefanfrede / array_flatten.js
Created February 8, 2016 09:47
These are the three ways to merge a multidimensional array into a single array.
/**
* Given:
* var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]];
*
* Result:
* // [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
// Using concat() and apply()
var myNewArray1 = [].concat.apply([], myArray);