Public Vars

Back

namespace (clj)

(source)

function

(namespace x)
Returns the namespace String of a symbol or keyword, or nil if not present.

Examples

clojure
(deftest division
  (is (= clojure.core// /))
  (binding [*ns* *ns*]
    (eval '(do (ns foo
                 (:require [clojure.core :as bar])
                 (:use [clojure.test]))
               (is (= clojure.core// bar//))))))

(require '[clojure.string :as s])
(deftest namespaced-maps
  (is (= #:a{1 nil, :b nil, :b/c nil, :_/d nil}
         #:a {1 nil, :b nil, :b/c nil, :_/d nil}
         {1 nil, :a/b nil, :b/c nil, :d nil}))
  (is (= #::{1 nil, :a nil, :a/b nil, :_/d nil}
         #::  {1 nil, :a nil, :a/b nil, :_/d nil}
         {1 nil, :clojure.test-clojure.reader/a nil, :a/b nil, :d nil} ))
  (is (= #::s{1 nil, :a nil, :a/b nil, :_/d nil}
         #::s  {1 nil, :a nil, :a/b nil, :_/d nil}
         {1 nil, :clojure.string/a nil, :a/b nil, :d nil}))
  (is (= (read-string "#:a{b 1 b/c 2}") {'a/b 1, 'b/c 2}))
  (is (= (binding [*ns* (the-ns 'clojure.test-clojure.reader)] (read-string "#::{b 1, b/c 2, _/d 3}")) {'clojure.test-clojure.reader/b 1, 'b/c 2, 'd 3}))
  (is (= (binding [*ns* (the-ns 'clojure.test-clojure.reader)] (read-string "#::s{b 1, b/c 2, _/d 3}")) {'clojure.string/b 1, 'b/c 2, 'd 3})))

(deftest namespaced-map-errors
  (are [err msg form] (thrown-with-msg? err msg (read-string form))
                      Exception #"Invalid token" "#:::"
                      Exception #"Namespaced map literal must contain an even number of forms" "#:s{1}"
                      Exception #"Namespaced map must specify a valid namespace" "#:s/t{1 2}"
                      Exception #"Unknown auto-resolved namespace alias" "#::BOGUS{1 2}"
                      Exception #"Namespaced map must specify a namespace" "#: s{:a 1}"
                      Exception #"Duplicate key: :user/a" "#::{:a 1 :a 2}"
                      Exception #"Duplicate key: user/a" "#::{a 1 a 2}"))

(deftest namespaced-map-edn
  (is (= {1 1, :a/b 2, :b/c 3, :d 4}
         (edn/read-string "#:a{1 1, :b 2, :b/c 3, :_/d 4}")
         (edn/read-string "#:a {1 1, :b 2, :b/c 3, :_/d 4}"))))
jonase/eastwood

;; Clojure 1.6.0 with the :require below causes clojure.core/load-lib
;; to be called with the arguments: prefix=nil, lib=eastwood.util,
;; options=(:as :require true). When it tries to call (apply hash-map
;; option) with an odd number of arguments, it throws the exception:

;; These examples are from namespace clojure.tools.analyzer.jvm
arohner/spectrum
(ns spectrum.core-specs
  (:require [clojure.core :as core]
            [clojure.spec.alpha :as s]
            [spectrum.core :as st]
            [spectrum.types :as t]
            [spectrum.util :refer [def-instance-predicate]]))

;;; specs for clojure.core fns, should only be used in cases where
;;; inference can't work, i.e. mostly on things that are built in,
;;; i.e. not defined in clojure source.
(def-instance-predicate namespace? clojure.lang.Namespace)

(s/fdef clojure.core/in-ns :args (s/cat :ns symbol?) :ret namespace?)
(s/fdef clojure.core/list :args (s/* any?) :ret list?)

(st/var-spec #'clojure.core/*ns* #'namespace?)
(st/var-spec #'clojure.core/*file* #'string?)
(st/var-spec #'clojure.core/*print-dup* #'boolean?)
(st/var-spec #'clojure.core/*unchecked-math* #'boolean?)
(st/var-spec #'clojure.core/*agent* (t/or-t [(t/class-t clojure.lang.Agent) (t/value-t nil)]))
(st/var-spec #'clojure.core/*warn-on-reflection* #'boolean?)
dvingo/dv.fulcro-template
(ns {{namespace}}.task.resolvers
  (:require
    [clojure.core.async :refer [go <! <!! >! >!! chan]]
    [clojure.pprint :refer [pprint]]
    [com.wsscode.pathom.connect :as pc]
    [{{namespace}}.user.model :as user]
    [{{namespace}}.user.db :as user.db]
    [{{namespace}}.task.model :as task]
    [{{namespace}}.task.db :as task.db]
    [dv.fulcro-util :as fu]
    [dv.xtdb-util :as xu]
    [taoensso.timbre :as log]))

(pc/defmutation create-task-mutation
  [{:keys [current-user xtdb-node]}
   {::task/keys [id description] :as props}]
  {::pc/sym '{{namespace}}.task/create-task}
  (let [user-tasks (::user/tasks current-user)]
    (log/info "props: ") (pprint props)
    (or
      (validity-check
        (not current-user) "You must be logged in to create a task."
        (not (task/valid? props)) "Task is invalid")