<commit_before>;; Pretty printing
;; Let's start by trying out Wadler's "A prettier printer".

;; Document datatypes
;; ()
;; (string @doc)
;; (int @doc)

(let nil       '())
(to (text str) `(,str))
(let line      '(0))
(to (<> @docs) (call chain docs))

(to (nest i doc)
  (match doc
    (()                     '())
    (((: s string?) @rest)  `(,s ,@(nest i rest)))
    (((: j integer?) @rest) `(,(+ i j) ,@(nest i rest)))))

(to (lay-out doc)
  (match doc
    (() "")
    (((: s string?) @rest)
     (chain s (lay-out rest)))
    (((: i integer?) @rest)
     (chain "\n" (" " .repeat i) (lay-out rest)))))

(to (pp sexpr)
  (match sexpr
    (((: s symbol?) _ @_)
     (<> (text "(")
         (nest 1 (<> (text s.name) (text " ")
                     (nest (+ s.name.count 1)
                           (call <> (intercalate line (each pp sexpr.rest))))))
         (text ")")))
    ((: list?)
     (<> (text "(")
         (nest 1 (call <> (intercalate line (each pp sexpr))))
         (text ")")))
    (_ (text ("~w" .format sexpr)))))

(to (intercalate between elements)      ;TODO unify with .join in stdlib
  (if elements.empty?
      elements
      `(,elements.first
        ,@(for gather ((x elements.rest))
            `(,between ,x)))))

(let eg1
  '(to (fact n)
     (if (= n 0) 1 (* n (fact (- n 1))))))

(to (main _)
  (display (lay-out (pp eg1)))
  (newline))

(export
  lay-out nil <> text line nest
  pp)
<commit_msg>rename<commit_after>;; Pretty printing
;; Let's start by trying out Wadler's "A prettier printer".

;; Document datatypes
;; ()
;; (string @doc)
;; (int @doc)

(let nil       '())
(to (text str) `(,str))
(let line      '(0))
(to (<> @docs) (call chain docs))

(to (nest i doc)
  (match doc
    (()                     '())
    (((: s string?) @rest)  `(,s ,@(nest i rest)))
    (((: j integer?) @rest) `(,(+ i j) ,@(nest i rest)))))

(to (lay-out doc)
  (match doc
    (() "")
    (((: s string?) @rest)
     (chain s (lay-out rest)))
    (((: i integer?) @rest)
     (chain "\n" (" " .repeat i) (lay-out rest)))))

(to (pp sexpr)
  (match sexpr
    (((: first symbol?) _ @_)
     (<> (text "(")
         (nest 1 (<> (text first.name) (text " ")
                     (nest (+ first.name.count 1)
                           (call <> (intercalate line (each pp sexpr.rest))))))
         (text ")")))
    ((: list?)
     (<> (text "(")
         (nest 1 (call <> (intercalate line (each pp sexpr))))
         (text ")")))
    (_ (text ("~w" .format sexpr)))))

(to (intercalate between elements)      ;TODO unify with .join in stdlib
  (if elements.empty?
      elements
      `(,elements.first
        ,@(for gather ((x elements.rest))
            `(,between ,x)))))

(let eg1
  '(to (fact n)
     (if (= n 0) 1 (* n (fact (- n 1))))))

(to (main _)
  (display (lay-out (pp eg1)))
  (newline))

(export
  lay-out nil <> text line nest
  pp)

{'fraction_non_alphanumeric': 0.2222222222222222, 'fraction_numerical': 0.006269592476489028, 'mean_word_length': 2.1251360174102287, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4970342', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1045, 'n_words': 318}