diff options
Diffstat (limited to 'lib/util.ml')
| -rw-r--r-- | lib/util.ml | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/lib/util.ml b/lib/util.ml index a8c25f7456..9fb0d48ee8 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -87,7 +87,17 @@ let matrix_transpose mat = let identity x = x -let compose f g x = f (g x) +(** Left-to-right function composition: + + [f1 %> f2] is [fun x -> f2 (f1 x)]. + + [f1 %> f2 %> f3] is [fun x -> f3 (f2 (f1 x))]. + + [f1 %> f2 %> f3 %> f4] is [fun x -> f4 (f3 (f2 (f1 x)))] + + etc. +*) +let (%>) f g x = g (f x) let const x _ = x @@ -124,11 +134,38 @@ let delayed_force f = f () type ('a, 'b) union = ('a, 'b) CSig.union = Inl of 'a | Inr of 'b type 'a until = 'a CSig.until = Stop of 'a | Cont of 'a +type ('a, 'b) eq = ('a, 'b) CSig.eq = Refl : ('a, 'a) eq + +module Union = +struct + let map f g = function + | Inl a -> Inl (f a) + | Inr b -> Inr (g b) + + (** Lifting equality onto union types. *) + let equal f g x y = match x, y with + | Inl x, Inl y -> f x y + | Inr x, Inr y -> g x y + | _, _ -> false + + let fold_left f g a = function + | Inl y -> f a y + | Inr y -> g a y +end -let map_union f g = function - | Inl a -> Inl (f a) - | Inr b -> Inr (g b) +let map_union = Union.map type iexn = Exninfo.iexn let iraise = Exninfo.iraise + +let open_utf8_file_in fname = + let is_bom s = + Int.equal (Char.code s.[0]) 0xEF && + Int.equal (Char.code s.[1]) 0xBB && + Int.equal (Char.code s.[2]) 0xBF + in + let in_chan = open_in fname in + let s = " " in + if input in_chan s 0 3 < 3 || not (is_bom s) then seek_in in_chan 0; + in_chan |
