Back

write (clj)

(source)

function

(write object & kw-args)
Write an object subject to the current bindings of the printer control variables. Use the kw-args argument to override individual variables for this call (and any recursive calls). Returns the string result if :stream is nil or nil otherwise. The following keyword arguments can be passed with values: Keyword Meaning Default value :stream Writer for output or nil true (indicates *out*) :base Base to use for writing rationals Current value of *print-base* :circle* If true, mark circular structures Current value of *print-circle* :length Maximum elements to show in sublists Current value of *print-length* :level Maximum depth Current value of *print-level* :lines* Maximum lines of output Current value of *print-lines* :miser-width Width to enter miser mode Current value of *print-miser-width* :dispatch The pretty print dispatch function Current value of *print-pprint-dispatch* :pretty If true, do pretty printing Current value of *print-pretty* :radix If true, prepend a radix specifier Current value of *print-radix* :readably* If true, print readably Current value of *print-readably* :right-margin The column for the right margin Current value of *print-right-margin* :suppress-namespaces If true, no namespaces in symbols Current value of *print-suppress-namespaces* * = not yet supported

Examples

clojure

(ns clojure.test-clojure.java-interop
  (:use clojure.test)
  (:require [clojure.data :as data]
            [clojure.inspector]
            [clojure.pprint :as pp]
            [clojure.set :as set]
            [clojure.test-clojure.proxy.examples :as proxy-examples])
  (:import java.util.Base64
           (java.util.concurrent.atomic AtomicLong AtomicInteger)))

