concept namespace in category clojure

appears as: The namespaces, namespace, namespaces, namespace, The namespace, namespace, A namespace, namespaces
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

  • General support is about additional APIs beyond what core has to offer. The namespaces present here enrich Clojure with new functionalities. clojure.walk and clojure.zip for example are two ways to walk and manipulate tree-like data structure. clojure.xml offers XML parsing capabilities. clojure.test is the unit test framework included with Clojure. clojure.sh contains functions to "shell-out" commands to the operative system. clojure.core.reducers offers a model of parallel computation.
  • The "alias table" is another type of dictionary associated with a namespace. It contains relationships with other namespaces, using a symbol as key and a namespace name as value. Items can be added to the alias table with :as during namespace creation or with the alias function. ns-aliases shows its content.
  • Once a namespace is created, a reference is added to the "global namespace repository". This is a static map inside the clojure.lang.Namespace class itself. It derives that namespaces are "global", that is, the running process doesn’t need to hold an explicit namespace reference to keep them alive (from the garbage collector perspective).
  • The group of functions alias, ns-aliases and ns-unalias is the small programming interface that controls alias mappings for a namespace. After creating a namespace, the alias table is usually empty:

    (create-ns 'com.web.tired-of-typing-this.myns)
    (ns-aliases 'com.web.tired-of-typing-this.myns) ; #1
    ;; {}

    The namespace just created has a somewhat long name, but this is a pretty common scenario. If the created namespace contains a var, we can reference the var without the need to require the namespace:

    Get Programming with Clojure

    This is an excerpt from Manning's book Get Programming with Clojure.

    user is the name of the current namespace. A namespace is like a module: it is the scope where the code runs. Until Unit 11, all our expressions will be executed in the user namespace.

    definition

    namespace

    In Clojure, each expression is evaluated in a namespace. The namespace is the scope of the expression. The default namespace of the REPL is named user. Namespaces will be covered in Unit 11.

    When we run a Clojure program from the command line, we specify the namespace where the entry point of the program is located. Currently, we will use namespaces mechanically. Namespaces will be covered in details in 11. The main thing to know about namespaces is that there is a one-to-one correspondence between namespaces and source files. Clojure files have the .clj suffix. For instance, the namespace corresponding to a file named hello.clj is hello.

    Now, if the entry point of our program is in a namespace named hello, we run the program with this command line:

    Clojure in Action, Second Edition

    This is an excerpt from Manning's book Clojure in Action, Second Edition.

    Like the Java compiler, the Clojure compiler expects to find your Clojure source code on the Java classpath and also expects that the full name of a namespace be unique. Appendix A covers the particulars of how projects are organized on the file system, how the classpath is set up, and how to invoke the Clojure compiler.

    When a program becomes larger than a few functions, computer languages allow the programmer to break it up into parts. An example of this facility is the package system in Java. Clojure provides the concept of namespaces for the same purpose. Programs can be broken up into parts, each being a logical collection of code—functions, vars, and the like.

    Another reason why namespaces are useful is to avoid name collisions in different parts of programs. Imagine that you were writing a program that dealt with students, tests, and scores. If you were to then use an external unit-testing library that also used the word test, Clojure might complain about redefinition! Such problems can be handled by writing code in its own namespace.

    The name, as mentioned previously, is the name of the namespace being made current. If it doesn’t already exist, it gets created. The references that follow the name are optional and can be one or more of the following: use, require, import, load, or gen-class. You’ll see some of these in action in this section and then again in chapter 5, which covers Java interop. First, let’s look at an example of defining a namespace:

    (ns org.currylogic.damages.calculators)
    (defn highest-expense-during [start-date end-date]
     ;; (logic to find the answer)
    )

    highest-expense-during is now a function that lives in the namespace with the name of org.currylogic.damages.calculators. To use it, code outside this namespace would need to make a call (directly or indirectly) to use, require, or import (if the library is compiled into a JAR) or they can call it with its fully qualified name, which is org.currylogic.damages.calculators/highest-expense-during. We’ll explore these now through examples.

    3.5.2. Working with namespaces

    Apart from the convenience offered by namespaces in helping keep code modular (and guarding from name collisions), Clojure namespaces can be accessed programmatically. In this section, we’ll review a few useful functions to do this.

    The Joy of Clojure, Second Edition

    This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

    The code in the next listing provides a function named lookup that returns the contents of a square on the chessboard, given the name of the square in standard rank-and-file notation. It also defines a few supporting functions[15] that are used in the implementation of lookup; these are encapsulated at the level of the namespace joy.chess through the use of the defn- macro that creates namespace private functions.

    A symbol can begin with a namespace and a slash (/). These can be called qualified symbols:

    2.10. Modularizing code with namespaces

    Clojure’s namespaces provide a way to bundle related functions, macros, and values. In this section, we’ll briefly talk about how to create namespaces and how to reference and use things from other namespaces. This section will focus on the Clojure (Java targeted) namespacing functionality. For a discussion with a ClojureScript-specific focus on namespaces, see chapter 13.

    Figure 9.1. The logical layout of namespaces. The process to resolve a var joy.ns/authors includes a symbolic resolution of the namespace and the var name. The result is the var itself. Aliases created with :use work as expected.
    (in-ns 'joy.ns) #1 (def authors ["Chouser"]) (in-ns 'your.ns) #2 (clojure.core/refer 'joy.ns) #3 joy.ns/authors ;=> ["Chouser"] (in-ns 'joy.ns) #4 (def authors ["Chouser" "Fogus"]) (in-ns 'your.ns) #5 joy.ns/authors  ;=> ["Chouser" "Fogus"] #1 - Create and switch to a namespace #2 - Create another #3 - Pull in all the definitions #4 - Switch namespaces #5 - Switch back, and check the value in the other namespace !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!