Skip to content

Instantly share code, notes, and snippets.

View dinocarl's full-sized avatar

Carl Albrecht-Buehler dinocarl

View GitHub Profile
// use transducers
const appendTo = flip(append);
const apCh = (fn, accVal) => (item) => accVal = fn(accVal, item);
const steps = [
filter(test(/[()]/)),
map((item) => item === '(' ? 1 : -1),
map(apCh(add, 0)), // scan transducer
];
@dinocarl
dinocarl / scan.js
Last active November 12, 2020 04:35
JS Scan function
const apCh = (fn, accVal) => (item) => accVal = fn(accVal, item);
const scanr = curry((fn, acc, xs) => map(
apCh(fn, acc),
xs
))
scanr(add, 0)(
[5,6,7,8]
);
const isntEmpty = complement(isEmpty);
const isntNil = complement(isNil);
const isntNaN = complement(isNaN);
const pageLens = lensProp('page');
const numberValidations = [
isntNaN,
isntNil,
isntEmpty,
@dinocarl
dinocarl / propsOr.js
Last active July 17, 2020 21:14
Uses a fallback value, then an array of strings that serve as an ordered list of preferred props from most to least to return from an object. The first valid prop gets returned.
const validVal = compose(
not,
anyPass([isEmpty, isNil])
);
const validProp = curry((propName, obj) => validVal(prop(propName, obj)));
const propsOr = curry((fallback, propsList, obj) => compose(
cond,
append([T, always(fallback)]),
@dinocarl
dinocarl / mergeWithStrategy.js
Created June 10, 2020 16:29
Apply an object of functions to 2 others of Data to arrive at a new one where each key has had its special function run against the other's values.
const selectBrevity= (l, r) => lte(length(l), length(r))
? l
: r;
const lispy = ([fn, ...args]) => fn(...args);
const makeSet = (arr) => uniq(flatten(arr));
const zipObjLisped = (arr) => ({ [head(arr)]: lispy(tail(arr)) });
@dinocarl
dinocarl / avgToThsnds.js
Created June 5, 2020 15:43
Takes an array of numbers and returns their average, rounded to the thousands place
const toThsnds = compose(
multiply(1000),
Math.round,
multiply(1/1000)
);
const avgToThsnds = (arr) => compose(
toThsnds,
multiply(1/length(arr)),
reduce(add, 0)
const izzer = (str, mod) => (num) => modulo(num, mod) === 0
? str
: '';
const izzerStr = compose(
join(''),
juxt([
izzer('fizz', 3),
izzer('buzz', 5),
])
@dinocarl
dinocarl / groupBy.js
Created March 6, 2020 18:17
Vanilla JS groupBy implementation
// utilities
const includes = (needle) => (haystack) => (haystack.indexOf(needle) > -1);
const includesAtPos = (needle) => (position) => (haystack) => (haystack.indexOf(needle) === position);
const testFor = (re) => (str) => re.test(str);
const and = (...fnList) => (data) => fnList.every((fn) => fn(data));
const groupBy = (fn) => (arr) => arr.reduce(
(out, val) => {
let by = fn(val);
(out[by] = out[by] || []).push(val);
const md = require('markdown-it')();
const markdownItAttrs = require('markdown-it-attrs');
const src = `
# header {: .title-class #title-id }
some text {: with=attrs and="attrs with space" }
more body copy with some *emphasis*{: .emph-class } thrown in {: .para-class}
- item 1
- item 2 {: .item-class }
- item 3
grep -R --exclude-dir={node_modules,build,.git} --exclude=*.{png,jpg,vim,swp,swo} 'searchString' ./