Infer scalar types with let generalization
This commit is contained in:
15 files changed
+980
-13
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
let assoc_opt key items =
|
||||
let rec search = function
|
||||
| [] -> None
|
||||
| (candidate, value) :: rest -> if candidate = key then Some value else search rest
|
||||
in
|
||||
search items
|
||||
|
||||
let hashtbl_find_opt table key = try Some (Hashtbl.find table key) with Not_found -> None
|
||||
|
||||
let rec filter_map f = function
|
||||
| [] -> []
|
||||
| item :: rest -> (
|
||||
match f item with
|
||||
| Some value -> value :: filter_map f rest
|
||||
| None -> filter_map f rest)
|
||||
|
||||
let rec concat_map f = function
|
||||
| [] -> []
|
||||
| item :: rest -> f item @ concat_map f rest
|
||||
|
||||
let rec list_init count f =
|
||||
if count <= 0 then [] else f 0 :: list_init (count - 1) (fun index -> f (index + 1))
|
||||
|
||||
let rec take count items =
|
||||
if count <= 0 then []
|
||||
else
|
||||
match items with
|
||||
| [] -> []
|
||||
| item :: rest -> item :: take (count - 1) rest
|
||||
|
||||
let rec drop count items =
|
||||
if count <= 0 then items
|
||||
else match items with [] -> [] | _ :: rest -> drop (count - 1) rest
|
||||
|
||||
let contains value items = List.exists (fun item -> item = value) items
|
||||
|
||||
let string_before_char text character =
|
||||
match String.index_opt text character with
|
||||
| Some index -> String.sub text 0 index
|
||||
| None -> text
|
||||
|
||||
let starts_with prefix text =
|
||||
String.length text >= String.length prefix && String.sub text 0 (String.length prefix) = prefix
|
||||
|
||||
let rec join separator = function
|
||||
| [] -> ""
|
||||
| [ single ] -> single
|
||||
| item :: rest -> item ^ separator ^ join separator rest
|
||||
Reference in new issue
Block a user