From: Siraaj Khandkar Date: Thu, 4 Jun 2015 21:00:16 +0000 (-0400) Subject: Implement hope_option:validate/2 X-Git-Tag: 3.4.0^0 X-Git-Url: https://git.xandkar.net/?p=hope.git;a=commitdiff_plain;h=352ddeb475ab48aabfab59558827e0e8d927551f Implement hope_option:validate/2 --- diff --git a/src/hope.app.src b/src/hope.app.src index 45b460d..41b038c 100644 --- a/src/hope.app.src +++ b/src/hope.app.src @@ -1,7 +1,7 @@ {application, hope, [ {description, "Higher Order Programming in Erlang"}, - {vsn, "3.3.0"}, + {vsn, "3.4.0"}, {registered, []}, {applications, [ kernel, diff --git a/src/hope_option.erl b/src/hope_option.erl index 02ef2f3..996e342 100644 --- a/src/hope_option.erl +++ b/src/hope_option.erl @@ -19,6 +19,7 @@ , iter/2 , of_result/1 , of_undefined/1 + , validate/2 ]). @@ -80,3 +81,13 @@ of_result({error, _}) -> none. t(A). of_undefined(undefined) -> none; of_undefined(X) -> {some, X}. + +-spec validate(t(A), fun((A) -> boolean())) -> + t(A). +validate(none, _) -> + none; +validate({some, X}=T, F) -> + case F(X) + of false -> none + ; true -> T + end. diff --git a/test/hope_option_SUITE.erl b/test/hope_option_SUITE.erl index 370cf24..af1e12e 100644 --- a/test/hope_option_SUITE.erl +++ b/test/hope_option_SUITE.erl @@ -15,6 +15,7 @@ , t_map/1 , t_iter/1 , t_pipe/1 + , t_validate/1 ]). @@ -38,6 +39,7 @@ groups() -> , t_map , t_iter , t_pipe + , t_validate ], Properties = [parallel], [ {?GROUP, Properties, Tests} @@ -99,3 +101,9 @@ t_of_undefined(_Cfg) -> {some, Bar} = hope_option:of_undefined(Bar), {some, Baz} = hope_option:of_undefined(Baz), none = hope_option:of_undefined(undefined). + +t_validate(_Cfg) -> + IsFoo = fun (X) -> X =:= foo end, + none = hope_option:validate(none, IsFoo), + none = hope_option:validate({some, bar}, IsFoo), + {some, foo} = hope_option:validate({some, foo}, IsFoo).