From: Siraaj Khandkar Date: Thu, 7 Jul 2016 19:43:56 +0000 (-0400) Subject: Add hope_option:to_undefined/1 X-Git-Tag: 4.1.0 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=3190482e90cdb78d824fdeeaf582dad9ad2f541e;p=hope.git Add hope_option:to_undefined/1 --- diff --git a/src/hope.app.src b/src/hope.app.src index 16745b5..55dc7e3 100644 --- a/src/hope.app.src +++ b/src/hope.app.src @@ -1,7 +1,7 @@ {application, hope, [ {description, "Higher Order Programming in Erlang"}, - {vsn, "4.0.0"}, + {vsn, "4.1.0"}, {registered, []}, {applications, [ kernel, diff --git a/src/hope_option.erl b/src/hope_option.erl index 29836f7..2bd9e7b 100644 --- a/src/hope_option.erl +++ b/src/hope_option.erl @@ -19,6 +19,7 @@ , iter/2 , of_result/1 , of_undefined/1 + , to_undefined/1 , validate/2 ]). @@ -85,6 +86,11 @@ of_result({error, _}) -> none. of_undefined(undefined) -> none; of_undefined(X) -> {some, X}. +-spec to_undefined(t(A)) -> + undefined | A. +to_undefined(none) -> undefined; +to_undefined({some, X}) -> X. + -spec validate(t(A), fun((A) -> boolean())) -> t(A). validate(none, _) -> diff --git a/test/hope_option_SUITE.erl b/test/hope_option_SUITE.erl index d2a73a3..e7d14dc 100644 --- a/test/hope_option_SUITE.erl +++ b/test/hope_option_SUITE.erl @@ -9,7 +9,7 @@ %% Test cases -export( [ t_of_result/1 - , t_of_undefined/1 + , t_undefined/1 , t_put/1 , t_get/1 , t_map/1 @@ -33,7 +33,7 @@ all() -> groups() -> Tests = [ t_of_result - , t_of_undefined + , t_undefined , t_put , t_get , t_map @@ -93,14 +93,12 @@ t_pipe(_Cfg) -> none = hope_option:pipe(Steps, 2), none = hope_option:pipe(Steps, 3). -t_of_undefined(_Cfg) -> - Foo = foo, - Bar = bar, - Baz = baz, - {some, Foo} = hope_option:of_undefined(Foo), - {some, Bar} = hope_option:of_undefined(Bar), - {some, Baz} = hope_option:of_undefined(Baz), - none = hope_option:of_undefined(undefined). +t_undefined(_Cfg) -> + X = foo, + {some, X} = hope_option:of_undefined(X), + X = hope_option:to_undefined({some, X}), + none = hope_option:of_undefined(undefined), + undefined = hope_option:to_undefined(none). t_validate(_Cfg) -> IsFoo = fun (X) -> X =:= foo end,