3

Here is the original code block for ClojureScript's string "replace" function:

(defn replace
  "Replaces all instance of match with replacement in s.
  match/replacement can be:
  string / string
  pattern / (string or function of match)."
 [s match replacement]
 (cond
   (string? match)
   (.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)

   (instance? js/RegExp match)
   (if (string? replacement)
     (replace-all s match replacement)
     (replace-all s match (replace-with replacement)))

   :else (throw (str "Invalid match arg: " match))))

As you can see on this line:[s match replacement], this method accepts three arguments.

From my REPL:

user=> (replace ":c41120" ":" "")

ArityException Wrong number of args (3) passed to: core/replace  clojure.lang.AFn.throwArity (AFn.java:429)

Am I the only one who thinks I have passed the correct number of arguments (3)? Any idea why this is failing?

Question, Part II: Getting Specific

In my components.cljs file, I have these 'requires':

(ns labrador.components
(:require [re-frame.core :as rf]
          [reagent.core :refer [atom]]
          [clojure.string :as s]
          [labrador.helpers :as h]))

I've had success using "s/join" and "s/blank?" in this file. But when I try using "s/replace" like below (note that the "replace" call is on line 484):

            (for [roll-count order-item-roll-counts]
              (let [key (key roll-count)
                    val (val roll-count)
                    code (s/replace key ":" "")]

...I get the following error:

Uncaught TypeError: s.replace is not a function
  at clojure$string$replace (string.cljs?rel=1489020198332:48)
  at components.cljs?rel=1489505254528:484

...And when I explicitly call the replace function, like so:

code (clojure.string/replace key ":" "")]

...I still get the exact same error, as if I'm still calling "s/replace."

I'm new to Clojure/ClojureScript, so bare with my apparent ignorance.

3 Answers 3

5

Firstly, it looks like you're running in a Clojure REPL, not a ClojureScript one, secondly, you're calling clojure.core/replace, instead of clojure.string/replace.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, clojure.string/replace works in my REPL, and I've used it successfully in my ClojureScript. Thanks for clarifying that distinction. But I'm getting errors when I try this one particular replace function. I'll update my question with more details.
5

I found the error. I was trying to do a replace on a key, not a string. Once I converted the key to a string before calling the replace function, by changing (s/replace key ":" "") with (s/replace (str key) ":" ""), all was well.

I was thrown way off-course by the ambiguous error message. Being told the function 'replace' isn't a function when it clearly is, rather than being told the function can't perform it's job because the data passed isn't a string, just cost me about three hours of dev time.

2 Comments

If you're trying to get a string from a keyword without the leading :, you can use the name function.
To clarify, error is not indicating that clojure.string/replace is not a function. Instead it is a result of a failing (.replace s ...) call in the implementation. Compare (let [s ":abc"] (.replace s ":" "")) with (let [s :abc] (.replace s ":" "")). The latter reproduces the error and is essentially indicating that there is no replace function defined on keywords.
2

I came to this issue when the first args is a number, not a string.

I think the error message is kinds of misleading

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.