Skip to content

Instantly share code, notes, and snippets.

@dd1994
Forked from ghoseb/ns-cheatsheet.clj
Created November 1, 2015 09:41
Show Gist options
  • Save dd1994/3c8744576b8aad0ef4ee to your computer and use it in GitHub Desktop.
Save dd1994/3c8744576b8aad0ef4ee to your computer and use it in GitHub Desktop.

Revisions

  1. @ghoseb ghoseb revised this gist Jul 9, 2014. 1 changed file with 44 additions and 40 deletions.
    84 changes: 44 additions & 40 deletions ns-cheatsheet.clj
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,74 @@
    ;;
    ;; 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).
    ;; * :require makes functions available with a namespace prefix
    ;; and optionally can refer functions to the current ns.
    ;;
    ;; * :import refers Java classes to the current namespace.
    ;;
    ;; * :refer-clojure affects availability of built-in (clojure.core)
    ;; functions.
    ;;
    ;; Updated: 7/9/2014 ~ghoseb

    (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]]]
    ;; (clojure.string/lower-case "FooBar)
    (:require [clojure.string])

    ;; 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 math into current NS, e.g.:
    ;; (string/lower-case "FooBar")
    (:require [clojure.string :as string])

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; 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]]]]
    ;; (union #{:a :b} #{:a :c} #{:b :d})
    ;; (lower-case "FooBar")
    (:require [clojure.string :refer :all]
    [clojure.set :refer :all])

    ;; Refers all functions from clojure.contrib.math except sqrt into
    ;; current NS
    [:use [clojure.contrib.math :exclude [sqrt]]]
    ;; Refers only sqrt and flatten into current NS, e.g.:
    ;; (union #{:a :b} #{:a :c} #{:b :d})
    ;; (lower-case "FooBar")
    (:require [clojure.string :refer [lower-case]]
    [clojure.set :refer [union]])

    ;; 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 ancestors & descendants into current NS as xml-ancestors & xml-descendants
    (:require clojure.data.zip)
    (:refer [clojure.data.zip :rename {ancestors xml-ancestors,
    descendants xml-descendants}])

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; Refers ArrayList and HashMap into current NS:
    [:import [java.util ArrayList HashMap]]
    (:import [java.util ArrayList HashMap])

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; Excludes built-in print
    [:refer-clojure :exclude [print]]
    (:refer-clojure :exclude [print])

    ;; Excludes all built-ins except print
    [:refer-clojure :only [print]]
    (:refer-clojure :only [print])

    ;; Renames built-in print to core-print
    [:refer-clojure :rename {print core-print}])
    (:refer-clojure :rename {print core-print})

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; Reload all dependencies while loading the ns
    (:require [clojure.string :as cs :refer [split join]] :reload)

    ;; Be verbose about loading the deps
    (:require [clojure.string :as cs :refer [split join]] :verbose)

    ;; There is also :reload-all that reloads every depenency in the ns
    (:require [clojure.string :refer [split join]] :reload-all)

    ;; Everything together
    (:require [clojure.string :as cs :refer [split join]] :verbose :reload))
  2. Baishampayan Ghose revised this gist Jan 27, 2010. 1 changed file with 0 additions and 13 deletions.
    13 changes: 0 additions & 13 deletions ns-cheatsheet.clj
    Original file line number Diff line number Diff line change
    @@ -11,19 +11,6 @@
    ;; * :refer-clojure affects availability of built-in (clojure.core)
    ;; functions.
    ;;
    ;; Rules of thumb:
    ;;
    ;; * Use lists to reduce repetition in namespace paths. These are
    ;; equivalent:
    ;;
    ;; [:require clojure.contrib.math clojure.contrib.seq-utils]
    ;; [:require [clojure.contrib math seq-utils]]
    ;;
    ;; * Use vectors for keyword options. Examples (which are equivalent):
    ;;
    ;; [:use [clojure.contrib.math :only [sqrt]]]
    ;; [:use [clojure.contrib [math :only [sqrt]]]]
    ;;

    (ns foo.bar

  3. @ghoseb ghoseb revised this gist Jan 27, 2010. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions ns-cheatsheet.clj
    Original file line number Diff line number Diff line change
    @@ -16,13 +16,13 @@
    ;; * Use lists to reduce repetition in namespace paths. These are
    ;; equivalent:
    ;;
    ;; (:require clojure.contrib.math clojure.contrib.seq-utils)
    ;; (:require (clojure.contrib math seq-utils))
    ;; [:require clojure.contrib.math clojure.contrib.seq-utils]
    ;; [:require [clojure.contrib math seq-utils]]
    ;;
    ;; * Use vectors for keyword options. Examples (which are equivalent):
    ;;
    ;; (:use [clojure.contrib.math :only [sqrt]])
    ;; (:use (clojure.contrib [math :only [sqrt]]))
    ;; [:use [clojure.contrib.math :only [sqrt]]]
    ;; [:use [clojure.contrib [math :only [sqrt]]]]
    ;;

    (ns foo.bar
    @@ -31,53 +31,53 @@

    ;; Refers clojure.contrib.math into current NS, e.g.:
    ;; (clojure.contrib.math/sqrt 4)
    (:require (clojure.contrib math))
    [:require [clojure.contrib math]]

    ;; Refers math into current NS, e.g.:
    ;; (math/sqrt 4)
    (:require (clojure.contrib [math :as math]))
    [: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]))
    [: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))
    [: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]]))
    [: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]])
    [: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}])
    [:use [clojure.contrib.math :rename {sqrt ccm-sqrt}]]

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; Refers ArrayList and HashMap into current NS:
    (:import (java.util ArrayList HashMap))
    [:import [java.util ArrayList HashMap]]

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;; Excludes built-in print
    (:refer-clojure :exclude [print])
    [:refer-clojure :exclude [print]]

    ;; Excludes all built-ins except print
    (:refer-clojure :only [print])
    [:refer-clojure :only [print]]

    ;; Renames built-in print to core-print
    (:refer-clojure :rename {print core-print}))
    [:refer-clojure :rename {print core-print}])
  4. @jkk jkk created this gist Jan 22, 2010.
    83 changes: 83 additions & 0 deletions ns-cheatsheet.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    ;;
    ;; 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.
    ;;
    ;; Rules of thumb:
    ;;
    ;; * Use lists to reduce repetition in namespace paths. These are
    ;; equivalent:
    ;;
    ;; (:require clojure.contrib.math clojure.contrib.seq-utils)
    ;; (:require (clojure.contrib math seq-utils))
    ;;
    ;; * Use vectors for keyword options. Examples (which are equivalent):
    ;;
    ;; (:use [clojure.contrib.math :only [sqrt]])
    ;; (:use (clojure.contrib [math :only [sqrt]]))
    ;;

    (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}))