<commit_before>(import [hy.models.string [HyString]]
        [hy.models.symbol [HySymbol]]
        [hy.models.list [HyList]]
        [hy.models.keyword [HyKeyword]]
        [hy.models.expression [HyExpression]])

(defclass MatchFailure [Exception] [])

(defn match-failed []
  (raise (MatchFailure "match failed")))

(defn try-func [f]
  (try
    (do (f) true)
    (catch [] false)))

(defmacro accfor [args &rest body]
  (def names (slice args 0 nil 2))
  `(genexpr ((fn [~@names] ~@body) ~@names) [~@args]))

(defmacro defunion [name &rest types]
  (setv base `(defclass ~name [object] []))
  (setv classes (accfor [t types]
    (setv fields (HyList (slice t 1)))
    (setv field-slist (HyList (map HyString fields)))
    (defn mk-fmstr [s]
      (HyString (.join ", " (accfor [f fields] (% "%s=%%%s" (, f s))))))
    (setv field-sfmstr (mk-fmstr "s"))
    (setv field-rfmstr (mk-fmstr "r"))
    (setv sname (HyString (get t 0)))
    (defn mk-fmfn [v]
      `(% "%s(%s)" (, ~sname (% ~v (tuple self.-fields)))))
    `(defclass ~(get t 0) [~name]
      [[--init-- (fn [self ~@fields]
                  (for [x (zip ~field-slist ~fields)]
                    (setattr self (get x 0) (get x 1)))
                  (setv self.-fields ~fields)
                  nil
                 )]
       [--str-- (fn [self] ~(mk-fmfn field-sfmstr))]
       [--repr-- (fn [self] ~(mk-fmfn field-sfmstr))]])))
  (setv result (list (+ [base] (list classes))))
  `(do ~@result nil))

(defmacro match [x &rest branches]
  (defn get-tp [p]
    (cond
      [(isinstance p HyExpression)
        (if (= (get p 0) `,) "tupl-match" "data-match")]
      [(isinstance p HySymbol)
        (if (= p `_) "fallthough" "test-value")]
      [(isinstance p HyList) "list-match"]
      [(isinstance p HyKeyword) "grap-value"]
      [true "test-value"]))

  (defn map-fields [func var p]
    (setv res [])
    (for [[i x] (enumerate p)]
      (if (= x (HySymbol "..."))
        (break))
      (res.append (func `(get ~var ~(HyInteger i)) x)))
    (reduce + res))

  (defn match-base [func var p fields no-slc]
    (unless no-slc (setv p (slice p 1)))
    (map-fields func (if fields `(. ~var -fields) var) p))

  (defn cond-match-base [var p &optional t no-slc fields]
    (setv p2 (if no-slc p (slice p 1)))
    (+ [`(isinstance ~var ~(or t (get p 0)))]
       (match-base recurse-cond var p fields no-slc)))
<commit_msg>Whoops<commit_after>(import [hy.models.string [HyString]]
        [hy.models.symbol [HySymbol]]
        [hy.models.list [HyList]]
        [hy.models.keyword [HyKeyword]]
        [hy.models.expression [HyExpression]])

(defclass MatchFailure [Exception] [])

(defn match-failed []
  (raise (MatchFailure "match failed")))

(defn try-func [f]
  (try
    (do (f) true)
    (catch [] false)))

(defmacro accfor [args &rest body]
  (def names (slice args 0 nil 2))
  `(genexpr ((fn [~@names] ~@body) ~@names) [~@args]))

(defmacro defunion [name &rest types]
  (setv base `(defclass ~name [object] []))
  (setv classes (accfor [t types]
    (setv fields (HyList (slice t 1)))
    (setv field-slist (HyList (map HyString fields)))
    (defn mk-fmstr [s]
      (HyString (.join ", " (accfor [f fields] (% "%s=%%%s" (, f s))))))
    (setv field-sfmstr (mk-fmstr "s"))
    (setv field-rfmstr (mk-fmstr "r"))
    (setv sname (HyString (get t 0)))
    (defn mk-fmfn [v]
      `(% "%s(%s)" (, ~sname (% ~v (tuple self.-fields)))))
    `(defclass ~(get t 0) [~name]
      [[--init-- (fn [self ~@fields]
                  (for [x (zip ~field-slist ~fields)]
                    (setattr self (get x 0) (get x 1)))
                  (setv self.-fields ~fields)
                  nil
                 )]
       [--str-- (fn [self] ~(mk-fmfn field-sfmstr))]
       [--repr-- (fn [self] ~(mk-fmfn field-rfmstr))]])))
  (setv result (list (+ [base] (list classes))))
  `(do ~@result nil))

(defmacro match [x &rest branches]
  (defn get-tp [p]
    (cond
      [(isinstance p HyExpression)
        (if (= (get p 0) `,) "tupl-match" "data-match")]
      [(isinstance p HySymbol)
        (if (= p `_) "fallthough" "test-value")]
      [(isinstance p HyList) "list-match"]
      [(isinstance p HyKeyword) "grap-value"]
      [true "test-value"]))

  (defn map-fields [func var p]
    (setv res [])
    (for [[i x] (enumerate p)]
      (if (= x (HySymbol "..."))
        (break))
      (res.append (func `(get ~var ~(HyInteger i)) x)))
    (reduce + res))

  (defn match-base [func var p fields no-slc]
    (unless no-slc (setv p (slice p 1)))
    (map-fields func (if fields `(. ~var -fields) var) p))

  (defn cond-match-base [var p &optional t no-slc fields]
    (setv p2 (if no-slc p (slice p 1)))
    (+ [`(isinstance ~var ~(or t (get p 0)))]
       (match-base recurse-cond var p fields no-slc)))

{'fraction_non_alphanumeric': 0.19517313746065057, 'fraction_numerical': 0.005036726128016789, 'mean_word_length': 2.476294675419402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7334686', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1761, 'n_words': 526}