Commit e35119d5 authored by François Bobot's avatar François Bobot
Browse files

Ajout de petite fonction utile

parent 2f93f077
...@@ -39,6 +39,11 @@ let channel_contents_buf cin = ...@@ -39,6 +39,11 @@ let channel_contents_buf cin =
let channel_contents cin = Buffer.contents (channel_contents_buf cin) let channel_contents cin = Buffer.contents (channel_contents_buf cin)
let rec fold_channel f acc cin =
try
fold_channel f (f acc (input_line cin)) cin
with End_of_file -> acc
let file_contents_fmt f fmt = let file_contents_fmt f fmt =
try try
let cin = open_in f in let cin = open_in f in
......
...@@ -26,6 +26,9 @@ val channel_contents_buf : in_channel -> Buffer.t ...@@ -26,6 +26,9 @@ val channel_contents_buf : in_channel -> Buffer.t
(* put the content of an in_channel in a formatter *) (* put the content of an in_channel in a formatter *)
val channel_contents_fmt : in_channel -> Format.formatter -> unit val channel_contents_fmt : in_channel -> Format.formatter -> unit
(* fold on the line of a file *)
val fold_channel : ('a -> string -> 'a) -> 'a -> in_channel -> 'a
(* return the content of a file *) (* return the content of a file *)
val file_contents : string -> string val file_contents : string -> string
......
...@@ -59,6 +59,11 @@ let option_map_fold f acc x = ...@@ -59,6 +59,11 @@ let option_map_fold f acc x =
let acc, x = f acc x in let acc, x = f acc x in
acc, Some x acc, Some x
(* useful iterator on int *)
let rec foldi f acc min max =
if min > max then acc else foldi f (f acc min) (succ min) max
let rec mapi f = foldi (fun acc i -> f i::acc) []
(* useful list combinators *) (* useful list combinators *)
let rev_map_fold_left f acc l = let rev_map_fold_left f acc l =
......
...@@ -54,6 +54,10 @@ val option_eq : ('a -> 'b -> bool) -> 'a option -> 'b option -> bool ...@@ -54,6 +54,10 @@ val option_eq : ('a -> 'b -> bool) -> 'a option -> 'b option -> bool
val option_map_fold : val option_map_fold :
('a -> 'b -> 'a * 'b) -> 'a -> 'b option -> 'a * 'b option ('a -> 'b -> 'a * 'b) -> 'a -> 'b option -> 'a * 'b option
(* useful int iterator *)
val foldi : ('a -> int -> 'a) -> 'a -> int -> int -> 'a
val mapi : (int -> 'a) -> int -> int -> 'a list
(* useful list combinators *) (* useful list combinators *)
val rev_map_fold_left : val rev_map_fold_left :
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment