Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Created July 4, 2018 08:33
Show Gist options
  • Save Willmo36/fc9cf224e368ea28cae22ee367dd6a41 to your computer and use it in GitHub Desktop.
Save Willmo36/fc9cf224e368ea28cae22ee367dd6a41 to your computer and use it in GitHub Desktop.
Tree.findFirst via Alt instance of Option
export const findFirst = <A>(predicate: (a: A) => boolean) => (t: Tree<A>): Option<A> => {
const ff = findFirst(predicate);
return t.fold(
() => none,
(l, v, r) =>
ff(l)
.orElse(() => ff(r))
.orElse(() => (predicate(v) ? some(v) : none))
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment