ClojureScript

clojure.string/replace pattern/function of match API difference with clojure version

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    [org.clojure/clojurescript "0.0-2138"]

Description

When calling clojure.core/replace with a pattern and a function, the Clojurescript version delegates to Javascript's s.replace method, which calls that function with a variable number of arguments, depending on how many match groups are in your pattern. The Clojure version always calls it with a single argument, which may be a vector if you have match groups in your pattern.

I'm not sure if this was intentional. If it wasn't, I think this difference could be fixed through some use of re-find, which appears to return the same string or vector that you'd get in Clojure. If this is intentional for performance reasons, perhaps the doc string should be updated to note this, as there's no warning that the function is being called with too many arguments.

Activity

Hide
Curtis Gagliardi added a comment - - edited

Afraid I don't see how to edit, but I wanted to include a simple example:

CLJS:
(clojure.string/replace "hello world" #"(hello) world" (fn [m] (.log js/console (str "Match: " m)) m))

will log: "Match: hello world"

CLJ
user=> (clojure.string/replace "hello world" #"(hello) world" (fn [m] (println (str "Match: " m) m)))
Match: ["hello world" "hello"] [hello world hello]

NullPointerException java.util.regex.Matcher.quoteReplacement (Matcher.java:655)

Show
Curtis Gagliardi added a comment - - edited Afraid I don't see how to edit, but I wanted to include a simple example: CLJS: (clojure.string/replace "hello world" #"(hello) world" (fn [m] (.log js/console (str "Match: " m)) m)) will log: "Match: hello world" CLJ user=> (clojure.string/replace "hello world" #"(hello) world" (fn [m] (println (str "Match: " m) m))) Match: ["hello world" "hello"] [hello world hello] NullPointerException java.util.regex.Matcher.quoteReplacement (Matcher.java:655)

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated: