1

In java I can do something like this:

myList.add(myObj);

then remove it like this

myList.remove(myObj);

This works by using the equals method and checking each element in myList for equivalence.

In clojure I can do this:

(def myvec [myrecord])

but I can't remove myrecord from myvec using remove like this:

(remove myrecord myvec)

because I get an error:

ClassCastException myproj.Task cannot be cast to clojure.lang.IFn  clojure.core/complement/fn--4611 (core.clj:1392)

How can I remove records from vectors in a way similar to the java method?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207

1 Answers1

4

Remove expects a function that runs over every item in your collection, and it keeps only the items that the function returned a falsey value on. In your case the error message is correct by telling you that your object is not a function.

You could either explicitly write a function that uses equals on your object, or you could add the object you want to remove to a set. A set acts as a function that returns a truthy value when its input is a member of that set.

(remove #{myrecord} myvec)

A concrete example

(remove #{4 3} [1 2 3 4]) 
;=> (1 2)
Shlomi
  • 4,708
  • 1
  • 23
  • 32