user12358384
user12358384

Reputation:

How to Use Remove in Clojure

(ns untitled1.core
(:require [clojure.string :as str]))

(defn foo

[half full] 

;Assume full = I am a taco
;Assume half = taco

(remove full half)

This is the message I receive: "Error printing return value (ClassCastException) at clojure.core/complement$fn (core.clj:1441). class clojure.lang.LazySeq cannot be cast to class clojure.lang.IFn (clojure.lang.LazySeq and clojure.lang.IFn are in unnamed module of loader 'app')"

I want (remove full half) to return -> I am a

I am new to Clojure, so I can well imagine that (remove) could be the wrong function. If anyone can lead me in the right direction, I would appreciate it.

Upvotes: 0

Views: 277

Answers (1)

jas
jas

Reputation: 10865

Look at the documentation for the remove function and you'll see, as you suspected, that it's not the function you were looking for https://clojuredocs.org/clojure.core/remove (You'll also see that its first argument is a function, which explains the error message you're seeing)

For this I would use clojure.string/replace (replacing something with nothing is the same as removing in this case). E.g.:

user> (def full "I am a taco")
#'user/fulluser> 
user> (def half "taco")
#'user/half
user> (clojure.string/replace full half "")
"I am a "

As a function

(ns untitled1.core
  (:require [clojure.string :as str]))

(defn foo
  [full half]
  (str/replace full half ""))

calling the function:

untitled1.core> (foo "I am a taco" "taco")
"I am a "

Upvotes: 1

Related Questions