Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

union and difference of sets in Clojure

Tags:

union

set

clojure

I am reading the Clojure section of the book Seven Languages in Seven Weeks. It says:

You can merge two sets, like this:
user=> (clojure.set/union #{:skywalker} #{:vader})
#{:skywalker :vader}
Or compute the difference:
(clojure.set/difference #{1 2 3} #{2})

This doesn't work on my version (Clojure 1.7.0 and Java version"1.8.0_51" ):

user=> (clojure.set/difference #{1 2 3} #{4})
ClassNotFoundException clojure.set  java.net.URLClassLoader.findClass (:-1)

user=> (clojure.set/union #{:skywalker} #{:vader})
ClassNotFoundException clojure.set  java.net.URLClassLoader.findClass (:-1)

In Clojuredocs shorter function names are used in the examples, but that also doesn't work:

user=> (difference #{1 2} #{2 3})
CompilerException java.lang.RuntimeException: Unable to resolve symbol: difference in this context, compiling:(NO_SOURCE_PATH:14:1) 

Why am I receiving these errors and how should I fix that?

like image 785
Morteza R Avatar asked Jul 21 '15 04:07

Morteza R


1 Answers

Namespaces other than clojure.core and user are only loaded if someone loads them. The standard way to load a namespace is with require.

(require '[clojure.set :as set])

(set/union ...)
like image 66
noisesmith Avatar answered Sep 27 '22 23:09

noisesmith