concept println in category clojure

appears as: println
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

Listing 2.2. custom-metadata-annotations
(ns profilable)

(defn ^:bench profile-me [ms] ; #1
  (println "Crunching bits for" ms "ms")
  (Thread/sleep ms))

(defn dont-profile-me [ms]
  (println "not expecting profiling"))

(ns user)

(defn- wrap [f]
  (fn [& args]
    (time (apply f args))))

(defn- make-profilable [v]
  (alter-var-root v (constantly (wrap @v))))

(defn- tagged-by [tag nsname]
  (->> (ns-publics nsname)
       vals
       (filter #(get (meta %) tag))))

(defn prepare-bench [nsname] ; #2
  (->> (tagged-by :bench nsname)
       (map make-profilable)
       dorun))

(profilable/profile-me 500) ; #3
;; Crunching bits for 500 ms

(prepare-bench 'profilable)

(profilable/profile-me 500) ; #4
;; Crunching bits for 500 ms
;; "Elapsed time: 502.422309 msecs"

(profilable/dont-profile-me 0) ; #5
;; not expecting profiling

format, printf and cl-format are functions dedicated to string formatting. format is a wrapper around Java’s String::format method, which is inspired by the venerable printf function from the C language. In Clojure printf is a small function wrapping println with format.

Get Programming with Clojure

This is an excerpt from Manning's book Get Programming with Clojure.

In order to print to the console in Clojure, we use mostly the  println functions. It works in a similar way to the printing functions that your are familiar with from other programming languages, but they are some subtle differences that we will detail in this Lesson.

println receive any number of arguments and print them to the console with a whitespace character between them and a new line character at the end of the output (println stands for “print line”). Here are some examples of print expressions:

(println "Hello")
 (println "How " "are " "you?")
 (println "I am " 42 " years old.")

Let’s first explore how println behaves when the arguments are all strings. Later on this section, we will deal with non-strings arguments.

When we call println in the REPL, we see both the output produced by the function and nil at the end of the output, which probably looks weird to you:

The first line is the output of println (the side effects), while the second line is the return value of println (return value).

KEY POINT

When an expression is evaluated in the REPL, the REPL, displays the output of the expression (side effects) and the return value of the expression.

Let’s take some time to clarify it further by drawing a flow diagram (Fig 16.1) for a println expression.

Fig 16.1 - Flow diagram for println receiving 42 as an input

KEY POINT

The distinction between side effects and return value is a key idea of functional programming.

Another way to clarify the distinction between side effects and return value is to assign the result of a println expression into a variable and to look at the value of the variable:

user=> (def my-var (println "hello"))
 hello
 nil

The value of my-var is the value returned by (println "hello"), which is nil. Therefore, when we evaluate my-var in the REPL we see nil:

user=> my-var
 nil

In my opinion, println is a boring function in a sense that its return value is always nil. Let’s make our own println function that is a bit more interesting. Our function will not only print to the console but also returns a value that is not nil. We  call it println-and-return: it receives a single argument, displays it to the console and returns it. Here is the code for println-and-return:

user=> (defn println-and-return [x]
   #_=>   (println x)      #A
   #_=>   x)               #B
 Created function println-and-return ([x])