Move memoize to the Aoc library.
This commit is contained in:
@@ -90,3 +90,11 @@ let pow10 n =
|
||||
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
|
||||
assert (n >= 0);
|
||||
impl 1 n
|
||||
|
||||
let memoize memo f value =
|
||||
match Hashtbl.find_opt memo value with
|
||||
| Some x -> x
|
||||
| None ->
|
||||
let x = f value in
|
||||
Hashtbl.add memo value x;
|
||||
x
|
||||
|
@@ -30,6 +30,11 @@ val main : (string -> 'a) -> (('b -> string) * ('a -> 'b)) list -> unit
|
||||
[string_of_int]). The second executes the given part. Output is given as if
|
||||
done by: [print_string ( prep fname |> snd |> fst )] *)
|
||||
|
||||
val memoize : ('a, 'b) Hashtbl.t -> ('a -> 'b) -> 'a -> 'b
|
||||
(** [memoize memo f value] returns the result of [f value]. The hashtable [memo]
|
||||
is used to cache results, so repeated calls with the same [value] will not
|
||||
call [f] again. *)
|
||||
|
||||
(** Module representing a pair of integers, useful for Set.Make *)
|
||||
module IntPair : sig
|
||||
type t = int * int
|
||||
|
Reference in New Issue
Block a user