Clojure Programming by Chas Emerick & Brian Carper & Christophe Grand

Clojure Programming by Chas Emerick & Brian Carper & Christophe Grand

Author:Chas Emerick & Brian Carper & Christophe Grand [Chas Emerick]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / LISP
ISBN: 9781449394745
Publisher: O'Reilly Media
Published: 2012-03-29T16:00:00+00:00


Note

refer is rarely used directly, but its effects and options are available through use, which is widely used.

require and use. When some code needs to make use of functions or data defined in public vars in another namespace, require and use are used to:

Ensure that the namespaces in question are loaded.

Optionally establish aliases for those namespaces’ names.

Trigger the implicit use of refer to allow code to refer to other namespaces’ vars without qualification.

require provides (1) and (2); use is built on top of it and refer to provide (3) in a succinct way.

Let’s start with a new REPL, where we’d like to use the union function in the clojure.set namespace:

(clojure.set/union #{1 2 3} #{4 5 6}) ;= #<ClassNotFoundException java.lang.ClassNotFoundException: clojure.set>

Wait, that namespace isn’t loaded yet—only clojure.core is preloaded. We can use require to load the clojure.set namespace from the classpath;[232] afterward, we can use any function within that namespace:

(require 'clojure.set) ;= nil (clojure.set/union #{1 2 3} #{4 5 6}) ;= #{1 2 3 4 5 6}

Having to use fully qualified symbols to name vars can be a pain though, especially if the libraries you are using provide namespaces that are long or have a number of segments. Thankfully, require provides a way to specify an alias for a namespace:

(require '[clojure.set :as set]) ;= nil (set/union #{1 2 3} #{4 5 6}) ;= #{1 2 3 4 5 6}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.