<commit_before>(*
Copyright (c) 2013, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.  Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

(** {1 Continuation List} *)

type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit

type + 'a t = unit ->
  [ `Nil
  | `Cons of 'a * 'a t
  ]

let nil () = `Nil
let cons a b () = `Cons (a,b)
let empty = nil

let singleton x () = `Cons (x, nil)

let rec _forever x () = `Cons (x, _forever x)

let rec _repeat n x () =
  if n<=0 then `Nil else `Cons (x, _repeat (n-1) x)

let repeat ?n x = match n with
  | None -> _forever x
  | Some n -> _repeat n x

(*$T
  repeat ~n:4 0 |> to_list = [0;0;0;0]
  repeat ~n:0 1 |> to_list = []
  repeat 1 |> take 20 |> to_list = (repeat ~n:20 1 |> to_list)
*)

let is_empty l = match l () with
  | `Nil -> true
  | `Cons _ -> false

let head_exn l = match l() with | `Nil -> raise Not_found | `Cons (x, _) -> x
let head l = match l() with `Nil -> None | `Cons (x, _) -> Some x
let tail_exn l = match l() with | `Nil -> raise Not_found | `Cons (_, l) -> l
let tail l = match l() with | `Nil -> None | `Cons (_, l) -> Some l

let rec equal eq l1 l2 = match l1(), l2() with
  | `Nil, `Nil -> true
  | `Nil, _
  | _, `Nil -> false
  | `Cons (x1,l1'), `Cons (x2,l2') ->
      eq x1 x2 && equal eq l1' l2'

let rec compare cmp l1 l2 = match l1(), l2() with
  | `Nil, `Nil -> 0
  | `Nil, _ -> -1
  | _, `Nil -> 1
  | `Cons (x1,l1'), `Cons (x2,l2') ->
      let c = cmp x1 x2 in
      if c = 0 then compare cmp l1' l2' else c

let rec fold f acc res = match res () with
  | `Nil -> acc
  | `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 iteri f l =
  let rec aux f l i = match l() with
    | `Nil -> ()
    | `Cons (x, l') ->
        f i x;
        aux f l' (i+1)
  in
  aux f l 0

let length l = fold (fun acc _ -> acc+1) 0 l

let rec take n (l:'a t) () = match l () with
  | _ when n=0 -> `Nil
  | `Nil -> `Nil
  | `Cons (x,l') -> `Cons (x, take (n-1) l')

let rec take_while p l () = match l () with
  | `Nil -> `Nil
  | `Cons (x,l') ->
      if p x then `Cons (x, take_while p l') else `Nil

(*$T
  of_list [1;2;3;4] |> take_while (fun x->x < 4) |> to_list = [1;2;3]
*)

let rec drop n (l:'a t) () = match l () with
  | l' when n=0 -> l'
  | `Nil -> `Nil
  | `Cons (_,l') -> drop (n-1) l' ()

let rec drop_while p l () = match l() with<commit_msg>bugfix in `CCKList.take`, it was slightly too eager<commit_after>(*
Copyright (c) 2013, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.  Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

(** {1 Continuation List} *)

type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit

type + 'a t = unit ->
  [ `Nil
  | `Cons of 'a * 'a t
  ]

let nil () = `Nil
let cons a b () = `Cons (a,b)
let empty = nil

let singleton x () = `Cons (x, nil)

let rec _forever x () = `Cons (x, _forever x)

let rec _repeat n x () =
  if n<=0 then `Nil else `Cons (x, _repeat (n-1) x)

let repeat ?n x = match n with
  | None -> _forever x
  | Some n -> _repeat n x

(*$T
  repeat ~n:4 0 |> to_list = [0;0;0;0]
  repeat ~n:0 1 |> to_list = []
  repeat 1 |> take 20 |> to_list = (repeat ~n:20 1 |> to_list)
*)

let is_empty l = match l () with
  | `Nil -> true
  | `Cons _ -> false

let head_exn l = match l() with | `Nil -> raise Not_found | `Cons (x, _) -> x
let head l = match l() with `Nil -> None | `Cons (x, _) -> Some x
let tail_exn l = match l() with | `Nil -> raise Not_found | `Cons (_, l) -> l
let tail l = match l() with | `Nil -> None | `Cons (_, l) -> Some l

let rec equal eq l1 l2 = match l1(), l2() with
  | `Nil, `Nil -> true
  | `Nil, _
  | _, `Nil -> false
  | `Cons (x1,l1'), `Cons (x2,l2') ->
      eq x1 x2 && equal eq l1' l2'

let rec compare cmp l1 l2 = match l1(), l2() with
  | `Nil, `Nil -> 0
  | `Nil, _ -> -1
  | _, `Nil -> 1
  | `Cons (x1,l1'), `Cons (x2,l2') ->
      let c = cmp x1 x2 in
      if c = 0 then compare cmp l1' l2' else c

let rec fold f acc res = match res () with
  | `Nil -> acc
  | `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 iteri f l =
  let rec aux f l i = match l() with
    | `Nil -> ()
    | `Cons (x, l') ->
        f i x;
        aux f l' (i+1)
  in
  aux f l 0

let length l = fold (fun acc _ -> acc+1) 0 l

let rec take n (l:'a t) () =
  if n=0 then `Nil
  else match l () with
    | `Nil -> `Nil
    | `Cons (x,l') -> `Cons (x, take (n-1) l')

let rec take_while p l () = match l () with
  | `Nil -> `Nil
  | `Cons (x,l') ->
      if p x then `Cons (x, take_while p l') else `Nil

(*$T
  of_list [1;2;3;4] |> take_while (fun x->x < 4) |> to_list = [1;2;3]
*)

let rec drop n (l:'a t) () = match l () with
  | l' when n=0 -> l'
  | `Nil -> `Nil
  | `Cons (_,l') -> drop (n-1) l' ()

let rec drop_while p l () = match l() with
{'fraction_non_alphanumeric': 0.13951323314270514, 'fraction_numerical': 0.01728953318260407, 'mean_word_length': 2.8943552563438635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6570352', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2653, 'n_words': 1194}