Implement hope_option:validate/2 3.4.0
authorSiraaj Khandkar <siraaj@khandkar.net>
Thu, 4 Jun 2015 21:00:16 +0000 (17:00 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 4 Jun 2015 21:00:16 +0000 (17:00 -0400)
src/hope.app.src
src/hope_option.erl
test/hope_option_SUITE.erl

index 45b460d..41b038c 100644 (file)
@@ -1,7 +1,7 @@
 {application, hope,
  [
   {description, "Higher Order Programming in Erlang"},
-  {vsn, "3.3.0"},
+  {vsn, "3.4.0"},
   {registered, []},
   {applications, [
                   kernel,
index 02ef2f3..996e342 100644 (file)
@@ -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.
index 370cf24..af1e12e 100644 (file)
@@ -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).
This page took 0.019976 seconds and 4 git commands to generate.