Commits

camlspotter committed eecf60b Merge

merge

Comments (0)

Files changed (8)

+<<<<<<< local
 2.3.0
 ------------
 
 
 let catch_exn f = catch (fun ~fail -> try f () with _ -> fail ())
 
+let format f ppf = function
+  | None -> Format.pp_print_string ppf "None"
+  | Some v -> Format.fprintf ppf "Some (%a)" f v
+
 module Pervasives = struct
   let from_Some = function
     | Some v -> v
 val from_Some : 'a option -> 'a
 (** may raise [Invalid_argument] *)
 
+val format : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a option -> unit
+
 module Pervasives : sig 
   val from_Some : 'a option -> 'a
   (** may raise [Invalid_argument] *)
 open List
 
-let iter_until f ls = 
+let iter_until f ls =
   let st = ref `Continue in
-  List.iter (fun x -> 
+  List.iter (fun x ->
     match f x with
     | `Break b -> st := `Break b; raise Exit
     | `Continue -> ()) ls;
   in
   iter 0 l
 
-let mapi f l = 
+let mapi f l =
   let rec map n = function
     | [] -> []
     | x::xs -> f n x :: map (n+1) xs
     else loop (fn i :: st) (i+1)
   in
   loop [] f
-  
-let find_opt f ls = try Some (List.find f ls) with Not_found -> None 
+
+let find_opt f ls = try Some (List.find f ls) with Not_found -> None
 
 let rec find_map_opt f = function
   | [] -> None
 module TR = struct
   let map f l = List.rev (List.rev_map f l)
 
-  TEST "TL.map" = 
+  TEST "TL.map" =
       map (fun x -> x) [1;2;3] = [1;2;3]
 
-  let rev_concat_map f l = 
+  let rev_concat_map f l =
     let rec loop st = function
       | [] -> st
       | x::xs -> loop (List.rev_append (f x) st) xs
     in
     loop [] l
 
-  TEST "TL.rev_concat_map" = 
+  TEST "TL.rev_concat_map" =
       rev_concat_map (fun x -> [x; x+1]) [1;2;3] = [4;3;3;2;2;1]
 
   let concat_map f l = List.rev (rev_concat_map f l)
 
-  TEST "TL.concat_map" = 
+  TEST "TL.concat_map" =
       concat_map (fun x -> [x; x+1]) [1;2;3] = [1;2;2;3;3;4]
-end 
+end
 
 let take n xs =
   let rec take_ n st xs =
   let r, dropped = split_at_ n [] xs in
   List.rev r, dropped
 
-TEST "split_at" = 
+TEST "split_at" =
       split_at 2 [] = ([], [])
       && split_at 2 [1] = ([1], [])
       && split_at 2 [1;2] = ([1;2], [])
     | [] -> None
     | x::xs ->
         let k = key x in
-        try 
+        try
           let x' = List.assoc k st in
           Some (x,x')
         with Not_found -> is_unique ((k,x)::st) xs
   | [x] -> x
   | _::xs -> last xs
 
-let assoc_all k = 
+let assoc_all k =
   let rec aux rev = function
     | [] -> List.rev rev
     | (k',v)::kvs when k = k' -> aux (v::rev) kvs
     | _::kvs -> aux rev kvs
   in
   aux []
-  
+
 let assoc_opt k l = try Some (List.assoc k l) with Not_found -> None
 
 let scani_left f acc xs =
 
 let sum xs = List.fold_left (+) 0 xs
 
-let group compare xs =
-  (* sort then, group, so inefficient than one which does one path *)
-  let rev_xs = sort (fun x y -> - (compare x y)) xs in
+let rev_group eq rev_xs =
   let rec grouping gs cur_group = function
-    | [] -> 
+    | [] ->
         begin match cur_group with
         | [] -> gs
         | _ -> cur_group :: gs
         match cur_group with
         | [] -> grouping gs [x] xs
         | y::_ ->
-            match compare x y with
-            | 0 -> grouping gs (x::cur_group) xs
-            | -1 -> grouping (cur_group::gs) [x] xs
-            | _ -> assert false
+            if eq x y then grouping gs (x::cur_group) xs
+            else grouping (cur_group::gs) [x] xs
   in
   grouping [] [] rev_xs
-        
-TEST "group" = 
-      group compare [1;2;3] = [[1];[2];[3]]
 
-TEST "group" = 
-      group compare [1;2;3;3;4;4;5;5;6] = [[1];[2];[3;3];[4;4];[5;5];[6]]
+let group eq xs = rev_group eq (List.rev xs)
+let sort_then_group compare xs = 
+  rev_group (fun x y -> compare x y = 0) (List.sort (fun x y -> - (compare x y)) xs)
 
-TEST "group" = 
-      group compare [1;2;3;3;4;4;5;5;6;6] = [[1];[2];[3;3];[4;4];[5;5];[6;6]]
+TEST "group" =
+      group (=) [1;2;3] = [[1];[2];[3]]
 
-TEST "group" = 
-      group compare [1;1;2;3;3;4;4;5;5;6;6] = [[1;1];[2];[3;3];[4;4];[5;5];[6;6]]
+TEST "group" =
+      group (=) [1;2;3;3;4;4;5;5;6] = [[1];[2];[3;3];[4;4];[5;5];[6]]
+
+TEST "group" =
+      group (=) [1;2;3;3;4;4;5;5;6;6] = [[1];[2];[3;3];[4;4];[5;5];[6;6]]
+
+TEST "group" =
+      group (=) [1;1;2;3;3;4;4;5;5;6;6] = [[1;1];[2];[3;3];[4;4];[5;5];[6;6]]
+
+TEST "group" =
+      group (=) [1;1;2;3;3;4;4;5;5;1;1;6;6] = [[1;1];[2];[3;3];[4;4];[5;5];[1;1];[6;6]]
 
 let splits_by max items =
   assert (max > 0);
   in
   List.rev (loop [] items)
 
-TEST "splits_by" = 
+TEST "splits_by" =
       splits_by 2 [] = []
       && splits_by 2 [1] = [[1]]
       && splits_by 2 [1;2] = [[1;2]]
 
 val sum : int list -> int
 
-val group : ('a -> 'a -> int) -> 'a list -> 'a list list
+val group : ('a -> 'a -> bool) -> 'a list -> 'a list list
+
+val sort_then_group : ('a -> 'a -> int) -> 'a list -> 'a list list
 
 val splits_by : int -> 'a list -> 'a list list
 (** Split a list into sub-lists of the fixed length *)

opam/spotlib.2.3.0/descr

+Useful functions for OCaml programming used by @camlspotter
+Spotlib is a small library package used for several softwares by Jun Furuse.
+It is almost a poor replication of Jane Street Core, but it is small.

opam/spotlib.2.3.0/opam

+opam-version: "1"
+maintainer: "jun.furuse@gmail.com"
+build: [
+  ["ocaml" "setup.ml" "-configure" "--prefix" "%{prefix}%"]
+  ["ocaml" "setup.ml" "-build"]
+  ["ocaml" "setup.ml" "-install"]
+]
+remove: [
+  ["ocaml" "setup.ml" "-uninstall"]
+]
+ocaml-version: [>= "4.0.1"]
+depends: ["ocamlfind" "omake" "pa_ounit" {>="109.18.00"}]

opam/spotlib.2.3.0/url

+archive: "https://bitbucket.org/camlspotter/spotlib/get/2.3.0.tar.gz"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.