home
/
code
/
hope.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Define and implement a generic monad behavior.
[hope.git]
/
src
/
hope_option.erl
diff --git
a/src/hope_option.erl
b/src/hope_option.erl
index
ab6eab7
..
180c416
100644
(file)
--- a/
src/hope_option.erl
+++ b/
src/hope_option.erl
@@
-1,5
+1,6
@@
-module(hope_option).
-module(hope_option).
+-behavior(hope_monad).
-export_type(
[ t/1
-export_type(
[ t/1
@@
-8,8
+9,10
@@
-export(
[ put/2
, get/2
-export(
[ put/2
, get/2
+ , return/1
, map/2
, iter/2
, map/2
, iter/2
+ , pipe/2
, of_result/1
]).
, of_result/1
]).
@@
-33,6
+36,11
@@
put(X, F) ->
get({some, X}, _) -> X;
get(none , Y) -> Y.
get({some, X}, _) -> X;
get(none , Y) -> Y.
+-spec return(A) ->
+ {some, A}.
+return(X) ->
+ {some, X}.
+
-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,6
+51,16
@@
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};
-spec of_result(hope_result:t(A, _B)) ->
t(A).
of_result({ok, X}) -> {some, X};
This page took
0.025152 seconds
and
4
git commands to generate.