Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Created September 15, 2022 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinocarl/21113e2a6c068c77fa45c84924a4aa4d to your computer and use it in GitHub Desktop.
Save dinocarl/21113e2a6c068c77fa45c84924a4aa4d to your computer and use it in GitHub Desktop.
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(
apply(path),
juxt([
compose(
flip(concat)(endOfPath),
of,
prop(lookupName)
),
identity,
])
);
const logAdd = curry((a, b) => {
const res = a + b;
// console.log(res);
return res;
});
const logAlways = (a) => (b) => {
// console.log(`a: ${a}`);
return a;
};
const accumApplyTo = (initVal) => (acc, fn) => applyTo(initVal, fn);
const leftCond = curry((fnList, initVal) =>
reduceWhile(
isNil,
accumApplyTo(initVal),
undefined,
fnList
)
);
const uistate = {
currView: 'page2',
page2: {sp: 100},
page4: {sp: 200},
};
leftCond([
always(null),
always(null),
always(undefined),
lookWithin({
lookupName: 'currView',
endOfPath: ['sp']
}),
path(['subpage1', 'sp']),
path(['subpage4', 'spx']),
prop('currView'),
always(0),
],
uistate
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment