tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

splitLines.scp (936B)


      1 !export splitLines
      2 -- Split LF-delimited character/byte lists into a list of line lists.
      3 -- Input: source list; initial state: ([], reverser worker descriptor).
      4 -- Reverser is the public `reverse` configured with `(fold, prepend)`.
      5 -- We accumulate each line in reverse, prepend each byte in constant time,
      6 -- and reverse it only at LF or at a nonempty unterminated EOF.
      7 -- Empty input => []; "a\n" => ["a"]; "\n" => [""];
      8 -- "a\n\n" => ["a" ""]. CR is retained as ordinary content (not CRLF-aware).
      9 -- No implicit decoding, trimming, or maximum-line-length guarantee.
     10 consumer splitLines
     11   []            @ ([], reverser)      => [] -- no extra line after trailing LF
     12   []            @ (pending, reverser) => call reverser pending as line in [line | []]
     13   [byte | rest] @ (pending, reverser) => choose equal byte 10
     14     call reverser pending as line in [line | recur rest @ ([], reverser)]
     15     recur rest @ ([byte | pending], reverser)