Created
February 6, 2016 15:33
-
-
Save jingkaihe/f27343808cc8c5e61814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let lengths row = | |
List.map row ~f:String.length | |
let max_width l1 l2 = | |
List.map2_exn l1 l2 ~f:Int.max | |
;; | |
let widths header rows = | |
List.fold rows | |
~init:(lengths header) | |
~f:(fun acc row -> | |
max_width acc (lengths row) | |
) | |
;; | |
let banner width = | |
let banners = List.map width ~f:(fun len -> String.make len '-') in | |
"|" ^ String.concat banners ~sep:"+" ^ "|\n" | |
;; | |
let line words width = | |
let padded_words = List.map2_exn words width | |
~f:(fun word width -> word ^ (String.make (width - String.length word) ' ')) | |
in | |
"|" ^ String.concat padded_words ~sep:"|" ^ "|\n" | |
;; | |
let render_table header rows = | |
let width = widths header rows in | |
let banner = banner width in | |
let lines = List.map rows | |
~f:(fun words -> line words width) | |
in | |
String.concat lines ~sep:banner | |
;; | |
printf "%s\n" (render_table ["language";"architect";"first release"] [ ["Lisp" ;"John McCarthy" ;"1958"] ; | |
["C" ;"Dennis Ritchie";"1969"] ; | |
["ML" ;"Robin Milner" ;"1973"] ; | |
["OCaml";"Xavier Leroy" ;"1996"] ; | |
]) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment