Blame src/extList.mli

rpm-build 0f2925
(*
rpm-build 0f2925
 * ExtList - additional and modified functions for lists.
rpm-build 0f2925
 * Copyright (C) 2003 Brian Hurt
rpm-build 0f2925
 * Copyright (C) 2003 Nicolas Cannasse
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is free software; you can redistribute it and/or
rpm-build 0f2925
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0f2925
 * License as published by the Free Software Foundation; either
rpm-build 0f2925
 * version 2.1 of the License, or (at your option) any later version,
rpm-build 0f2925
 * with the special exception on linking described in file LICENSE.
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is distributed in the hope that it will be useful,
rpm-build 0f2925
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0f2925
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0f2925
 * Lesser General Public License for more details.
rpm-build 0f2925
 *
rpm-build 0f2925
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0f2925
 * License along with this library; if not, write to the Free Software
rpm-build 0f2925
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rpm-build 0f2925
 *)
rpm-build 0f2925
rpm-build 0f2925
(** Additional and modified functions for lists.
rpm-build 0f2925
rpm-build 0f2925
  The OCaml standard library provides a module for list functions.
rpm-build 0f2925
  This ExtList module can be used to override the List module or
rpm-build 0f2925
  as a standalone module. It provides new functions and modify
rpm-build 0f2925
  the behavior of some other ones (in particular all functions
rpm-build 0f2925
  are now {b tail-recursive}).
rpm-build 0f2925
*)
rpm-build 0f2925
rpm-build 0f2925
module List :
rpm-build 0f2925
    sig
rpm-build 0f2925
rpm-build 0f2925
  (** {6 New functions} *)
rpm-build 0f2925
rpm-build 0f2925
  val init : int -> (int -> 'a) -> 'a list
rpm-build 0f2925
  (** Similar to [Array.init], [init n f] returns the list containing
rpm-build 0f2925
   the results of (f 0),(f 1).... (f (n-1)).
rpm-build 0f2925
   Raise [Invalid_arg "ExtList.init"] if n < 0.
rpm-build 0f2925
   Uses stdlib implementation in OCaml 4.06.0 and newer.
rpm-build 0f2925
  *)
rpm-build 0f2925
rpm-build 0f2925
  val make : int -> 'a -> 'a list
rpm-build 0f2925
    (** Similar to [String.make], [make n x] returns a
rpm-build 0f2925
      * list containing [n] elements [x].
rpm-build 0f2925
          *)
rpm-build 0f2925
rpm-build 0f2925
  val first : 'a list -> 'a
rpm-build 0f2925
  (** Returns the first element of the list, or raise [Empty_list] if
rpm-build 0f2925
   the list is empty (similar to [hd]). *)
rpm-build 0f2925
rpm-build 0f2925
  val last : 'a list -> 'a
rpm-build 0f2925
  (** Returns the last element of the list, or raise [Empty_list] if
rpm-build 0f2925
   the list is empty. This function takes linear time. *)
rpm-build 0f2925
rpm-build 0f2925
  val iteri : (int -> 'a -> unit) -> 'a list -> unit
rpm-build 0f2925
  (** [iteri f l] will call [(f 0 a0);(f 1 a1) ... (f n an)] where
rpm-build 0f2925
   [a0..an] are the elements of the list [l]. *)
rpm-build 0f2925
rpm-build 0f2925
  val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
rpm-build 0f2925
  (** [mapi f l] will build the list containing
rpm-build 0f2925
   [(f 0 a0);(f 1 a1) ... (f n an)] where [a0..an] are the elements of
rpm-build 0f2925
   the list [l]. *)
rpm-build 0f2925
rpm-build 0f2925
  val rfind : ('a -> bool) -> 'a list -> 'a
rpm-build 0f2925
  (** [rfind p l] returns the last element [x] of [l] such as [p x] returns
rpm-build 0f2925
   [true] or raises [Not_found] if such element as not been found. *)
rpm-build 0f2925
rpm-build 0f2925
  val find_exc : ('a -> bool) -> exn -> 'a list -> 'a
rpm-build 0f2925
  (** [find_exc p e l] returns the first element of [l] such as [p x]
rpm-build 0f2925
   returns [true] or raises [e] if such element as not been found. *)
rpm-build 0f2925
rpm-build 0f2925
  val findi : (int -> 'a -> bool) -> 'a list -> (int * 'a)
rpm-build 0f2925
  (** [findi p e l] returns the first element [ai] of [l] along with its
rpm-build 0f2925
   index [i] such that [p i ai] is true, or raises [Not_found] if no
rpm-build 0f2925
   such element has been found. *)
rpm-build 0f2925
rpm-build 0f2925
  val unique : ?cmp:('a -> 'a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
  (** [unique cmp l] returns the list [l] without any duplicate element.
rpm-build 0f2925
   Default comparator ( = ) is used if no comparison function specified. *)
rpm-build 0f2925
rpm-build 0f2925
  val filter_map : ('a -> 'b option) -> 'a list -> 'b list
rpm-build 0f2925
  (** [filter_map f l] call [(f a0) (f a1).... (f an)] where [a0..an] are
rpm-build 0f2925
   the elements of [l]. It returns the list of elements [bi] such as
rpm-build 0f2925
   [f ai = Some bi] (when [f] returns [None], the corresponding element of
rpm-build 0f2925
   [l] is discarded). *)
rpm-build 0f2925
rpm-build 0f2925
  val find_map : ('a -> 'b option) -> 'a list -> 'b
rpm-build 0f2925
  (** [find_map pred list] finds the first element of [list] for which
rpm-build 0f2925
      [pred element] returns [Some r].  It returns [r] immediately
rpm-build 0f2925
      once found or raises [Not_found] if no element matches the
rpm-build 0f2925
      predicate.  See also {!filter_map}. *)
rpm-build 0f2925
rpm-build 0f2925
  val split_nth : int -> 'a list -> 'a list * 'a list
rpm-build 0f2925
  (** [split_nth n l] returns two lists [l1] and [l2], [l1] containing the
rpm-build 0f2925
   first [n] elements of [l] and [l2] the others. Raise [Invalid_index] if
rpm-build 0f2925
   [n] is outside of [l] size bounds. *)
rpm-build 0f2925
rpm-build 0f2925
  val remove : 'a list -> 'a -> 'a list
rpm-build 0f2925
  (** [remove l x] returns the list [l] without the first element [x] found
rpm-build 0f2925
   or returns  [l] if no element is equal to [x]. Elements are compared
rpm-build 0f2925
   using ( = ). *)
rpm-build 0f2925
rpm-build 0f2925
  val remove_if : ('a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
  (** [remove_if cmp l] is similar to [remove], but with [cmp] used
rpm-build 0f2925
   instead of ( = ). *)
rpm-build 0f2925
rpm-build 0f2925
  val remove_all : 'a list -> 'a -> 'a list
rpm-build 0f2925
  (** [remove_all l x] is similar to [remove] but removes all elements that
rpm-build 0f2925
   are equal to [x] and not only the first one. *)
rpm-build 0f2925
rpm-build 0f2925
  val take : int -> 'a list -> 'a list
rpm-build 0f2925
  (** [take n l] returns up to the [n] first elements from list [l], if
rpm-build 0f2925
   available. *)
rpm-build 0f2925
rpm-build 0f2925
  val drop : int -> 'a list -> 'a list
rpm-build 0f2925
  (** [drop n l] returns [l] without the first [n] elements, or the empty
rpm-build 0f2925
   list if [l] have less than [n] elements. *)
rpm-build 0f2925
rpm-build 0f2925
  val takewhile : ('a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
    (** [takewhile f xs] returns the first elements of list [xs]
rpm-build 0f2925
        which satisfy the predicate [f]. *)
rpm-build 0f2925
rpm-build 0f2925
  val dropwhile : ('a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
    (** [dropwhile f xs] returns the list [xs] with the first
rpm-build 0f2925
        elements satisfying the predicate [f] dropped. *)
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Enum functions} *)
rpm-build 0f2925
rpm-build 0f2925
  (** Enumerations are important in ExtLib, they are a good way to work with
rpm-build 0f2925
   abstract enumeration of elements, regardless if they are located in a list,
rpm-build 0f2925
   an array, or a file. *)
rpm-build 0f2925
rpm-build 0f2925
  val enum : 'a list -> 'a Enum.t
rpm-build 0f2925
  (** Returns an enumeration of the elements of a list. *)
rpm-build 0f2925
rpm-build 0f2925
  val of_enum : 'a Enum.t -> 'a list
rpm-build 0f2925
  (** Build a list from an enumeration. *)
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Compatibility functions} *)
rpm-build 0f2925
rpm-build 0f2925
  val cons : 'a -> 'a list -> 'a list
rpm-build 0f2925
rpm-build 0f2925
  val assoc_opt : 'a -> ('a * 'b) list -> 'b option
rpm-build 0f2925
  val assq_opt : 'a -> ('a * 'b) list -> 'b option
rpm-build 0f2925
rpm-build 0f2925
  val find_opt : ('a -> bool) -> 'a list -> 'a option
rpm-build 0f2925
  val nth_opt : 'a list -> int -> 'a option
rpm-build 0f2925
rpm-build 0f2925
  val compare_lengths : 'a list -> 'b list -> int
rpm-build 0f2925
  val compare_length_with : 'a list -> int -> int
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Modified functions} *)
rpm-build 0f2925
rpm-build 0f2925
  (** Some minor modifications have been made to the specification of some
rpm-build 0f2925
   functions, especially concerning exceptions raised. *)
rpm-build 0f2925
rpm-build 0f2925
  val hd : 'a list -> 'a
rpm-build 0f2925
  (** Returns the first element of the list or raise [Empty_list] if the
rpm-build 0f2925
   list is empty. *)
rpm-build 0f2925
rpm-build 0f2925
  val tl : 'a list -> 'a list
rpm-build 0f2925
  (** Returns the list without its first elements or raise [Empty_list] if
rpm-build 0f2925
   the list is empty. *)
rpm-build 0f2925
rpm-build 0f2925
  val nth : 'a list -> int -> 'a
rpm-build 0f2925
  (** [nth l n] returns the n-th element of the list [l] or raise
rpm-build 0f2925
   [Invalid_index] is the index is outside of [l] bounds. *)
rpm-build 0f2925
rpm-build 0f2925
  val sort : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list
rpm-build 0f2925
  (** Sort the list using optional comparator (by default [compare]). *)
rpm-build 0f2925
rpm-build 0f2925
  (** The following functions have been improved so all of them are
rpm-build 0f2925
   tail-recursive. They have also been modified so they no longer
rpm-build 0f2925
   raise [Invalid_arg] but [Different_list_size] when used on two
rpm-build 0f2925
   lists having a different number of elements. *)
rpm-build 0f2925
rpm-build 0f2925
  val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
rpm-build 0f2925
  val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
rpm-build 0f2925
  val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
rpm-build 0f2925
  val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
rpm-build 0f2925
  val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
rpm-build 0f2925
  val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
rpm-build 0f2925
  val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
rpm-build 0f2925
  val combine : 'a list -> 'b list -> ('a * 'b) list
rpm-build 0f2925
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Improved functions} *)
rpm-build 0f2925
rpm-build 0f2925
  (** The following functions have the same behavior as the [List]
rpm-build 0f2925
    module ones but are tail-recursive. That means they will not
rpm-build 0f2925
    cause a [Stack_overflow] when used on very long list.
rpm-build 0f2925
rpm-build 0f2925
    The implementation might be a little more slow in bytecode,
rpm-build 0f2925
    but compiling in native code will not affect performances. *)
rpm-build 0f2925
rpm-build 0f2925
  val map : ('a -> 'b) -> 'a list -> 'b list
rpm-build 0f2925
  val append : 'a list -> 'a list -> 'a list
rpm-build 0f2925
  val flatten : 'a list list -> 'a list
rpm-build 0f2925
  val concat : 'a list list -> 'a list
rpm-build 0f2925
  val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
rpm-build 0f2925
  val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
rpm-build 0f2925
  val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
rpm-build 0f2925
  val split : ('a * 'b) list -> 'a list * 'b list
rpm-build 0f2925
rpm-build 0f2925
  (** The following functions were already tail-recursive in the [List]
rpm-build 0f2925
    module but were using [List.rev] calls. The new implementations
rpm-build 0f2925
    have better performances. *)
rpm-build 0f2925
rpm-build 0f2925
  val filter : ('a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
  val find_all : ('a -> bool) -> 'a list -> 'a list
rpm-build 0f2925
  val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Older functions} *)
rpm-build 0f2925
rpm-build 0f2925
  (** These functions are already part of the Ocaml standard library
rpm-build 0f2925
    and have not been modified. Please refer to the Ocaml Manual for
rpm-build 0f2925
    documentation. *)
rpm-build 0f2925
rpm-build 0f2925
  val length : 'a list -> int
rpm-build 0f2925
  val rev_append : 'a list -> 'a list -> 'a list
rpm-build 0f2925
  val rev : 'a list -> 'a list
rpm-build 0f2925
  val rev_map : ('a -> 'b) -> 'a list -> 'b list
rpm-build 0f2925
  val iter : ('a -> unit) -> 'a list -> unit
rpm-build 0f2925
  val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a list -> 'b
rpm-build 0f2925
  val for_all : ('a -> bool) -> 'a list -> bool
rpm-build 0f2925
  val exists : ('a -> bool) -> 'a list -> bool
rpm-build 0f2925
  val find : ('a -> bool) -> 'a list -> 'a
rpm-build 0f2925
rpm-build 0f2925
  val mem : 'a -> 'a list -> bool
rpm-build 0f2925
  val memq : 'a -> 'a list -> bool
rpm-build 0f2925
  val assoc : 'a -> ('a * 'b) list -> 'b
rpm-build 0f2925
  val assq : 'a -> ('a * 'b) list -> 'b
rpm-build 0f2925
  val mem_assoc : 'a -> ('a * 'b) list -> bool
rpm-build 0f2925
  val mem_assq : 'a -> ('a * 'b) list -> bool
rpm-build 0f2925
rpm-build 0f2925
rpm-build 0f2925
  val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
rpm-build 0f2925
  val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
rpm-build 0f2925
  val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
rpm-build 0f2925
rpm-build 0f2925
#if OCAML >= 402
rpm-build 0f2925
  val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list
rpm-build 0f2925
  (** Same as {!List.sort}, but also remove duplicates.
rpm-build 0f2925
      @since 4.02.0 *)
rpm-build 0f2925
#endif
rpm-build 0f2925
rpm-build 0f2925
#if OCAML >= 407
rpm-build 0f2925
  (** [*_seq] functions were introduced in OCaml 4.07.0, and are _not_ implemented in extlib for older OCaml versions *)
rpm-build 0f2925
  val to_seq : 'a list -> 'a Seq.t
rpm-build 0f2925
  val of_seq : 'a Seq.t -> 'a list
rpm-build 0f2925
#endif
rpm-build 0f2925
rpm-build 0f2925
  (** {6 Exceptions} *)
rpm-build 0f2925
rpm-build 0f2925
  exception Empty_list
rpm-build 0f2925
  (** [Empty_list] is raised when an operation applied on an empty list
rpm-build 0f2925
    is invalid : [hd] for example. *)
rpm-build 0f2925
rpm-build 0f2925
  exception Invalid_index of int
rpm-build 0f2925
  (** [Invalid_index] is raised when an indexed access on a list is
rpm-build 0f2925
    out of list bounds. *)
rpm-build 0f2925
rpm-build 0f2925
  exception Different_list_size of string
rpm-build 0f2925
  (** [Different_list_size] is raised when applying functions such as
rpm-build 0f2925
    [iter2] on two lists having different size. *)
rpm-build 0f2925
rpm-build 0f2925
rpm-build 0f2925
end
rpm-build 0f2925
rpm-build 0f2925
val ( @ ) : 'a list -> 'a list -> 'a list
rpm-build 0f2925
(** the new implementation for ( @ ) operator, see [List.append]. *)