<commit_before>(*
 * Option - functions for the option type
 * Copyright (C) 2003 <NAME>
 *               2008 <NAME> (Contributor)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
exception No_value

type 'a t = 'a option

let may f = function
	| None -> ()
	| Some v -> f v

let map f = function
	| None -> None
	| Some v -> Some (f v)

let bind f = function
  | None -> None
  | Some v -> f v

let apply = function
  | None -> (fun x -> x)
  | Some f -> f

let default v = function
	| None -> v
	| Some v -> v

let is_some = function
	| None -> false
	| _ -> true

let is_none = function
	| None -> true
	| _ -> false

let get_exn s e = match s with
        | None   -> raise e
	| Some v -> v

let get s = get_exn s Not_found

let map_default f v = function
	| None -> v
	| Some v2 -> f v2

let compare ?(cmp=Pervasives.compare) a b = match a with
    None -> (match b with
      None -> 0
    | Some _ -> -1)
  | Some x -> (match b with
      None -> 1
    | Some y -> cmp x y)

let eq ?(eq=(=)) x y = match x,y with 
  | None, None -> true 
  | Some a, Some b -> eq a b
  | _ -> false

(**T eq
   eq ~eq:(fun a b -> (a land 1) = (b land 1)) (Some 1) (Some 3)
   eq (Some 3) (None) = false
   eq None None = true
**)

let enum = function
        | None   -> BatEnum.from (fun () -> raise BatEnum.No_more_elements)
        | Some e -> BatEnum.singleton e

let of_enum = BatEnum.get

let print print_a out = function
  | None   -> BatInnerIO.nwrite out "None"
  | Some x -> BatPrintf.fprintf out "Some %a" print_a x

let t_printer a_printer paren out = function
  | Some x ->
      if paren then
        BatIO.write out '(';
      BatIO.nwrite out "Some ";
      a_printer true out x;
      if paren then
        BatIO.write out ')';
  | None ->
      BatIO.nwrite out "None"

let maybe_printer a_printer paren out = function
  | None -> ()
  | Some x -> a_printer paren out x

module Monad =<commit_msg>tests for batOption.ml<commit_after>(*
 * Option - functions for the option type
 * Copyright (C) 2003 <NAME>
 *               2008 <NAME> (Contributor)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
exception No_value

type 'a t = 'a option

let may f = function
	| None -> ()
	| Some v -> f v
(**T may
   let x = ref 3 in may incr (Some x); !x = 4
**)


let map f = function
	| None -> None
	| Some v -> Some (f v)
(**T map
   map succ None = None
   map succ (Some 3) = (Some 4)
**)


let bind f = function
  | None -> None
  | Some v -> f v
(**T bind
   bind (fun s -> Some s) None = None
   bind (fun s -> Some s) (Some ()) = Some ()
**)


let apply = function
  | None -> (fun x -> x)
  | Some f -> f
(**T apply
   apply None 3 = 3
   apply (Some succ) 3 = 4
 **)

let default v = function
	| None -> v
	| Some v -> v
(**T default
   default 3 None = 3
   default 3 (Some 4) = 4
 **)

let is_some = function
	| None -> false
	| _ -> true
(**T is_some
   not (is_some None)
   is_some (Some ())
 **)

let is_none = function
	| None -> true
	| _ -> false
(**T is_none
   is_none None
   not (is_none (Some ()))
 **)

let get_exn s e = match s with
        | None   -> raise e
	| Some v -> v
(**T get_exn
   try get_exn None Exit with Exit -> true
   try get_exn (Some true) Exit with Exit -> false
**)

let get s = get_exn s Not_found
(**T get
   try get None with Not_found -> true
   try get (Some true) with Not_found -> false
 **)

let map_default f v = function
	| None -> v
	| Some v2 -> f v2
(**T map_default
   map_default succ 2 None = 2
   map_default succ 2 (Some 3) = 4
**)

let compare ?(cmp=Pervasives.compare) a b = match a with
    None -> (match b with
      None -> 0
    | Some _ -> -1)
  | Some x -> (match b with
      None -> 1
    | Some y -> cmp x y)
(**T compare
   compare (Some 0) (Some 1) < 0
   compare (Some 0) (Some 0) = 0
   compare (Some 0) (Some (-1)) > 0
   compare None (Some ()) < 0
   compare None None = 0
   compare (Some ()) None > 0
   compare ~cmp:(fun _ _ -> 0) (Some (fun x -> x)) (Some (fun y -> y)) = 0
**)


let eq ?(eq=(=)) x y = match x,y with 
  | None, None -> true 
  | Some a, Some b -> eq a b
  | _ -> false

(**T eq
   eq ~eq:(fun a b -> (a land 1) = (b land 1)) (Some 1) (Some 3)
   eq (Some 3) (None) = false
   eq None None = true
**)

let enum = function
        | None   -> BatEnum.from (fun () -> raise BatEnum.No_more_elements)
        | Some e -> BatEnum.singleton e
(**T enum 
   BatList.of_enum (enum None) = []
   BatList.of_enum (enum (Some 3)) = [3]
**)

let of_enum = BatEnum.get
(**T of_enum
   of_enum (BatList.enum []) = None
   let e = BatList.enum [1; 2; 3] in of_enum e = Some 1 && BatList.of_enum e = [2; 3]
**)

let print print_a out = function
  | None   -> BatInnerIO.nwrite out "None"
  | Some x -> BatPrintf.fprintf out "Some %a" print_a x

let t_printer a_printer paren out = function
  | Some x ->
      if paren then
        BatIO.write out '(';
      BatIO.nwrite out "Some ";
      a_printer true out x;
      if paren then
        BatIO.write out ')';
  | None ->
      BatIO.nwrite out "None"

let maybe_printer a_printer paren out = function
  | None -> ()
  | Some x -> a_printer paren out x

module Monad =
{'fraction_non_alphanumeric': 0.11551204819277108, 'fraction_numerical': 0.016265060240963854, 'mean_word_length': 2.4462895692786715, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 94, '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': '3575827', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2217, 'n_words': 970}