From f2e1fffc2e9ce505ae1891a2251ae2036ed13422 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 22 Dec 2014 16:35:06 -0500 Subject: [PATCH] Implement hope_option:of_undefined/1 --- src/hope.app.src | 2 +- src/hope_option.erl | 6 ++++++ test/hope_option_SUITE.erl | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/hope.app.src b/src/hope.app.src index 6446af5..e8f8be9 100644 --- a/src/hope.app.src +++ b/src/hope.app.src @@ -1,7 +1,7 @@ {application, hope, [ {description, "Higher Order Programming in Erlang"}, - {vsn, "1.8.0"}, + {vsn, "1.9.0"}, {registered, []}, {applications, [ kernel, diff --git a/src/hope_option.erl b/src/hope_option.erl index 180c416..529158d 100644 --- a/src/hope_option.erl +++ b/src/hope_option.erl @@ -14,6 +14,7 @@ , iter/2 , pipe/2 , of_result/1 + , of_undefined/1 ]). @@ -65,3 +66,8 @@ pipe([F|Fs], X) -> t(A). of_result({ok, X}) -> {some, X}; of_result({error, _}) -> none. + +-spec of_undefined(undefined | A) -> + t(A). +of_undefined(undefined) -> none; +of_undefined(X) -> {some, X}. diff --git a/test/hope_option_SUITE.erl b/test/hope_option_SUITE.erl index 8789d3d..370cf24 100644 --- a/test/hope_option_SUITE.erl +++ b/test/hope_option_SUITE.erl @@ -9,6 +9,7 @@ %% Test cases -export( [ t_of_result/1 + , t_of_undefined/1 , t_put/1 , t_get/1 , t_map/1 @@ -31,6 +32,7 @@ all() -> groups() -> Tests = [ t_of_result + , t_of_undefined , t_put , t_get , t_map @@ -88,3 +90,12 @@ t_pipe(_Cfg) -> none = hope_option:pipe(Steps, 1), 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). -- 2.20.1