## cool / src / lib / CoolUtils.ml @ 7c4d2eb4

History | View | Annotate | Download (1.68 KB)

1 | |
---|---|

2 |
module TArray = struct |

3 |
let any f arr = |

4 |
let rec g idx = |

5 |
if idx >= Array.length arr then false |

6 |
else if f arr.(idx) then true |

7 |
else g (idx + 1) |

8 |
in g 0 |

9 |
let all f arr = not (any (fun x -> not (f x)) arr) |

10 |
let elem (x: 'a) (arr: 'a array) = |

11 |
any (fun y -> x == y) arr |

12 |
let included sub sup = all (fun x -> elem x sup) sub |

13 |
let to_string ts arr = "[| " ^ (Array.fold_right (fun i str -> (ts i) ^" "^ str) arr "|]") |

14 |
let uniq arr = |

15 |
let combine n l = |

16 |
match l with |

17 |
| [] -> [n] |

18 |
| m::tail -> if n == m then l else n::l |

19 |
in |

20 |
Array.of_list (Array.fold_right combine arr []) |

21 |
end |

22 | |

23 |
module TList = struct |

24 |
let rec zip l1 l2 = match l1 with |

25 |
| h1::t1 -> (match l2 with |

26 |
| h2::t2 -> (h1,h2)::(zip t1 t2) |

27 |
| [] -> []) |

28 |
| [] -> [] |

29 |
let rec powerset l = match l with |

30 |
| [] -> [[]] |

31 |
| h::t -> let pt = powerset t in |

32 |
let oth = List.map (fun x -> h::x) pt in |

33 |
List.append pt oth |

34 |
let prod la lb = List.concat (List.map (fun a -> List.map (fun b -> (a,b)) lb) la) |

35 |
let empty l = match l with |

36 |
| (_::_) -> false |

37 |
| [] -> true |

38 |
end |

39 | |

40 |
let agents = ref ([|1;2;3;4;5|]) |

41 | |

42 |
let cl_get_agents () = !agents |

43 |
let cl_set_agents arr = ignore ( |

44 |
agents := arr; |

45 |
Array.fast_sort compare !agents; |

46 |
agents := TArray.uniq !agents) |

47 | |

48 | |

49 |
let intlist_of_string str = List.map int_of_string (Str.split (Str.regexp "[ \t,]+") str) |

50 | |

51 |
let compose f g x = f (g (x)) |

52 |
let flip f y x = f x y |

53 |
let eval a f = f a |

54 |
let id a = a |

55 |