Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created April 11, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/f495c1b1a0fa02e6b5be to your computer and use it in GitHub Desktop.
Save podhmo/f495c1b1a0fa02e6b5be to your computer and use it in GitHub Desktop.
open Core_kernel.Std
module type Itemize = sig
val dump : string list -> string
end
module HTML : Itemize = struct
let dump xs =
let buf = Buffer.create 0 in
let put_item x =
Buffer.add_string buf "<li>";
Buffer.add_string buf x;
Buffer.add_string buf "<li>";
in
Buffer.add_string buf "<ul>";
List.iter xs ~f:put_item;
Buffer.add_string buf "</ul>";
Buffer.contents buf
end
module Text : Itemize = struct
let dump xs =
let buf = Buffer.create 0 in
let put_item x =
Buffer.add_string buf "- ";
Buffer.add_string buf x;
Buffer.add_string buf "\n";
in
List.iter xs ~f:put_item;
Buffer.contents buf
end
let printer title content ((module M): (module Itemize)) =
Printf.printf "** %s **\n" (title);
print_string @@ M.dump content
let main title flag =
let xs = ["foo"; "bar"; "boo";] in
match flag with
| "html" | "HTML" -> printer title xs (module HTML)
| _ -> printer title xs (module Text)
let () =
main Sys.argv.(1) Sys.argv.(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment