Fork me on GitHub
#clojure-norway
<
2023-07-14
>
terjesb13:07:57

Ettermiddag! Syns det passer å kryssposte denne fra vår interne Slack:) Kontekst:

❯ clj
Clojure 1.12.0-alpha4
user=> (clojure.string/split "user:pass:word" #":")
["user" "pass" "word"]
user=> (clojure.string/split "user:pass:word" #":" 2)
["user" "pass:word"]
And since it’s Friday and in the spirit of “100%” understanding it, why does it not return [“user:pass” “word”]? The docstring doesn’t tell me: Splits string on a regular expression. Optional argument limit is the maximum number of splits. Not lazy. Returns vector of the splits. However, the Clojure version is a one-liner that simply delegates to Java’s String/split, which succinctly documents this behavior: The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. • If the limit is positive then the pattern will be applied at most limit - 1 times, the array’s length will be no greater than limit, and the array’s last entry will contain all input beyond the last matched delimiter. • If the limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. • If the limit is negative then the pattern will be applied as many times as possible and the array can have any length. So this cannot possibly work in ClojureScript or ClojureCLR? Well, ClojureScript has to do the work itself:
(defn split
  ([s re]
     (split s re 0))
    ([s re limit]
     (discard-trailing-if-needed limit
       (if (identical? "/(?:)/" (str re))
         (split-with-empty-regex s limit)
         (if (< limit 1)
           (vec (.split (str s) re))
           (loop [s s
                  limit limit
                  parts []]
             (if (== 1 limit)
               (conj parts s)
               (let [m (re-find re s)]
                 (if-not (nil? m)
                   (let [index (.indexOf s m)]
                     (recur (.substring s (+ index (count m)))
                       (dec limit)
                       (conj parts (.substring s 0 index))))
                   (conj parts s))))))))))
Even ClojureCLR supports it, somehow:
❯ docker compose exec -it dotnet-clojure /app/docker/start-repl.sh
Exported CLOJURE_LOAD_PATH: src:dev:/app/dependencies/tools.nrepl-0.1.0-alpha1:/app/dependencies/tools.reader-1.3.7
Started nREPL server at localhost:6667
See you guys in two weeks! 🙂
❯ clj -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.5.3"}}}' -m nrepl.cmdline -c --host 127.0.0.1 --port 6667
WARNING: Implicit use of clojure.main with options is deprecated, use -M
nREPL 0.5.3
Clojure 1.12.0-alpha4
OpenJDK 64-Bit Server VM 20.0.1+9-sapmachine
Interrupt: Control+C
Exit:      Control+D or (exit) or (quit)
user=> (clojure.string/split "user:pass:word" #":")
["user" "pass" "word"]
user=> (clojure.string/split "user:pass:word" #":" 2)
["user" "pass:word"]
user=> (System.Console/WriteLine "See you guys in two weeks! 🙂")
nil

til 2
👍 2