From 352ddeb475ab48aabfab59558827e0e8d927551f Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Thu, 4 Jun 2015 17:00:16 -0400 Subject: [PATCH] Implement hope_option:validate/2 --- src/hope.app.src | 2 +- src/hope_option.erl | 11 +++++++++++ test/hope_option_SUITE.erl | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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). -- 2.20.1