From ed2a97502cb825877562c05881da4fb2e8eecad6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 8 Aug 2021 16:55:37 +0530 Subject: refactor --- src/marker.ml | 36 ++++++++++++++++++++++++++++++++++++ src/utils.ml | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/marker.ml create mode 100644 src/utils.ml (limited to 'src') diff --git a/src/marker.ml b/src/marker.ml new file mode 100644 index 0000000..19b1be0 --- /dev/null +++ b/src/marker.ml @@ -0,0 +1,36 @@ +module type Marker = sig + type t + val of_string : string -> t + val pp : out_channel -> t -> unit +end + +module Marker = struct + type t = Point + | Pixel + | Circle + | TriangleDown + | TriangleUp + | TriangleLeft + | TriangleRight + | Custom of string + + exception Invalid_marker_length + let of_string s = + if String.length s <> 1 + then raise Invalid_marker_length + else Custom(s) + + let pp channel n = + (match n with + | Point -> "." + | Pixel -> "," + | Circle -> "o" + | TriangleDown -> "v" + | TriangleUp -> "^" + | TriangleLeft -> ">" + | TriangleRight -> "<" + | Custom(s) -> s) + |> Printf.fprintf channel "%s" + +end + diff --git a/src/utils.ml b/src/utils.ml new file mode 100644 index 0000000..8d1094e --- /dev/null +++ b/src/utils.ml @@ -0,0 +1,36 @@ +module Utils = struct + exception Empty_list + let fold fn = function + | [] -> raise Empty_list + | head :: tail -> List.fold_left fn head tail + + let list_min ls = fold Stdlib.min ls + let list_max ls = fold Stdlib.max ls + + let rec repeat n a = + if n <= 0 + then [] + else a :: repeat (n - 1) a + + let empty ls = (ls = []) + + let rec zip l1 l2 = + match (l1, l2) with + | ([], _) -> [] + | (_, []) -> [] + | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2 + + let rec transpose = function + | [] :: _ -> [] + | ls -> List.(map hd ls) :: transpose List.(map tl ls) + + let flip f a b = f b a + + let rot_right ls = List.(rev ls |> transpose) + let rot_left ls = List.(transpose ls |> rev) + + let saturating_sub b a = + if a < b then 0 + else a - b +end + -- cgit v1.2.3