summaryrefslogtreecommitdiff
path: root/ocaml/lib/application/parser.ml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-06-26 18:04:09 +0200
committerDJ O'Leary <dijitol@proton.me>2026-06-26 18:04:09 +0200
commit6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (patch)
tree17c90d3984c114aa023600cc5bec6f6004b02243 /ocaml/lib/application/parser.ml
parentad4ea5ca4bc9f7540ee1d6a4fa2d881709dc6459 (diff)
refactor!: invert repo directory structure
now is language first
Diffstat (limited to 'ocaml/lib/application/parser.ml')
-rw-r--r--ocaml/lib/application/parser.ml36
1 files changed, 36 insertions, 0 deletions
diff --git a/ocaml/lib/application/parser.ml b/ocaml/lib/application/parser.ml
new file mode 100644
index 0000000..099900b
--- /dev/null
+++ b/ocaml/lib/application/parser.ml
@@ -0,0 +1,36 @@
+(** [rows_of_string s] is a list of strings where each element is a row of [s]. *)
+let rows_of_string s =
+ s |> String.split_on_char '\n'
+ |> List.filter_map (fun l ->
+ let trimmed = String.trim l in
+ if trimmed <> "" then Some trimmed else None )
+
+(** [columns_of_string ?sep s] is a list of strings where each element is
+ a column of [s] where each column is separated by [sep]. *)
+let rec columns_of_string ?(sep = ' ') s =
+ rows_of_string s
+ |> List.map (fun l ->
+ l |> String.split_on_char sep |> List.filter (fun l -> l <> "") )
+ |> transpose
+
+(** [group_columns lst] is a list where the first element of each
+ sublist of [lst] is grouped in [acc] and so on for the rest of the elements.
+ Example: [group_columns [] \[\["abc"; "def"\]; \["ghi"; "jkl"\]\]] is the list [\[\["abc";"ghi"\];\["def";"jkl"\]\]]. *)
+and transpose lst =
+ assert (lst <> []) ;
+ if List.mem [] lst then []
+ else List.map List.hd lst :: transpose (List.map List.tl lst)
+
+let%expect_test "rows correctly parsed" =
+ let have = {| abc def
+ ghi jkl |} in
+ let rows = rows_of_string have in
+ List.iter (fun c -> print_string (c ^ " ")) rows ;
+ [%expect {| abc def ghi jkl |}]
+
+let%expect_test "columns correctly parsed" =
+ let have = {| abc def
+ ghi jkl |} in
+ let cols = columns_of_string have in
+ List.iter (fun row -> List.iter (fun c -> print_string (c ^ " ")) row) cols ;
+ [%expect {| abc ghi def jkl |}]