1

I'm wondering why the following have different output orders in an nREPL

(map println [1 2 3])

Result:
1
2
3
(nil nil nil)

Versus

(map print [1 2 3])

Result:
(nil nil nil)123

Why does applying print show the return value and then display 123?

Weebs
  • 435
  • 6
  • 17

3 Answers3

2

Also to note, that this works in REPL, in the code you need to use (dorun), as map produces a lazy sequence, and dorun actually forces print to happen:

(dorun (map print [1 2 3])) ;=> 123
Aliaksandr Kazlou
  • 3,221
  • 6
  • 28
  • 34
1

Actually, you may see a different order if you run the second one multiple times. print does not print any newlines, so the output buffer is not flushed. You could very well also see:

Result:
123(nil nil nil)

I suppose the first example could possibly change order, too, but the REPL has *flush-on-newline* set to true by default.

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
0

It looks pretty much like result of output stream buffering.

You could forcibly print all data in output stream buffer by calling flush function:

(defn print! [& args]
  (apply print args)
  (flush))

(map print! [1 2 3])
; => 123(nil nil nil)
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122