Skip to content

Instantly share code, notes, and snippets.

View dinocarl's full-sized avatar

Carl Albrecht-Buehler dinocarl

View GitHub Profile
const S = f => g => x => f (x) (g (x));
const S2 = f => g => h => x => f (g (x)) (h (x));
// create a list based on a list of numbers that won't
// contain a zero, where the result is a list of products
// of the list without the number at the position of a given member
// eg [5,2,1,4,3] => [24, 60, 120, 30, 40]
// the key is that the computed product of the list and
// then divided by a given member, is equivalent to
@dinocarl
dinocarl / assocPath.js
Created September 7, 2023 13:44
Simple dependency-less assocPath function
const assocPth = ([head, ...rest], value, obj) => Array.isArray(obj)
? [].concat(
obj.slice(0, head),
[rest.length
? assocPth(rest, value, obj[head])
: value],
obj.slice(head + 1))
: Object.assign({},
obj,
{[head]: rest.length
@dinocarl
dinocarl / maxPropLenList.js
Created March 9, 2023 19:37
Given a list of objects and list of keys from them, find the longest length
const data = [
{ label: 'first', val: 'gold-like' },
{ label: 'second', val: 'silver-esque' },
{ label: 'third', val: 'bronz-ish' },
];
const propLen = (propName) => compose(
length,
propOr('', propName)
);
// permitted actions for state changes
const eventActions = {
write: (path, val = null) => assocPath(path, val),
erase: (path) => dissocPath(path),
};
const noop = () => () => {};
// run a legitmate action on a state object
const eventToState = ({ type, path, val }, stateObj, actions) => propOr(
const formula =
{'+': [
2, 2, 3,
{'-': [
5,
{'+': [
2, {'var': 'base-val'}, {var: 'base'},
{'+': [
1,
{'*': [
@dinocarl
dinocarl / dotcomp.js
Last active October 14, 2022 14:59
A dot operator function with reduce
const dot = (f, g) => a => f(g(a));
const cmp = reduce(dot, identity);
const cmp2 = reduce(o, identity);
[
compose(
sum,
range(2),
@dinocarl
dinocarl / leftCond.js
Created September 15, 2022 18:30
Like cond, but simply returns the first truthy value it encounters when executing functions in fnList against initVal. Intended for the occastions when one wants to write a cond, but the left and the right sides end up with the same function, but can also be used in any circumstance where the function being run can both serve as predicate and re…
// accepts an argument object and a data object.
// first returns the value at lookupName in the data object
// and then applies the value of that back onto the data object
// treating it as meta data about the data object, eg
// lookWIthin(
// { lookupName: 'currPage', endOfPath: ['position'] },
// { currPage: 'page!', page!: {position: 55} }
// ) => 55
const lookWithin = ({ lookupName, endOfPath = [] }) => compose(
const juxtMerge = (fnList) => compose(
mergeAll,
juxt(fnList)
);
// given an object, a foreignKey (name), and a lookup object
// containing data keyed to the foreignKey,
// return the data of the lookup, or a fallback value
const propOfPropOr = (fallback, foreignKey, lookup) => (item) => propOr(
fallback,
@dinocarl
dinocarl / everyItemEqual.js
Last active August 5, 2022 02:11
Different ways of checking whether all items in an array are equal
const eq = (a) => (b) => a === b;
const everyItemEquala = compose(
apply(all),
juxt([compose(equals, head), identity])
);
const everyItemEqualb = compose(
equals(1),
length,
const filterOperations = {
eq: equals,
neq: complement(equals),
lt: gt,
gt: lt,
lte: gte,
gte: lte,
includes: includes,
}