1
votes

I'm trying to learn Clojure and I'm starting with some basics such as functions, concatenation, lexicon replacement, etc. The one I'm stuck on currently is replacement. I've created a function called test that accepts two parameters v and v2; it's purpose is to replace periods with whitespace using clojure.string/replace:

(defn replace-lexicon [value lexicon replacementValue]
(println str(clojure.string/replace value lexicon replacementValue)))
  
(replace-lexicon "this.is.a.test" "." " ")

The expected output is:

this is a test

However, the output has additional information with it:

#object[clojure.core$str 0x35aea049 clojure.core$str@35aea049] this is a test

I tried searching for answers on how to remove it, but I'm not finding anything conclusive. Drawing on my experience with other languages, I can't help but feel this is demonstrating that what's being printed isn't a string but rather an object. Unfortunately though, this is just an educated guess, and, I haven't been able to prove it.


What is #object..., and how do I remove it to correct my output?

1

1 Answers

2
votes

Close! The extra str is the problem. Here is a version written with unit tests from my favorite template project:

(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [clojure.string :as str]
  ))

(defn replace-lexicon
  [value lexicon replacementValue]
  (str/replace value lexicon replacementValue))

(dotest
  (is= "this is a test" (replace-lexicon "this.is.a.test" "." " "))
  (is= "this is a test" (replace-lexicon "this#is#a#test" "#" " "))
  )

A hint is in the substring clojure.core$str in the error msg, which is the compiler's way of representing the function clojure.core/str.