;; serialized-proxy can be regenerated using a modified version of
;; Clojure with the proxy serialization prohibition disabled and the
;; following code:
;; revert 271674c9b484d798484d134a5ac40a6df15d3ac3 to allow serialization
(comment
  (require 'clojure.inspector)
  (let [baos (java.io.ByteArrayOutputStream.)]
    (with-open [baos baos]
      (.writeObject (java.io.ObjectOutputStream. baos) (clojure.inspector/list-model nil)))
    (prn (vector (System/getProperty "java.specification.version")
                 (.encodeToString (java.util.Base64/getEncoder) (.toByteArray baos))))))

(deftest test-proxy-non-serializable
  (testing "That proxy classes refuse serialization and deserialization"
    ;; Serializable listed directly in interface list:
    (is (thrown? java.io.NotSerializableException
                 (-> (java.io.ByteArrayOutputStream.)
                     (java.io.ObjectOutputStream.)
                     (.writeObject (proxy [Object java.io.Serializable] [])))))
    ;; Serializable included via inheritence:
    (is (thrown? java.io.NotSerializableException
                 (-> (java.io.ByteArrayOutputStream.)
                     (java.io.ObjectOutputStream.)
                     (.writeObject (clojure.inspector/list-model nil)))))
    ;; Deserialization also prohibited:
    (let [java-version (System/getProperty "java.specification.version")
          serialized-proxy (get serialized-proxies java-version)]
      (if serialized-proxy
        (is (thrown? java.io.NotSerializableException
                     (-> serialized-proxy
                         decode-base64
                         java.io.ByteArrayInputStream. java.io.ObjectInputStream.
                         .readObject)))
        (println "WARNING: Missing serialized proxy for Java" java-version "in test/clojure/test_clojure/java_interop.clj")))))
clojure
  "(ns autodoc.build-html
  \"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
  {:skip-wiki true, :author \"Tom Faulhaber\"}
  (:refer-clojure :exclude [empty complement])
  (:import [java.util.jar JarFile]
           [java.io File FileWriter BufferedWriter StringReader
                    BufferedInputStream BufferedOutputStream
                    ByteArrayOutputStream FileReader FileInputStream]
           [java.util.regex Pattern])
  (:require [clojure.string :as str])
  (:use [net.cgrand.enlive-html :exclude (deftemplate)]
        [clojure.java.io :only (as-file file writer)]
        [clojure.java.shell :only (sh)]
        [clojure.pprint :only (pprint cl-format pprint-ident
                               pprint-logical-block set-pprint-dispatch
                               get-pretty-writer fresh-line)]
        [clojure.data.json :only (pprint-json)]
        [autodoc.collect-info :only (contrib-info)]
        [autodoc.params :only (params expand-classpath)])
  (:use clojure.set clojure.java.io clojure.data clojure.java.browse
        clojure.inspector clojure.zip clojure.stacktrace))")

(defn tst-pprint
  "A helper function to pprint to a string with a restricted right margin"
  [right-margin obj]
  (binding [*print-right-margin* right-margin
            *print-pretty* true]
    (write obj :stream nil)))

(defmethod test-dispatch true [avec]
  (pprint-logical-block :prefix "[" :suffix "]"
    (loop [aseq (seq avec)]
      (when aseq
	(write-out (first aseq))
	(when (next aseq)
	  (.write ^java.io.Writer *out* " ")
	  (pprint-newline :linear)
	  (recur (next aseq)))))))

(defn- flush-alerting-writer
  [o]
  (let [flush-count-atom (atom 0)]
    [
      (proxy [java.io.BufferedWriter] [o]
        (flush []
          (proxy-super flush)
          (swap! flush-count-atom inc)))
      flush-count-atom]))

(deftest test-flush-underlying-prn
  []
  (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
    (binding [*out* out
              *flush-on-newline* true]
      (prn (range 50))
      (prn (range 50)))
    (is (= @flush-count-atom 2) "println flushes on newline")))

(deftest test-flush-underlying-pprint
  []
  (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
    (binding [*out* out
              *flush-on-newline* true]
      (pprint (range 50))
      (pprint (range 50)))
    (is (= @flush-count-atom 2) "pprint flushes on newline")))

(deftest test-noflush-underlying-prn
  []
  (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
    (binding [*out* out
              *flush-on-newline* nil]
      (prn (range 50))
      (prn (range 50)))
    (is (= @flush-count-atom 0) "println flushes on newline")))

(deftest test-noflush-underlying-pprint
  []
  (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
    (binding [*out* out
              *flush-on-newline* nil]
      (pprint (range 50))
      (pprint (range 50)))
    (is (= @flush-count-atom 0) "pprint flushes on newline")))
epiccastle/spire
(def namespaces
  {
   'clojure.core {'slurp slurp
                  'spit spit
                  'future (with-meta @#'clojure.core/future {:sci/macro true})
                  'future-call clojure.core/future-call
                  '*in* (sci/new-dynamic-var '*in* *in*)
                  '*out* (sci/new-dynamic-var '*out* *out*)
                  '*err* (sci/new-dynamic-var '*err* *err*)
                  }
   'clojure.main {'repl-requires
                  '[[clojure.repl :refer [dir doc]]
                    [clojure.pprint :refer [pprint]]
                    [spire.default :refer [push-ssh! set-ssh!
                                           push-local! set-local!
                                           pop! empty!]]]}
   'clojure.pprint (make-sci-bindings fipp.edn)
   'clojure.repl clojure-repl
   'clojure.stacktrace {'root-cause stacktrace/root-cause
                        'print-trace-element (redirect-out-to-sci stacktrace/print-trace-element)
                        'print-throwable (redirect-out-to-sci stacktrace/print-throwable)
                        'print-stack-trace (redirect-out-to-sci stacktrace/print-stack-trace)
                        'print-cause-trace (redirect-out-to-sci stacktrace/print-cause-trace)}

   'clojure.java.io (make-sci-bindings clojure.java.io)
   'clojure.tools.cli (make-sci-bindings clojure.tools.cli)
   'clojure.set (make-sci-bindings clojure.set)
   'clojure.string (make-sci-bindings clojure.string)
   'clojure.data.json (make-sci-bindings clojure.data.json
                                         {:only #{read-json read-str read
                                                  write-json write-str write}})
chr15m/sitefox
(ns update-deps
  (:require
    ["fs" :as fs]
    [clojure.edn :as edn]
    [clojure.pprint :refer [pprint]]))

(let [package (js/require "../package.json")
      js-deps (js->clj (aget package "dependencies"))
      deps (edn/read-string (fs/readFileSync "src/deps.cljs" "utf8"))
      deps-updated (assoc deps :npm-deps js-deps)]
  (binding [*print-fn* (fn [s]
                         (fs/writeFileSync "src/deps.cljs" s))]
    (pprint deps-updated)))