Clojure Namespaces & `require` — Organizing Code Across Files | Episode 20
0views
C
CelesteAI
Description
Every Clojure file starts with a namespace. A namespace is how Clojure says "this code lives here" — and `require` is how one file reaches into another. In this episode we leave the single-file world behind and build our first real two-file project: `app.core` requiring `app.util`.
We show the three moves you'll use every day: `:require` with `:as` for an alias, `:refer` to pull specific names in unqualified, and the file-to-namespace mapping that ties it all together (`src/app/util.clj` ↔ `app.util`).
This kicks off Phase 4 — Namespaces, Projects, Testing — the practical phase that lets you actually ship Clojure code beyond a single script.
Student code: https://github.com/GoCelesteAI/clojure-for-beginners/tree/main/episode20
Every keystroke is shown on screen with generous pauses so you can follow along at your own pace.
What You'll Learn:
- `*ns*` and `in-ns` — what a namespace is, at the REPL
- `ns` declarations — every file starts with one
- `:require` with `:as` — the default, readable form
- `:require` with `:refer` — pulling specific names in unqualified
- File-to-namespace mapping — `src/app/util.clj` ↔ `app.util`
- `deps.edn` with a `:run` alias for `clj -M:run`
Timestamps:
0:00 - Intro
0:15 - Preview: organizing code across files
0:38 - Start the REPL
0:52 - `*ns*` — the current namespace
1:10 - `in-ns` and fully-qualified names
1:50 - Exit REPL
2:05 - Write deps.edn
2:30 - Project layout
2:45 - Write src/app/util.clj
4:30 - Write src/app/core.clj with `:require`
6:15 - Run `clj -M -m app.core`
6:35 - Run via `:run` alias with an argument
7:00 - Recap
7:25 - What's next: Episode 21
Key Takeaways:
1. A namespace is Clojure's unit of code organization — every file has one.
2. `ns` at the top of a file declares its namespace and its dependencies.
3. `:as` gives an alias you write at every call site (`u/shout`).
4. `:refer` pulls specific names in unqualified (`join` instead of `str/join`).
5. File path under `src/` must match the namespace (`app.util` ↔ `src/app/util.clj`).
6. A `deps.edn` `:aliases` block lets you run the app with `clj -M:run`.
Phase 3 — Functional Core — is behind us. Phase 4 begins here: the practical work of namespaces, projects, and testing that turns your Clojure from REPL snippets into shippable code.
Taught by CelesteAI. Like and subscribe for more Clojure tutorials!