[m-rev.] for review: Add maybe_default/2 standard library function

Paul Bone paul at bone.id.au
Mon Aug 1 15:57:38 AEST 2016


This very simple change should be reviewed as it is a standard library
change.

Thanks.

---

Add maybe_default/2 standard library function

library/maybe.m:
    As above.

NEWS:
    Announce the new addition.
---
 NEWS            |  3 ++-
 library/maybe.m | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index a94da46..48df372 100644
--- a/NEWS
+++ b/NEWS
@@ -271,9 +271,10 @@ Changes to the Mercury standard library:
 
 * We have a added a new type, maybe_errors, to the maybe module.
 
-  The following predicate has been added to the maybe module:
+  The following predicate and function have been added to the maybe module:
 
    - fold2_maybe/6
+   - maybe_default/2
 
 * The following predicates and functions have been added to the calendar
   module:
diff --git a/library/maybe.m b/library/maybe.m
index 3f9f75c..fca865b 100644
--- a/library/maybe.m
+++ b/library/maybe.m
@@ -169,6 +169,14 @@
     %
 :- pred maybe_is_yes(maybe(T)::in, T::out) is semidet.
 
+    % maybe_default(_, yes(X)) = X.
+    % maybe_default(X, no) = X.
+    %
+    % Return the value from within the maybe or a default value if there is
+    % none.
+    %
+:- func maybe_default(T, maybe(T)) = T.
+
 %---------------------------------------------------------------------------%
 %---------------------------------------------------------------------------%
 
@@ -205,6 +213,9 @@ map_fold3_maybe(P, yes(T0), yes(T), !A, !B, !C) :-
 
 maybe_is_yes(yes(X), X).
 
+maybe_default(_, yes(X)) = X.
+maybe_default(D, no) = D.
+
 %---------------------------------------------------------------------------%
 :- end_module maybe.
 %---------------------------------------------------------------------------%
-- 
2.7.4



More information about the reviews mailing list