diff --git a/learnclj.clj b/learnclj.clj index 0bb4f39..6fe1f04 100644 --- a/learnclj.clj +++ b/learnclj.clj @@ -259,8 +259,7 @@ nil ; => nil (Null/Nothing) (swap! counter inc) ; => 1 (swap! counter + 5) ; => 6 -(dosync - (reset! counter 0)) ; => 0 +(reset! counter 0) ; => 0 ;; --- 14. Java Interop --- @@ -317,9 +316,21 @@ nil ; => nil (Null/Nothing) ;; --- 18. Common Patterns --- -;; Compose functions -(defn make-upper [s] (str/upper-case s)) -(def make-upper-lower (comp str/lower-case str/upper-case)) ; => #object[clojure.core$comp$fn ...] +;; Compose functions — comp reads right-to-left (like math) +;; Example: slugify — find word chunks, lowercase, join with dashes +(def slugify (comp #(str/join "-" %) #(mapv str/lower-case %) #(re-seq #"\S+" %))) + +;; Usage +(slugify " Hello World ") ; => "hello-world" +(slugify "Foo Bar Baz") ; => "foo-bar-baz" +(slugify " Hello World ") ; => "hello-world" + +;; Another: trim then uppercase +(def trim->upper (comp str/upper-case str/trim)) + +;; Usage +(trim->upper " hello ") ; => "HELLO" +(trim->upper " Clojure is fun ") ; => "CLOJURE IS FUN" ;; Partial application diff --git a/repl.md b/repl.md index eefa07d..370fa5d 100644 --- a/repl.md +++ b/repl.md @@ -120,37 +120,79 @@ bb --nrepl-server --nrepl-port 9999 ### Clojure (JVM-based) -```bash -clj -A:nrepl +> **Important**: The `clj` command needs a `deps.edn` with an `:nREPL` alias that includes the nREPL dependency. Without one, these commands will not work. + +**Option A — Quick start (requires a `deps.edn`)** + +Add this to a `deps.edn` in your project root: + +```edn +{:aliases + {:nREPL + {:extra-deps + {nrepl/nrepl {:mvn/version "1.12.0"}}}}} ``` -This starts a Clojure REPL with nREPL support. For a server: +Then start a headless nREPL server: ```bash -clj -M:nrepl-server +clj -M:nREPL -m nrepl.cmdline +``` + +**Option B — Plain Clojure REPL (no nREPL server)** + +If you just want an interactive REPL for experimentation (no editor integration): + +```bash +clj +``` + +This starts a standard Clojure REPL (REPL-y). You type code directly into the terminal. No deps.edn, no nREPL needed. + +**Option C — Global alias (one-time setup)** + +Add the nREPL alias to your global config `~/.clojure/deps.edn`: + +```edn +{:aliases + {:nREPL + {:extra-deps + {nrepl/nrepl {:mvn/version "1.12.0"}}}}} +``` + +Then from any directory: + +```bash +clj -M:nREPL -m nrepl.cmdline ``` ### Connecting from Your Editor #### Calva (VS Code) -1. Start an nREPL server (`bb --nrepl-server` or `clj -A:nrepl`) +1. Start an nREPL server (`bb --nrepl-server` or `clj -M:nREPL -m nrepl.cmdline`) 2. `Ctrl+Alt+C, Ctrl+Alt+P` — Calva connects automatically #### Conjure (NeoVim) 1. Install Conjure via your plugin manager (`vim-plug`, `packer`, `lazy`, etc.) -2. Start an nREPL server (`bb --nrepl-server` or `clj -A:nrepl`) +2. Start an nREPL server (`bb --nrepl-server` or `clj -M:nREPL -m nrepl.cmdline`) 3. `:ConjureConnect localhost ` — connect to the REPL -4. Send code with `,e` (evaluate current line/form) +4. Send code with `ee` (evaluate form under cursor) 5. Results appear inline below the code 6. Open the REPL buffer with `:ConjureShow` **Key Conjure shortcuts:** - `ee` — evaluate form under cursor -- `E` — evaluate current line/buffer -- `pp` — evaluate and print (with pprint) -- `dd` — evaluate and doc -- `oo` — open result in browser (for HTML, URLs, etc.) -- `K` — doc word under cursor (built-in, no prefix) +- `ef` — evaluate file +- `eb` — evaluate buffer +- `er` — evaluate root form +- `ew` — evaluate word under cursor +- `ep` — evaluate previous evaluation +- `em` — evaluate marked form +- `e!` — evaluate and replace form with result +- `ece` — evaluate form and comment the result +- `gd` — get definition under cursor +- `K` — get documentation under cursor (no prefix, built-in) +- `E` (visual mode) — evaluate visual selection > Note: `` defaults to `,` but is typically remapped in your config (e.g., to `\` or `Space`). Check `:map ` to see yours. - `:ConjureConnect` — connect to an nREPL server @@ -182,7 +224,7 @@ clj -M:nrepl-server - REPL commands — create custom keybindings for common workflows (e.g., reload + evaluate) #### Calva (VS Code) -1. Start an nREPL server (`bb --nrepl-server` or `clj -A:nrepl`) +1. Start an nREPL server (`bb --nrepl-server` or `clj -M:nREPL -m nrepl.cmdline`) 2. `Ctrl+Alt+C, Ctrl+Alt+P` — Calva connects automatically ### Loading Your Code