Skip to content

Instantly share code, notes, and snippets.

@emillon
Created August 31, 2018 12:02
Show Gist options
  • Save emillon/e1a1b660c5308f2bfeffbad6840b456b to your computer and use it in GitHub Desktop.
Save emillon/e1a1b660c5308f2bfeffbad6840b456b to your computer and use it in GitHub Desktop.
(* Step 1: 'a list -> bool everywhere. *)
(* Step 2: Wrap in a newtype: Finder of 'a list -> bool *)
(* Step 3: abstract in a module with this signature *)
module type LIST_FINDER = sig
type 'a t
val this_element : 'a -> 'a t
val find : 'a t -> 'a list -> bool
end
(* And this implementation *)
module Before : LIST_FINDER = struct
type 'a t = Finder of ('a list -> bool)
let this_element x = Finder (List.mem x)
let find (Finder f) l = f l
end
(* Step 4: invert constructor and deconstructor *)
module After : LIST_FINDER = struct
type 'a t = 'a
let this_element x = x
let find x l = List.mem x l
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment