mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
additional functions for KList
This commit is contained in:
parent
ab83ea4827
commit
c5b2373c03
3 changed files with 21 additions and 3 deletions
|
|
@ -34,6 +34,8 @@ type + 'a t =
|
||||||
let nil = `Nil
|
let nil = `Nil
|
||||||
let cons a b = `Cons (a,b)
|
let cons a b = `Cons (a,b)
|
||||||
|
|
||||||
|
let singleton x = `Cons (x, fun () -> `Nil)
|
||||||
|
|
||||||
let to_list l =
|
let to_list l =
|
||||||
let rec direct i (l:'a t) = match l with
|
let rec direct i (l:'a t) = match l with
|
||||||
| `Nil -> []
|
| `Nil -> []
|
||||||
|
|
@ -45,6 +47,12 @@ let to_list l =
|
||||||
in
|
in
|
||||||
direct 200 l
|
direct 200 l
|
||||||
|
|
||||||
|
let of_list l =
|
||||||
|
let rec aux l () = match l with
|
||||||
|
| [] -> `Nil
|
||||||
|
| x::l' -> `Cons (x, aux l')
|
||||||
|
in aux l ()
|
||||||
|
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
type 'a gen = unit -> 'a option
|
type 'a gen = unit -> 'a option
|
||||||
|
|
||||||
|
|
@ -65,6 +73,10 @@ let rec fold f acc res = match res with
|
||||||
| `Nil -> acc
|
| `Nil -> acc
|
||||||
| `Cons (s, cont) -> fold f (f acc s) (cont ())
|
| `Cons (s, cont) -> fold f (f acc s) (cont ())
|
||||||
|
|
||||||
|
let rec iter f l = match l with
|
||||||
|
| `Nil -> ()
|
||||||
|
| `Cons (x, l') -> f x; iter f (l' ())
|
||||||
|
|
||||||
let length l = fold (fun acc _ -> acc+1) 0 l
|
let length l = fold (fun acc _ -> acc+1) 0 l
|
||||||
|
|
||||||
let rec take n (l:'a t):'a t = match l with
|
let rec take n (l:'a t):'a t = match l with
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,10 @@ val nil : 'a t
|
||||||
|
|
||||||
val cons : 'a -> (unit -> 'a t) -> 'a t
|
val cons : 'a -> (unit -> 'a t) -> 'a t
|
||||||
|
|
||||||
|
val singleton : 'a -> 'a t
|
||||||
|
|
||||||
|
val of_list : 'a list -> 'a t
|
||||||
|
|
||||||
val to_list : 'a t -> 'a list
|
val to_list : 'a t -> 'a list
|
||||||
(** Gather all values into a list *)
|
(** Gather all values into a list *)
|
||||||
|
|
||||||
|
|
@ -46,6 +50,8 @@ val to_gen : 'a t -> 'a gen
|
||||||
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
||||||
(** Fold on values *)
|
(** Fold on values *)
|
||||||
|
|
||||||
|
val iter : ('a -> unit) -> 'a t -> unit
|
||||||
|
|
||||||
val length : 'a t -> int
|
val length : 'a t -> int
|
||||||
|
|
||||||
val take : int -> 'a t -> 'a t
|
val take : int -> 'a t -> 'a t
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(** Compute the memory footprint of a value (and its subvalues). Reference is
|
(** Compute the memory footprint of a value (and its subvalues). Reference is
|
||||||
http://rwmj.wordpress.com/2009/08/05/ocaml-internals-part-2-strings-and-other-types/ *)
|
http://rwmj.wordpress.com/2009/08/05/ocaml-internals-part-2-strings-and-other-types/ *)
|
||||||
|
|
||||||
open Sequence.Infix
|
module Sequence = CCSequence
|
||||||
|
|
||||||
(** A graph vertex is an Obj.t value *)
|
(** A graph vertex is an Obj.t value *)
|
||||||
let graph =
|
let graph =
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue