4 ms·
Regardless of how awesome this may be, I cannot get over how there are 6 (!) Levels of nesting in the code sample. I hope it's simply for the sake of keeping th
by andyg_blog 3y ago
Regardless of how awesome this may be, I cannot get over how there are 6 (!) Levels of nesting in the code sample. I hope it's simply for the sake of keeping the sample short, but this kind of "Chevron programming" is generally hard to read.
- beckford 3y agoThat's fair. Assuming I introduced a new macro "compactexpr" it could have been written: [%%compactexpr "SumArStaged"; [ arr; n ]; let sum = ref 0 in for i = 0 to 3 do for j = i to min (i + 3) (n - 1) do sum := !sum + arr.(j) done done; !sum] ;; print_c "sum_ar" (module SumArConv) SumArStaged.expr;; So that has the exact indent level of the C code. (Details: The above is formatted using the "conventional" profile for the "ocamlformat" tool) Of course, there is a disadvantage. I had to use global variables in the above "compactexpr". Global variables mimic the C language (ie. C functions are globally-scoped or file-scoped functions) but wouldn't be a good fit when translating that code to functional languages. Additionally, the OCaml idiom `fun arr n ->` was lost, but that seems minor. Do you and others find the above readable? (If so, perhaps it would be best to give some flexibility for the coding style.)