package batteries

  1. Overview
  2. Docs
A community-maintained standard library extension

Install

Dune Dependency

Authors

Maintainers

Sources

v3.9.0.tar.gz
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb

doc/src/batteries.unthreaded/batInnerPervasives.ml.html

Source file batInnerPervasives.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(*
 * Copyright (C) 2012 Batteries Included Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

(* Inner functions for Pervasives, that can be accessed from other
   modules without pulling in all of batteries as deps. *)

let finally handler f x =
  let r = (
    try
      f x
    with
      e -> handler(); raise e
  ) in
  handler();
  r

let with_dispose ~dispose f x =
  finally (fun () -> dispose x) f x

(* unique int generation *)
let unique_value  = ref 0
let lock          = ref BatConcurrent.nolock
let unique ()     =
  BatConcurrent.sync !lock BatRef.post_incr unique_value

(*$Q unique
   Q.unit (fun () -> unique () <> unique ())
*)

type ('a, 'b) result =
##V>=4.8## ('a, 'b) Stdlib.result =
  | Ok  of 'a
  | Error of 'b

(* Ideas taken from Nicholas Pouillard's my_std.ml in ocamlbuild/ *)
let ignore_ok = function
    Ok _ -> ()
  | Error ex -> raise ex

let ok = function
    Ok v -> v
  | Error ex -> raise ex

let wrap f x = try Ok (f x) with ex -> Error ex

let forever f x = ignore (while true do ignore (f x) done)

let ignore_exceptions f x = try ignore (f x) with _ -> ()


  (** {1 Operators}*)

##V<4## let ( |> ) x f = f x
##V>=4## external (|>) : 'a -> ('a -> 'b) -> 'b = "%revapply"

##V<4## let ( @@ ) f x = f x
##V>=4## external ( @@ ) : ('a -> 'b) -> 'a -> 'b = "%apply"

let ( %> ) f g x = g (f x)

let ( % ) f g x = f (g x)

let flip f x y = f y x

let curry f x y = f (x,y)

let uncurry f (x,y) = f x y

let const x _ = x

let neg p x = not (p x)

let neg2 p x y = not (p x y)

external identity : 'a -> 'a = "%identity"

let tap f x = f x; x

let ( |? ) = BatOption.Infix.( |? )
OCaml

Innovation. Community. Security.