How to "print" to stdout from the repl without a nil at the end?

1,580 views
Skip to first unread message

Frank Siebenlist

unread,
Dec 30, 2011, 2:38:16 PM12/30/11
to Clojure, Frank Siebenlist
I'm trying to write some filters in clojure from stdin to stdout, but I have this nil at the end of the stream, which is what print returns.

As an alternative, I can return strings, but then I have the surrounding "s.

Or I could end by printing (symbol ""), like (do (print "hi")(symbol "")) which kind of gives me what I'm looking for but seems to include a newline at the end.

Any other suggestions maybe?

Thanks, Frank.

gaz jones

unread,
Dec 30, 2011, 2:51:57 PM12/30/11
to clo...@googlegroups.com
are you sure you're not just seeing the result of the function call in
the repl? for example this:

(ns filter.core
(:require [clojure.string :as string])
(:gen-class))

(defn -main
[& args]
(loop []
(when-let [line (read-line)]
(println (string/upper-case line))
(recur))))

when run, produces:

$ echo -e 'foo\nbar\nbaz' | java -cp lib/*:filter-1.0.0-SNAPSHOT.jar filter.core
FOO
BAR
BAZ
$

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Frank Siebenlist

unread,
Dec 30, 2011, 3:56:51 PM12/30/11
to clo...@googlegroups.com, Frank Siebenlist
Yes - I do get the result of the print function in the repl (i.e. nil), and I'm trying to see how I can somehow prevent that nil from printing to stdout.

I understand that you do not see any nil's in your example as you stay inside of your loop and there is no result only side effects.
(which is the same reason why the printing of the repl-prompt doesn't add an additional nil as it is called in the repl-loop…)

(…actually wonder why you wouldn't get one printed nil or false when you exit the loop when the when-let doesn't yield truthy anymore as that would be the final result… maybe that -main function is special… (?))

-Frank.

gaz jones

unread,
Dec 30, 2011, 4:31:46 PM12/30/11
to clo...@googlegroups.com
> Yes - I do get the result of the print function in the repl (i.e. nil), and I'm trying to see how I can somehow prevent that nil from printing to stdout.

Yeah, the point I was making is that you are always going to see that
'nil' printed at the repl because it always evaluates and prints the
result of the function, where as in reality that 'nil' would never get
written to stdout (unless you explicitly captured it and wrote it
yourself). It's the repl that's special - not the -main function.

Frank Siebenlist

unread,
Dec 30, 2011, 5:07:34 PM12/30/11
to clo...@googlegroups.com, Frank Siebenlist
Looking at the clojure.main/repl code, I can see that they print the result value to stdout, and that I can change that by assigning a different function to the :print option at repl-startup.

It's a little tricky, but that does allow me to turn the printing of results on/off, which would give me what I was looking for.

Thanks for your help!

-Frank.

Reply all
Reply to author
Forward
0 new messages