What is the all-ns method in Clojure?
Overview
Namespaces in Clojure are used to distinguish classes into separate logical parts. It's used when we have multiple imported classes in our code. The all-ns method works with namespaces.
The all-ns method is used to get the namespaces of our code.
Syntax
(all-ns)
The syntax for the all-ns method
Parameters
The all-ns method does not take any parameters.
Return value
The all-ns method returns all the namespaces of our code. This simply means that wherever the all-ns method is used, it returns all the namespaces that our code is working with. Let's look at an example.
Example
(ns clojure.examples(:gen-class))(defn func [](println all-ns))(func)
Explanation
In the code above, we see the following:
- Line 3: We define a function
func. - Line 4: We use the
all-nsmethod and print the output.
Note: The output is an object containing all the namespaces our code is working with.
- Line 5: We call the function
func.