Back
cl-format (clj)
(source)function
(cl-format writer format-in & args)
An implementation of a Common Lisp compatible format function. cl-format formats its
arguments to an output stream or string based on the format control string given. It
supports sophisticated formatting of structured data.
Writer is an instance of java.io.Writer, true to output to *out* or nil to output
to a string, format-in is the format control string and the remaining arguments
are the data to be formatted.
The format control string is a string to be output with embedded 'format directives'
describing how to format the various arguments passed in.
If writer is nil, cl-format returns the formatted result string. Otherwise, cl-format
returns nil.
For example:
(let [results [46 38 22]]
(cl-format true "There ~[are~;is~:;are~]~:* ~d result~:p: ~{~d~^, ~}~%"
(count results) results))
Prints to *out*:
There are 3 results: 46, 38, 22
Detailed documentation on format control strings is available in the "Common Lisp the
Language, 2nd edition", Chapter 22 (available online at:
http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/clm/node200.html#SECTION002633000000000000000)
and in the Common Lisp HyperSpec at
http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm
Examples
manwar/perlweeklychallenge-club
(ns c157.t1-test
(:require [clojure.test :refer [deftest is testing]]
[clojure.pprint :refer [cl-format]]
[c157.t1 :refer [pythagorean-means]]))
(def calc (comp (partial map #(cl-format nil "~,1f" %)) pythagorean-means))
reborg/clojure-essential-reference
(require '[clojure.pprint :refer [cl-format]])
(apply cl-format nil num-sentence [1 2]) ;; "1 and 2."
(apply cl-format nil num-sentence [1 2 3]) ;; "1, 2 and 3."
(apply cl-format nil num-sentence [1 2 3 4]) ;; "1, 2, 3, etc."
(cl-format nil pluralize 0) ;; "I see no fishes."
(cl-format nil pluralize 1) ;; "I see one fish."
(cl-format nil pluralize 100) ;; "I see one hundred fishes."
reborg/clojure-essential-reference
(require '[clojure.pprint :refer [cl-format]]) ; <1>
(cl-format nil "~:d" 1000000) ;; "1,000,000" ; <2>
(cl-format nil "~b" 10) ;; "1010" ; <3>
(cl-format nil "Anno Domini ~@r" 25) ;; "Anno Domini XXV" ; <4>
(cl-format nil "~r" 158) ;; "one hundred fifty-eight" ; <5>
(cl-format nil "~:r and ~:r" 1 2) ;; "first and second" ; <6>
(cl-format nil "~r banana~:p" 1) ;; "one banana"
(cl-format nil "~r banana~:p" 2) ;; "two bananas" ; <7>
reborg/clojure-essential-reference
(require '[clojure.pprint :refer [cl-format]]) ; <1>
(cl-format nil "~:d" 1000000) ;; "1,000,000" ; <2>
(cl-format nil "~b" 10) ;; "1010" ; <3>
(cl-format nil "Anno Domini ~@r" 25) ;; "Anno Domini XXV" ; <4>
(cl-format nil "~r" 158) ;; "one hundred fifty-eight" ; <5>
(cl-format nil "~:r and ~:r" 1 2) ;; "first and second" ; <6>
(cl-format nil "~r banana~:p" 1) ;; "one banana"
(cl-format nil "~r banana~:p" 2) ;; "two bananas" ; <7>
SVMBrown/weaver
(ns weaver.processors.format
(:require
[weaver.interop :as x]
[weaver.processors.multi :refer [pre-process-node process-node]]
[clojure.pprint :as pp]))
(defmethod pre-process-node [:vector "format"] [[kw & args]]
(case kw
:format/cl-format {:weaver.processor/id :format/cl-format
:string (first args)
:args (rest args)}
{:weaver.processor/id kw
:args args}))
(defmethod process-node :format/cl-format [_ {:keys [string args] :as node}]
(cond
(not (string? string)) (x/warn-and-exit (ex-info "String argument to :format/cl-format must be a string"
{:node node}))
(not (coll? args)) (x/warn-and-exit (ex-info ":args argument to :format/cl-format must be a collection"
{:node node}))
:else (apply pp/cl-format false string args)))