Created
July 4, 2018 08:33
-
-
Save Willmo36/fc9cf224e368ea28cae22ee367dd6a41 to your computer and use it in GitHub Desktop.
Tree.findFirst via Alt instance of Option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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