Skip to content

Commit

Permalink
exhaustive-tree-search
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Jan 3, 2025
1 parent 3b5da9b commit 5fbbe20
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ export const findInTree =
}
return null;
};

export const findInTreeExhaustive =
<T>(predicate: (t: T) => boolean, children: (t: T) => T[]) => (t: T): T[] => {
const results = (predicate(t)) ? [t] : [];
for (const child of children(t)) {
results.push(...findInTreeExhaustive(predicate, children)(child));
}
return results;
};

0 comments on commit 5fbbe20

Please sign in to comment.