|
|
ae5492a |
From f57dc38c05f3b8b002b8daa823a21ad25f5d682d Mon Sep 17 00:00:00 2001
|
|
|
ae5492a |
From: "Richard W.M. Jones" <rjones@redhat.com>
|
|
|
ae5492a |
Date: Tue, 10 Nov 2015 20:07:56 +0000
|
|
|
ae5492a |
Subject: [PATCH 2/5] mllib: Add Common_utils 'may' function.
|
|
|
ae5492a |
|
|
|
ae5492a |
This higher order function encapsulates the following pattern:
|
|
|
ae5492a |
|
|
|
ae5492a |
match x with
|
|
|
ae5492a |
| None -> ()
|
|
|
ae5492a |
| Some x -> f x
|
|
|
ae5492a |
|
|
|
ae5492a |
(replaced with: `may f x`)
|
|
|
ae5492a |
|
|
|
ae5492a |
This is taken from OCaml lablgtk (Gtk bindings) where this pattern is
|
|
|
ae5492a |
used frequently.
|
|
|
ae5492a |
|
|
|
ae5492a |
See also: https://ocaml.org/learn/tutorials/labels.html
|
|
|
ae5492a |
|
|
|
ae5492a |
(cherry picked from commit 9a2c5b7c4d367aae665ab66f85e463f9d244e465)
|
|
|
ae5492a |
---
|
|
|
ae5492a |
mllib/common_utils.ml | 4 ++++
|
|
|
ae5492a |
mllib/common_utils.mli | 3 +++
|
|
|
ae5492a |
2 files changed, 7 insertions(+)
|
|
|
ae5492a |
|
|
|
ae5492a |
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
|
|
|
ae5492a |
index c714cb3..dfee615 100644
|
|
|
ae5492a |
--- a/mllib/common_utils.ml
|
|
|
ae5492a |
+++ b/mllib/common_utils.ml
|
|
|
ae5492a |
@@ -254,6 +254,10 @@ let rec assoc ?(cmp = compare) ~default x = function
|
|
|
ae5492a |
| (y, y') :: _ when cmp x y = 0 -> y'
|
|
|
ae5492a |
| _ :: ys -> assoc ~cmp ~default x ys
|
|
|
ae5492a |
|
|
|
ae5492a |
+let may f = function
|
|
|
ae5492a |
+ | None -> ()
|
|
|
ae5492a |
+ | Some x -> f x
|
|
|
ae5492a |
+
|
|
|
ae5492a |
let istty chan =
|
|
|
ae5492a |
Unix.isatty (Unix.descr_of_out_channel chan)
|
|
|
ae5492a |
|
|
|
ae5492a |
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
|
|
|
ae5492a |
index 39e05a1..081e4a4 100644
|
|
|
ae5492a |
--- a/mllib/common_utils.mli
|
|
|
ae5492a |
+++ b/mllib/common_utils.mli
|
|
|
ae5492a |
@@ -65,6 +65,9 @@ val assoc : ?cmp:('a -> 'a -> int) -> default:'b -> 'a -> ('a * 'b) list -> 'b
|
|
|
ae5492a |
(** Like {!List.assoc} but with a user-defined comparison function, and
|
|
|
ae5492a |
instead of raising [Not_found], it returns the [~default] value. *)
|
|
|
ae5492a |
|
|
|
ae5492a |
+val may : ('a -> unit) -> 'a option -> unit
|
|
|
ae5492a |
+(** [may f (Some x)] runs [f x]. [may f None] does nothing. *)
|
|
|
ae5492a |
+
|
|
|
ae5492a |
val prog : string
|
|
|
ae5492a |
(** The program name (derived from {!Sys.executable_name}). *)
|
|
|
ae5492a |
|
|
|
ae5492a |
--
|
|
|
ae5492a |
2.5.0
|
|
|
ae5492a |
|