Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Created October 12, 2022 22:00
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/cf70d3b03da125f2389a206f7335c7ce to your computer and use it in GitHub Desktop.
Save dinocarl/cf70d3b03da125f2389a206f7335c7ce to your computer and use it in GitHub Desktop.
const formula =
{'+': [
2, 2, 3,
{'-': [
5,
{'+': [
2, {'var': 'base-val'}, {var: 'base'},
{'+': [
1,
{'*': [
1, 4]}]}]}]}]};
const plumbDepths = (depth) => (obj) => {
const [operator, args] = Object.entries(obj)[0];
return (operator === 'var')
? depth - 1
: args.map(
(item) => (is(Object, item)
? plumbDepths(depth + 1)(item)
: depth));
};
const deepestLevel = compose(
apply(Math.max),
flatten,
plumbDepths(0)
);
const obj2ListRec = (obj) => map(map((item) => (Array.isArray(item)
? map(
(child) => (is(Object, child)
? obj2ListRec(child)
: child
),
item)
: item
)),
Object.entries(obj)
);
[
deepestLevel(formula),
obj2ListRec(formula),
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment