Skip to content

Instantly share code, notes, and snippets.

View dinocarl's full-sized avatar

Carl Albrecht-Buehler dinocarl

View GitHub Profile
@dinocarl
dinocarl / cond.ts
Created June 25, 2025 14:58
Recursive cond fn in typescript
type Predicate<T> = (val: T) => boolean;
type Mapper<T, R> = (val: T) => R;
type CondTuple<T, R> = [Predicate<T>, Mapper<T, R>];
function cond<T, R>(clauses: CondTuple<T, R>[] = []) {
const [[pred, mapr] = [undefined, undefined], ...rest] = clauses;
return (val: T): R | undefined => {
// base case: pred is undefined - exit the recursion
if (pred === undefined || mapr === undefined) {
type Predicate<T> = (value: T) => boolean;
type Mapper<T, R> = (value: T) => R;
type CondPair<T, R> = [Predicate<T>, Mapper<T, R>];
// recursive cond that can breaks early or returns undefined
const cond = <T, R>(
[[pred, mapr] = [] as unknown as CondPair<T, R>, ...rest]: CondPair<T, R>[] = []
): ((val: T) => R | undefined) => (val: T): R | undefined =>
!pred ? undefined :
pred(val) ? mapr(val) : cond(rest)(val);
@dinocarl
dinocarl / smplRecMerge.js
Created July 18, 2024 16:58
Simplistic recursive merge fn for JS
const recMerge = (objA, objB) => Object.entries(objB).reduce(
(acc, [key, val]) => Object.assign(
{},
objA,
{ [key]: is(Object, val) ? recMerge(objA[key], val) : val }
),
{}
);
const strongOrPlain = (bool, str) => bool
? `<strong>${str}</strong>`
: str;
const RangeItemComp = ({header, start, end, highlighted}) => `<li>${
strongOrPlain(
highlighted,
`${header}: ${start} - ${end}`
)}</li>`;
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),