Namespaces
Create a new namespace:
The new namespace is activated as the current namespace, which will hold the vars in a lookup table indexed by their symbols.
Define vars with the same name in two different namespaces:
Using ns on an existing namespace does not create a new namespace,
but activates the existing one.
Use fully qualified symbols to access other namespaces:
Use a namespace from the standard library, and use it to compare data objects:
The diff function returns a three-element list with
- data that only exists in the first argument,
- data that only exists in the second argument, and
- data that is common to both arguments.
Create a new Leiningen project in the shell:
The file music_store/src/music_store.clj defines the namespace
music-store.core and contains the following code:
Dashes are used for symbol names, and underscores for file names.
Create a new namespace music-store.bands in music_store/src/music_store/bands.clj:
Use a namespace in the ns declaration in music_store/src/mustic_store/core.clj:
Unlike the standalone require function used in the REPL, :require
declarations within ns do not need quoting:
Create a (shorter) alias for a namespace:
Same within ns without quoting:
Pull in specific symbols from another namespace:
Pull in all the symbols from another namespace:
Use :refer :all with caution, for it pollutes the current namespace.
Get hold of the current namespace:
Lookup a namespace by its name:
Discover the bindings of a namespace:
If a symbol instead of a namespace is given, ns-map will look up the namespace:
Get the namespace of a symbol:
Qualify a keyword with a namespace:
Qualify a keyword with the current namespace (e.g. from music-store.core):
Reload the symbols of a namespace (force-reloads already loaded symbols):
Get rid of loaded symbols:
Define a var that resits :reload (e.g. when initialized using a heavy function call):
Exercises
Leiningen Project
Create a new Leiningen project called fibonacci and run the generated code.
Hint: Use lein run to run the project from the fibonacci/ folder.
Test: lein run shall output "Hello, World!".
Additional Namespace
Create a new namespace recursive within the fibonacci
project. Implement a function called fib that calculates the nth
Fibonacci number given the parameter n. Use that function from the
core namespace (src/fibonacci/core.clj) in the -main function
and call it with the argument 35 and output the result.
Hint: Put the file into the src/fibonacci folder and name it
according to the namespace defined therin.
Test: The application shall output fib(35)=14930352.
Yet Another Namespace
Create a new namespace tail-recursive within the fibonacci
project. Re-use the tail-recursive implementation of the fib
function from chapter
5.
Now import both the recursive and tail-recursive namespaces into the
core namespace as fib-rec and fib-tail, respectively. Call both
functions with the argument 35 and output the result as before.
Hint: Use :require and :as to define an alias name for the
namespaces.
Test: The application shall output fib(35)=14930352 twice.