Skip to content

Instantly share code, notes, and snippets.

@daemianmack
Forked from ghoseb/ns-cheatsheet.clj
Created July 8, 2012 13:42
Show Gist options
  • Save daemianmack/3070981 to your computer and use it in GitHub Desktop.
Save daemianmack/3070981 to your computer and use it in GitHub Desktop.
Clojure ns syntax cheat-sheet
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix.
;;
;; * :use makes functions available without a namespace prefix
;; (i.e., refers functions to the current namespace).
;;
;; * :import refers Java classes to the current namespace.
;;
;; * :refer-clojure affects availability of built-in (clojure.core)
;; functions.
;;
(ns foo.bar
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Refers clojure.contrib.math into current NS, e.g.:
;; (clojure.contrib.math/sqrt 4)
[:require [clojure.contrib math]]
;; Refers math into current NS, e.g.:
;; (math/sqrt 4)
[:require [clojure.contrib [math :as math]]]
;; Refers clojure.contrib.seq-utils and math into current NS, e.g.:
;; (clojure.contrib.seq-utils/flatten [[1 2] 3])
;; (math/sqrt 4)
[:require [clojure.contrib seq-utils [math :as math]]]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Refers all functions from clojure.contrib.math and
;; clojure.contrib.seq-utils into current NS, e.g.:
;; (sqrt 4)
;; (flatten [[1 2] 3])
[:use [clojure.contrib math seq-utils]]
;; Refers only sqrt and flatten into current NS, e.g.:
;; (sqrt 4)
;; (flatten [[1 2] 3])
[:use [clojure.contrib [math :only [sqrt]]
[seq-utils :only [flatten]]]]
;; Refers all functions from clojure.contrib.math except sqrt into
;; current NS
[:use [clojure.contrib.math :exclude [sqrt]]]
;; Refers sqrt into current NS as ccm-sqrt, plus all other math
;; functions e.g.:
;; (ccm-sqrt 4)
;; (round 1.3)
[:use [clojure.contrib.math :rename {sqrt ccm-sqrt}]]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Refers ArrayList and HashMap into current NS:
[:import [java.util ArrayList HashMap]]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Excludes built-in print
[:refer-clojure :exclude [print]]
;; Excludes all built-ins except print
[:refer-clojure :only [print]]
;; Renames built-in print to core-print
[:refer-clojure :rename {print core-print}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment