Nested vectors are implemented, including parser and class nstio.
[Faustine.git] / interpreter / aux.ml
1 (**
2 Module: Aux
3 Description: all auxiliary functions
4 @author WANG Haisheng
5 Created: 12/08/2013 Modified: 13/08/2013
6 *)
7
8 let array_map2 = fun f -> fun a -> fun b ->
9 let n1 = Array.length a in
10 let n2 = Array.length b in
11 if n1 = n2 then Array.init n1 (fun i -> f a.(i) b.(i))
12 else raise (Invalid_argument "Array.map2 size not matched.");;
13
14 let array_map3 = fun f -> fun a -> fun b -> fun c ->
15 let n1 = Array.length a in
16 let n2 = Array.length b in
17 let n3 = Array.length c in
18 if n1 = n2 && n1 = n3 then Array.init n1 (fun i -> f a.(i) b.(i) c.(i))
19 else raise (Invalid_argument "Array.map3 size not matched.");;
20
21 let decorate = fun s -> " Faustine -> " ^ s ^ "\n";;
22
23 let xor : bool -> bool -> bool =
24 fun a -> fun b -> (a || b) && (not (a && b));;
25
26 let rint : float -> float =
27 fun f ->
28 if (f -. (floor f)) >= 0.5 then ceil f
29 else floor f;;
30
31 let remainder_float : float -> float -> float =
32 fun f1 -> fun f2 ->
33 let r = mod_float f1 f2 in
34 if (abs_float r) > ((abs_float f2) /. 2.) then
35 (if r *. f2 > 0. then (r -. f2) else (r +. f2))
36 else r;;
37
38 let format_of_file : string -> string =
39 fun (path : string) ->
40 let fragments = Str.split (Str.regexp "\.") path in
41 let n = List.length fragments in
42 List.nth fragments (n - 1);;
43
44 let transpose : int -> 'a array array -> 'a array array =
45 fun width -> fun matrix ->
46 let get_element = fun i -> fun array -> array.(i) in
47 let get_column = fun m -> fun i -> Array.map (get_element i) m in
48 Array.init width (get_column matrix);;