cider-cheatsheet.el (21591B)
1 ;;; cider-cheatsheet.el --- Quick reference for Clojure -*- lexical-binding: t -*- 2 3 ;; Copyright © 2019-2023 Kris Jenkins, Bozhidar Batsov and CIDER contributors 4 ;; 5 ;; Author: Kris Jenkins <krisajenkins@gmail.com> 6 7 ;; This program is free software: you can redistribute it and/or modify 8 ;; it under the terms of the GNU General Public License as published by 9 ;; the Free Software Foundation, either version 3 of the License, or 10 ;; (at your option) any later version. 11 12 ;; This program is distributed in the hope that it will be useful, 13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 ;; GNU General Public License for more details. 16 17 ;; You should have received a copy of the GNU General Public License 18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 19 20 ;; This file is not part of GNU Emacs. 21 22 ;;; Commentary: 23 24 ;; A quick reference system for Clojure. Fast, searchable & available offline. 25 26 ;; Mostly taken from Kris Jenkins' `clojure-cheatsheet' 27 ;; See: https://github.com/clojure-emacs/clojure-cheatsheet 28 29 ;;; Code: 30 31 (require 'cider-doc) 32 (require 'seq) 33 34 (defconst cider-cheatsheet-hierarchy 35 '(("Primitives" 36 ("Numbers" 37 ("Arithmetic" 38 (clojure.core + - * / quot rem mod dec inc max min)) 39 ("Compare" 40 (clojure.core = == not= < > <= >= compare)) 41 ("Bitwise" 42 (clojure.core bit-and bit-and-not bit-clear bit-flip bit-not bit-or bit-set bit-shift-left bit-shift-right bit-test bit-xor unsigned-bit-shift-right)) 43 ("Cast" 44 (clojure.core byte short long int float double bigdec bigint biginteger num rationalize)) 45 ("Test" 46 (clojure.core nil? some? identical? zero? pos? neg? even? odd?)) 47 ("Random" 48 (clojure.core rand rand-int)) 49 ("BigDecimal" 50 (clojure.core with-precision)) 51 ("Ratios" 52 (clojure.core numerator denominator ratio?)) 53 ("Arbitrary Precision Arithmetic" 54 (clojure.core +\' -\' *\' inc\' dec\')) 55 ("Unchecked" 56 (clojure.core *unchecked-math* 57 unchecked-add 58 unchecked-add-int 59 unchecked-byte 60 unchecked-char 61 unchecked-dec 62 unchecked-dec-int 63 unchecked-divide-int 64 unchecked-double 65 unchecked-float 66 unchecked-inc 67 unchecked-inc-int 68 unchecked-int 69 unchecked-long 70 unchecked-multiply 71 unchecked-multiply-int 72 unchecked-negate 73 unchecked-negate-int 74 unchecked-remainder-int 75 unchecked-short 76 unchecked-subtract 77 unchecked-subtract-int))) 78 79 ("Strings" 80 ("Create" 81 (clojure.core str format)) 82 ("Use" 83 (clojure.core count get subs compare) 84 (clojure.string join escape split split-lines replace replace-first reverse re-quote-replacement index-of last-index-of starts-with? ends-with? includes?)) 85 ("Regex" 86 (clojure.core re-find re-seq re-matches re-pattern re-matcher re-groups) 87 (clojure.string replace replace-first re-quote-replacement)) 88 ("Letters" 89 (clojure.string capitalize lower-case upper-case)) 90 ("Trim" 91 (clojure.string trim trim-newline triml trimr)) 92 ("Test" 93 (clojure.core char char? string?) 94 (clojure.string blank?))) 95 96 ("Other" 97 ("Characters" 98 (clojure.core char char-name-string char-escape-string)) 99 ("Keywords" 100 (clojure.core keyword keyword? find-keyword)) 101 ("Symbols" 102 (clojure.core symbol symbol? gensym)) 103 ("Data Readers" 104 (clojure.core *data-readers* default-data-readers *default-data-reader-fn*)))) 105 106 ("Collections" 107 ("Generic Ops" 108 (clojure.core count bounded-count empty not-empty into conj)) 109 ("Tree Walking" 110 (clojure.walk walk prewalk prewalk-demo prewalk-replace postwalk postwalk-demo postwalk-replace keywordize-keys stringify-keys)) 111 ("Content tests" 112 (clojure.core distinct? empty? every? not-every? some not-any?)) 113 ("Capabilities" 114 (clojure.core sequential? associative? sorted? counted? reversible?)) 115 ("Type tests" 116 (clojure.core type class coll? list? vector? set? map? seq? 117 number? integer? float? decimal? class? rational? ratio? 118 chunked-seq? reduced? special-symbol? record?)) 119 ("Lists" 120 ("Create" 121 (clojure.core list list*)) 122 ("Examine" 123 (clojure.core first nth peek)) 124 ("Change" 125 (clojure.core cons conj rest pop))) 126 127 ("Vectors" 128 ("Create" 129 (clojure.core vec vector vector-of)) 130 ("Examine" 131 (clojure.core get peek)) 132 133 ("Change" 134 (clojure.core assoc pop subvec replace conj rseq)) 135 ("Ops" 136 (clojure.core mapv filterv reduce-kv))) 137 138 ("Sets" 139 ("Create" 140 (clojure.core set hash-set sorted-set sorted-set-by)) 141 ("Examine" 142 (clojure.core get contains?)) 143 ("Change" 144 (clojure.core conj disj)) 145 ("Relational Algebra" 146 (clojure.set join select project union difference intersection)) 147 ("Get map" 148 (clojure.set index rename-keys rename map-invert)) 149 ("Test" 150 (clojure.set subset? superset?)) 151 ("Sorted Sets" 152 (clojure.core rseq subseq rsubseq))) 153 154 ("Maps" 155 ("Create" 156 (clojure.core hash-map array-map zipmap sorted-map sorted-map-by bean frequencies group-by)) 157 ("Examine" 158 (clojure.core get get-in contains? find keys vals map-entry?)) 159 ("Change" 160 (clojure.core assoc assoc-in dissoc merge merge-with select-keys update update-in)) 161 ("Entry" 162 (clojure.core key val)) 163 ("Sorted Maps" 164 (clojure.core rseq subseq rsubseq))) 165 166 ("Hashes" 167 (clojure.core hash hash-ordered-coll hash-unordered-coll mix-collection-hash)) 168 169 ("Volatiles" 170 (clojure.core volatile! volatile? vreset! vswap!))) 171 172 ("Functions" 173 ("Create" 174 (clojure.core fn defn defn- definline identity constantly comp complement partial juxt memfn memoize fnil every-pred some-fn trampoline)) 175 ("Call" 176 (clojure.core -> ->> some-> some->> as-> cond-> cond->>)) 177 ("Test" 178 (clojure.core fn? ifn?))) 179 180 ("Transducers" 181 ("Create" 182 (clojure.core cat dedupe distinct drop drop-while filter halt-when interpose keep keep-indexed map map-indexed mapcat partition-all partition-by random-sample remove replace take take-nth take-while)) 183 ("Call" 184 (clojure.core ->Eduction eduction into sequence transduce completing run!)) 185 ("Early Termination" 186 (clojure.core deref reduced reduced? ensure-reduced unreduced))) 187 188 ("Spec" 189 ("Operations" 190 (clojure.spec.alpha valid? conform unform explain explain-data explain-str explain-out form describe assert check-asserts check-asserts?)) 191 ("Generator Ops" 192 (clojure.spec.alpha gen exercise exercise-fn)) 193 ("Defn & Registry" 194 (clojure.spec.alpha def fdef registry get-spec spec? spec with-gen)) 195 ("Logical" 196 (clojure.spec.alpha and or)) 197 ("Collection" 198 (clojure.spec.alpha coll-of map-of every every-kv keys merge)) 199 ("Regex " 200 (clojure.spec.alpha cat alt * + \? & keys*)) 201 ("Range" 202 (clojure.spec.alpha int-in inst-in double-in int-in-range? inst-in-range?)) 203 ("Custom Explain" 204 (clojure.spec.alpha explain-printer *explain-out*)) 205 ("Other" 206 (clojure.spec.alpha nilable multi-spec fspec conformer)) 207 208 ("Predicates with test.check generators" 209 ("Numbers" 210 (clojure.core number? rational? integer? ratio? decimal? float? zero? double? int? nat-int? neg-int? pos-int?)) 211 ("Symbols & Keywords" 212 (clojure.core keyword? symbol? ident? qualified-ident? qualified-keyword? qualified-symbol? simple-ident? simple-keyword? simple-symbol?)) 213 ("Scalars" 214 (clojure.core string? true? false? nil? some? boolean? bytes? inst? uri? uuid?)) 215 ("Collections" 216 (clojure.core list? map? set? vector? associative? coll? sequential? seq? empty? indexed? seqable?)) 217 ("Other" 218 (clojure.core any?)))) 219 220 ("Other" 221 ("XML" 222 (clojure.core xml-seq) 223 (clojure.xml parse)) 224 ("REPL" 225 (clojure.core *1 *2 *3 *e *print-dup* *print-length* *print-level* *print-meta* *print-readably*)) 226 ("EDN" 227 (clojure.edn read read-string)) 228 ("Compiling Code & Class Generation" 229 (clojure.core *compile-files* *compile-path* *file* *warn-on-reflection* compile gen-class gen-interface loaded-libs test)) 230 ("Misc" 231 (clojure.core eval force name *clojure-version* clojure-version *command-line-args*)) 232 ("Pretty Printing" 233 (clojure.pprint pprint print-table pp *print-right-margin*)) 234 ("Browser / Shell" 235 (clojure.java.browse browse-url) 236 (clojure.java.shell sh with-sh-dir with-sh-env))) 237 238 ("Vars & Global Environment" 239 ("Def Variants" 240 (:special def) 241 (clojure.core defn defn- definline defmacro defmethod defmulti defonce defrecord)) 242 ("Interned Vars" 243 (:special var) 244 (clojure.core declare intern binding find-var)) 245 ("Var Objects" 246 (clojure.core with-local-vars var-get var-set alter-var-root var?)) 247 ("Var Validators" 248 (clojure.core set-validator! get-validator))) 249 250 ("Reader Conditionals" 251 (clojure.core reader-conditional reader-conditional? tagged-literal tagged-literal?)) 252 253 ("Abstractions" 254 ("Protocols" 255 (clojure.core defprotocol extend extend-type extend-protocol reify extends? satisfies? extenders)) 256 ("Records & Types" 257 (clojure.core defrecord deftype)) 258 ("Multimethods" 259 ("Define" 260 (clojure.core defmulti defmethod)) 261 ("Dispatch" 262 (clojure.core get-method methods)) 263 ("Remove" 264 (clojure.core remove-method remove-all-methods)) 265 ("Prefer" 266 (clojure.core prefer-method prefers)) 267 ("Relation" 268 (clojure.core derive isa? parents ancestors descendants make-hierarchy)))) 269 270 ("Macros" 271 ("Create" 272 (clojure.core defmacro definline)) 273 ("Debug" 274 (clojure.core macroexpand-1 macroexpand) 275 (clojure.walk macroexpand-all)) 276 ("Branch" 277 (clojure.core and or when when-not when-let when-first if-not if-let cond condp case)) 278 ("Loop" 279 (clojure.core for doseq dotimes while)) 280 ("Arrange" 281 (clojure.core .. doto ->)) 282 ("Scope" 283 (clojure.core binding locking time) 284 (clojure.core with-in-str with-local-vars with-open with-out-str with-precision with-redefs with-redefs-fn)) 285 ("Lazy" 286 (clojure.core lazy-cat lazy-seq delay delay?)) 287 ("Doc" 288 (clojure.core assert comment) 289 (clojure.repl doc dir dir-fn source-fn))) 290 291 ("Java Interop" 292 ("General" 293 (:special new set!) 294 (clojure.core .. doto bean comparator enumeration-seq import iterator-seq memfn definterface supers bases)) 295 ("Cast" 296 (clojure.core boolean byte short char int long float double bigdec bigint num cast biginteger)) 297 ("Exceptions" 298 (:special throw try catch finally) 299 (clojure.core ex-info ex-data Throwable->map StackTraceElement->vec) 300 (clojure.repl pst)) 301 ("Arrays" 302 ("Create" 303 (clojure.core boolean-array byte-array double-array char-array float-array int-array long-array make-array object-array short-array to-array)) 304 ("Manipulate" 305 (clojure.core aclone aget aset alength amap areduce aset-int aset-long aset-short aset-boolean aset-byte aset-char aset-double aset-float)) 306 ("Cast" 307 (clojure.core booleans bytes chars doubles floats ints longs shorts))) 308 ("Proxy" 309 ("Create" 310 (clojure.core proxy get-proxy-class construct-proxy init-proxy)) 311 ("Misc" 312 (clojure.core proxy-mappings proxy-super update-proxy)))) 313 314 ("Namespaces" 315 ("Current" 316 (clojure.core *ns*)) 317 ("Create Switch" 318 (clojure.core ns in-ns create-ns)) 319 ("Add" 320 (clojure.core alias import intern refer refer-clojure)) 321 ("Find" 322 (clojure.core all-ns find-ns)) 323 ("Examine" 324 (clojure.core ns-aliases ns-imports ns-interns ns-map ns-name ns-publics ns-refers)) 325 ("From symbol" 326 (clojure.core resolve namespace ns-resolve the-ns)) 327 ("Remove" 328 (clojure.core ns-unalias ns-unmap remove-ns))) 329 ("Loading" 330 ("Load libs" 331 (clojure.core require use import refer)) 332 ("List Loaded" 333 (clojure.core loaded-libs)) 334 ("Load Misc" 335 (clojure.core load load-file load-reader load-string))) 336 337 ("Concurrency" 338 ("Atoms" 339 (clojure.core atom swap! swap-vals! reset! reset-vals! compare-and-set!)) 340 ("Futures" 341 (clojure.core future future-call future-cancel future-cancelled? future-done? future?)) 342 ("Threads" 343 (clojure.core bound-fn bound-fn* get-thread-bindings pop-thread-bindings push-thread-bindings)) 344 345 ("Misc" 346 (clojure.core locking pcalls pvalues pmap seque promise deliver)) 347 348 ("Refs & Transactions" 349 ("Create" 350 (clojure.core ref)) 351 ("Examine" 352 (clojure.core deref)) 353 ("Transaction" 354 (clojure.core sync dosync io!)) 355 ("In Transaction" 356 (clojure.core ensure ref-set alter commute)) 357 ("Validators" 358 (clojure.core get-validator set-validator!)) 359 ("History" 360 (clojure.core ref-history-count ref-max-history ref-min-history))) 361 362 ("Agents & Asynchronous Actions" 363 ("Create" 364 (clojure.core agent)) 365 ("Examine" 366 (clojure.core agent-error)) 367 ("Change State" 368 (clojure.core send send-off restart-agent send-via set-agent-send-executor! set-agent-send-off-executor!)) 369 ("Block Waiting" 370 (clojure.core await await-for)) 371 ("Ref Validators" 372 (clojure.core get-validator set-validator!)) 373 ("Watchers" 374 (clojure.core add-watch remove-watch)) 375 ("Thread Handling" 376 (clojure.core shutdown-agents)) 377 ("Error" 378 (clojure.core error-handler set-error-handler! error-mode set-error-mode!)) 379 ("Misc" 380 (clojure.core *agent* release-pending-sends)))) 381 382 ("Sequences" 383 ("Creating a Lazy Seq" 384 ("From Collection" 385 (clojure.core seq sequence keys vals rseq subseq rsubseq)) 386 ("From Producer Fn" 387 (clojure.core lazy-seq repeatedly iterate)) 388 ("From Constant" 389 (clojure.core repeat range)) 390 ("From Other" 391 (clojure.core file-seq line-seq resultset-seq re-seq tree-seq xml-seq iterator-seq enumeration-seq)) 392 ("From Seq" 393 (clojure.core keep keep-indexed))) 394 395 ("Seq in, Seq out" 396 ("Get shorter" 397 (clojure.core distinct dedupe filter remove for)) 398 ("Get longer" 399 (clojure.core cons conj concat lazy-cat mapcat cycle interleave interpose))) 400 ("Tail-items" 401 (clojure.core rest nthrest fnext nnext drop drop-while take-last for)) 402 ("Head-items" 403 (clojure.core take take-nth take-while butlast drop-last for)) 404 ("Change" 405 (clojure.core conj concat distinct flatten group-by partition partition-all partition-by split-at split-with filter remove replace shuffle random-sample)) 406 ("Rearrange" 407 (clojure.core reverse sort sort-by compare)) 408 ("Process items" 409 (clojure.core map pmap map-indexed mapcat for replace seque)) 410 411 ("Using a Seq" 412 ("Extract item" 413 (clojure.core first second last rest next ffirst nfirst fnext nnext nth nthnext rand-nth when-first max-key min-key)) 414 ("Construct coll" 415 (clojure.core zipmap into reduce reductions set vec into-array to-array-2d)) 416 ("Pass to fn" 417 (clojure.core apply)) 418 ("Search" 419 (clojure.core some filter)) 420 ("Force evaluation" 421 (clojure.core doseq dorun doall)) 422 ("Check for forced" 423 (clojure.core realized?)))) 424 425 ("Zippers" 426 ("Create" 427 (clojure.zip zipper seq-zip vector-zip xml-zip)) 428 ("Get loc" 429 (clojure.zip up down left right leftmost rightmost)) 430 ("Get seq" 431 (clojure.zip lefts rights path children)) 432 ("Change" 433 (clojure.zip make-node replace edit insert-child insert-left insert-right append-child remove)) 434 ("Move" 435 (clojure.zip next prev)) 436 ("XML" 437 (clojure.data.zip.xml attr attr= seq-test tag= text text= xml-> xml1->)) 438 ("Misc" 439 (clojure.zip root node branch? end?))) 440 441 ("Documentation" 442 ("REPL" 443 (clojure.repl doc find-doc apropos source pst) 444 (clojure.java.javadoc javadoc))) 445 446 ("Transients" 447 ("Create" 448 (clojure.core transient persistent!)) 449 ("Change" 450 (clojure.core conj! pop! assoc! dissoc! disj!))) 451 ("Misc" 452 ("Compare" 453 (clojure.core = == identical? not= not compare) 454 (clojure.data diff)) 455 ("Test" 456 (clojure.core true? false? nil? instance?))) 457 458 ("IO" 459 ("To/from ..." 460 (clojure.core spit slurp)) 461 ("To *out*" 462 (clojure.core pr prn print printf println newline) 463 (clojure.pprint print-table)) 464 ("To writer" 465 (clojure.pprint pprint cl-format)) 466 ("To string" 467 (clojure.core format with-out-str pr-str prn-str print-str println-str)) 468 ("From *in*" 469 (clojure.core read-line read)) 470 ("From reader" 471 (clojure.core line-seq read)) 472 ("From string" 473 (clojure.core read-string with-in-str)) 474 ("Open" 475 (clojure.core with-open) 476 (clojure.java.io reader writer input-stream output-stream)) 477 ("Interop" 478 (clojure.java.io make-writer make-reader make-output-stream make-input-stream)) 479 ("Misc" 480 (clojure.core flush file-seq *in* *out* *err*) 481 (clojure.java.io file copy delete-file resource as-file as-url as-relative-path make-parents))) 482 483 ("Metadata" 484 (clojure.core meta with-meta alter-meta! reset-meta! vary-meta)) 485 486 ("Special Forms" 487 (:special def if do quote var recur throw try monitor-enter monitor-exit) 488 (clojure.core fn loop) 489 ("Binding / Destructuring" 490 (clojure.core let fn letfn defn defmacro loop for doseq if-let if-some when-let when-some))) 491 492 ("Async" 493 ("Main" 494 (clojure.core.async go go-loop <! <!! >! >!! chan put! take take! close! timeout offer! poll! promise-chan)) 495 ("Choice" 496 (clojure.core.async alt! alt!! alts! alts!! do-alts)) 497 ("Buffering" 498 (clojure.core.async buffer dropping-buffer sliding-buffer unblocking-buffer?)) 499 ("Pipelines" 500 (clojure.core.async pipeline pipeline-async pipeline-blocking)) 501 ("Threading" 502 (clojure.core.async thread thread-call)) 503 ("Mixing" 504 (clojure.core.async admix solo-mode mix unmix unmix-all toggle merge pipe unique)) 505 ("Multiples" 506 (clojure.core.async mult tap untap untap-all)) 507 ("Publish/Subscribe" 508 (clojure.core.async pub sub unsub unsub-all)) 509 ("Higher Order" 510 (clojure.core.async filter< filter> map map< map> mapcat< mapcat> partition partition-by reduce remove< remove> split)) 511 ("Pre-Populate" 512 (clojure.core.async into onto-chan to-chan))) 513 ("Unit Tests" 514 ("Defining" 515 (clojure.test deftest deftest- testing is are)) 516 ("Running" 517 (clojure.test run-tests run-all-tests test-vars)) 518 ("Fixtures" 519 (clojure.test use-fixtures join-fixtures compose-fixtures)))) 520 "A data structure for Clojure cheatsheet information. 521 522 It's a tree, where the head of each list determines the context of the rest 523 of the list. The head may be: 524 525 - A string, in which case it's a (sub)heading for the rest of the items. 526 527 - A symbol, in which case it's the Clojure namespace of the symbols that 528 follow it. 529 530 - The keyword :special, in which case it's a Clojure special form 531 532 - Any other keyword, in which case it's a typed item that will be passed 533 through. 534 535 Note that some Clojure symbols appear in more than once. This is entirely 536 intentional. For instance, `map` belongs in the sections on collections 537 and transducers.") 538 539 (defun cider-cheatsheet--expand-vars (list) 540 "Expand the symbols in LIST to fully-qualified var names. 541 542 This list is supposed to have the following format: 543 544 (my-ns var1 var2 var3)" 545 (let ((ns (car list)) 546 (vars (cdr list))) 547 (if (eq ns :special) 548 (mapcar #'symbol-name vars) 549 (mapcar (lambda (var) (format "%s/%s" ns var)) vars)))) 550 551 (defun cider-cheatsheet--select-var (var-list) 552 "Expand the symbols in VAR-LIST to fully-qualified var names. 553 554 The list can hold one or more lists inside - one per each namespace." 555 (let ((namespaced-vars (seq-mapcat #'cider-cheatsheet--expand-vars 556 (seq-remove (lambda (list) 557 (eq (car list) :url)) 558 var-list)))) 559 (cider-doc-lookup (completing-read "Select var: " namespaced-vars)))) 560 561 ;;;###autoload 562 (defun cider-cheatsheet () 563 "Navigate `cider-cheatsheet-hierarchy' with `completing-read'. 564 565 When you make it to a Clojure var its doc buffer gets displayed." 566 (interactive) 567 (let ((cheatsheet-data cider-cheatsheet-hierarchy)) 568 (while (stringp (caar cheatsheet-data)) 569 (let* ((sections (mapcar #'car cheatsheet-data)) 570 (sel-section (completing-read "Select cheatsheet section: " sections)) 571 (section-data (seq-find (lambda (elem) (equal (car elem) sel-section)) cheatsheet-data))) 572 (setq cheatsheet-data (cdr section-data)))) 573 (cider-cheatsheet--select-var cheatsheet-data))) 574 575 (provide 'cider-cheatsheet) 576 577 ;;; cider-cheatsheet.el ends here