home
/
code
/
hope.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Replace lists:keysearch with hope_kv_list:get
[hope.git]
/
src
/
hope_option.erl
diff --git
a/src/hope_option.erl
b/src/hope_option.erl
index
ab6eab7
..
02ef2f3
100644
(file)
--- a/
src/hope_option.erl
+++ b/
src/hope_option.erl
@@
-1,16
+1,24
@@
-module(hope_option).
-module(hope_option).
+-behavior(hope_gen_monad).
-export_type(
[ t/1
]).
-export(
-export_type(
[ t/1
]).
-export(
- [ put/2
- , get/2
+ % Generic monad interface
+ [ return/1
, map/2
, map/2
+ , pipe/2
+
+ % Specific to hope_option:t()
+ , return/2
+ , put/2
+ , get/2
, iter/2
, of_result/1
, iter/2
, of_result/1
+ , of_undefined/1
]).
]).
@@
-23,16
+31,26
@@
-spec put(A, fun((A) -> boolean())) ->
t(A).
put(X, F) ->
-spec put(A, fun((A) -> boolean())) ->
t(A).
put(X, F) ->
- case F(X)
- of true -> {some, X}
- ; false -> none
- end.
+ return(X, F).
-spec get(t(A), Default :: A) ->
A.
get({some, X}, _) -> X;
get(none , Y) -> Y.
-spec get(t(A), Default :: A) ->
A.
get({some, X}, _) -> X;
get(none , Y) -> Y.
+-spec return(A) ->
+ {some, A}.
+return(X) ->
+ {some, X}.
+
+-spec return(A, fun((A) -> boolean())) ->
+ t(A).
+return(X, Condition) ->
+ case Condition(X)
+ of true -> {some, X}
+ ; false -> none
+ end.
+
-spec map(t(A), fun((A) -> (B))) ->
t(B).
map({some, X}, F) -> {some, F(X)};
-spec map(t(A), fun((A) -> (B))) ->
t(B).
map({some, X}, F) -> {some, F(X)};
@@
-43,7
+61,22
@@
map(none , _) -> none.
iter({some, X}, F) -> ok = F(X);
iter(none , _) -> ok.
iter({some, X}, F) -> ok = F(X);
iter(none , _) -> ok.
+-spec pipe([fun((A) -> t(B))], A) ->
+ t(B).
+pipe([], X) ->
+ return(X);
+pipe([F|Fs], X) ->
+ case F(X)
+ of none -> none
+ ; {some, Y} -> pipe(Fs, Y)
+ end.
+
-spec of_result(hope_result:t(A, _B)) ->
t(A).
of_result({ok, X}) -> {some, X};
of_result({error, _}) -> none.
-spec of_result(hope_result:t(A, _B)) ->
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}.
This page took
0.021549 seconds
and
4
git commands to generate.