From a626cf31904f7d782c8c9756adc4279b0dd5db66 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Sun, 8 Feb 2015 01:09:24 -0500 Subject: [PATCH] Implement hope_list:first_match/2 Returns the option of a tag of the first tag/function pair where function returns true for the input. --- src/hope_list.erl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/hope_list.erl b/src/hope_list.erl index abfb3d3..f179d1a 100644 --- a/src/hope_list.erl +++ b/src/hope_list.erl @@ -10,6 +10,7 @@ , map/3 % Tunable recursion limit , map_rev/2 , map_slow/2 + , first_match/2 ]). @@ -102,3 +103,13 @@ unique_preserve_order(L) -> end end, lists:reverse(lists:foldl(PrependIfNew, [], L)). + +-spec first_match([{Tag, fun((A) -> boolean())}], A) -> + hope_option:t(Tag). +first_match([], _) -> + none; +first_match([{Tag, F} | Tests], X) -> + case F(X) + of true -> {some, Tag} + ; false -> first_match(Tests, X) + end. -- 2.20.1