<commit_before>(ns zweikopf.multi
  (:require [clojure.string :as str])
  (:import (org.jruby.embed ScriptingContainer
                            LocalContextScope)
           (org.jruby Ruby
                      RubyArray
                      RubyBasicObject
                      RubyHash
                      RubyHash$RubyHashEntry
                      RubyObject
                      RubyString
                      RubySymbol)))

(defprotocol Clojurize
  (clojurize [this ^ScriptingContainer ruby]))

(defprotocol Rubyize
  (rubyize [this ^ScriptingContainer ruby]))

(defn ruby-eval
  [^ScriptingContainer ruby script]
  (.runScriptlet ruby script))

(defn call-ruby
  [^ScriptingContainer container klass method & args]
  (let [method-name (name method)
        klass (if (string? klass)
                (ruby-eval container (str/replace klass "/" "::"))
                klass)]
    (if (empty? args)
      (.callMethod container klass method-name Object)
      (.callMethod container klass method-name (object-array args) Object))))

(defn context
  []
  (ScriptingContainer. LocalContextScope/SINGLETHREAD))

(defn terminate
  [^ScriptingContainer ctx]
  (.terminate ctx))

(extend-protocol Clojurize
  nil
  (clojurize [this _]
    nil)

  RubySymbol
  (clojurize [this _]
    (clojure.lang.Keyword/intern (.toString this)))

  RubyHash
  (clojurize [this ruby]
    (persistent!
      (reduce (fn [acc ^RubyHash$RubyHashEntry entry]
                (assoc! acc
                        (clojurize (.getKey entry) ruby)
                        (clojurize (.getValue entry) ruby)))
              (transient {})
              (.directEntrySet this))))

  RubyArray
  (clojurize [this ruby]
    (persistent!
      (reduce (fn [acc entry]
                (conj! acc (clojurize entry ruby)))
              (transient [])
              this)))

  RubyString
  (clojurize [this _]
    (.decodeString this))

  org.jruby.RubyNil
  (clojurize  [_ _]
    nil)

  org.jruby.RubyFixnum
  (clojurize  [this _]
    (.getLongValue this))
<commit_msg>Use mapv (uses transients internally)<commit_after>(ns zweikopf.multi
  (:require [clojure.string :as str])
  (:import (org.jruby.embed ScriptingContainer
                            LocalContextScope)
           (org.jruby Ruby
                      RubyArray
                      RubyBasicObject
                      RubyHash
                      RubyHash$RubyHashEntry
                      RubyObject
                      RubyString
                      RubySymbol)))

(defprotocol Clojurize
  (clojurize [this ^ScriptingContainer ruby]))

(defprotocol Rubyize
  (rubyize [this ^ScriptingContainer ruby]))

(defn ruby-eval
  [^ScriptingContainer ruby script]
  (.runScriptlet ruby script))

(defn call-ruby
  [^ScriptingContainer container klass method & args]
  (let [method-name (name method)
        klass (if (string? klass)
                (ruby-eval container (str/replace klass "/" "::"))
                klass)]
    (if (empty? args)
      (.callMethod container klass method-name Object)
      (.callMethod container klass method-name (object-array args) Object))))

(defn context
  []
  (ScriptingContainer. LocalContextScope/SINGLETHREAD))

(defn terminate
  [^ScriptingContainer ctx]
  (.terminate ctx))

(extend-protocol Clojurize
  nil
  (clojurize [this _]
    nil)

  RubySymbol
  (clojurize [this _]
    (clojure.lang.Keyword/intern (.toString this)))

  RubyHash
  (clojurize [this ruby]
    (persistent!
      (reduce (fn [acc ^RubyHash$RubyHashEntry entry]
                (assoc! acc
                        (clojurize (.getKey entry) ruby)
                        (clojurize (.getValue entry) ruby)))
              (transient {})
              (.directEntrySet this))))

  RubyArray
  (clojurize [this ruby]
    (mapv #(clojurize % ruby) this))

  RubyString
  (clojurize [this _]
    (.decodeString this))

  org.jruby.RubyNil
  (clojurize  [_ _]
    nil)

  org.jruby.RubyFixnum
  (clojurize  [this _]
    (.getLongValue this))

{'fraction_non_alphanumeric': 0.1012216404886562, 'fraction_numerical': 0.0, 'mean_word_length': 2.0416982562547386, 'pattern_counts': {'":': 2, '<': 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': '3182548', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1175, 'n_words': 292}