Non Dev Builds

41 views
Skip to first unread message

Andres Gomez

unread,
Nov 16, 2011, 11:17:51 AM11/16/11
to Clojure
Inspired by Hickey's keynote at the conj, I built a proof of concept
for a program to make non dev builds (remove comments, docstrings,
line breaks)

https://github.com/fractalmedia/prod-build

It is very simple, its most important function is: (defn read-file
[name] (eval (read-string (str "'(" (slurp name) ")"))))
Which strips comments and line breaks.

I'm sharing this for peer reviewing, once it is confirmed that it
works nice (or modified accordingly) i will wrap it as a lein plugin.

I have 2 questions regarding this:
1. is it more efficient to leave anonymous functions on the form of:
#(something %)
or:
(fn* [p1__69#] (something p1__69#))
(because read-file leaves them on the second form.

Also, can somebody confirm if the drop-doc is missing something?


Cheers

Meikel Brandmeyer

unread,
Nov 16, 2011, 6:17:40 PM11/16/11
to clo...@googlegroups.com
Hi,

Am 16.11.2011 um 17:17 schrieb Andres Gomez:

> It is very simple, its most important function is: (defn read-file
> [name] (eval (read-string (str "'(" (slurp name) ")"))))

As a minor nitpick to promote robust code… Please use something like this for reading:

(let [eof (Object.)] (take-while (complement #{eof}) (repeatedly #(read file-reader false eof))))

(str "(" ...) is so ugly and fragile. There are many files which do not end in a newline. And if the last line is a comment…

Sincerely
Meikel


Andres Gomez

unread,
Nov 16, 2011, 11:32:28 PM11/16/11
to Clojure
Thanks for the robustness tip, Meikel.

Just a question, i dont understand what you state, i dont think it
needs to end in a newline in order to work.

Ben Smith-Mannschott

unread,
Nov 17, 2011, 1:28:35 AM11/17/11
to clo...@googlegroups.com
== FILE ==
(def x 1) <NEWLINE>
; my comment
==

(str "'(" (slurp "FILE") ")" )

produces:

==
'((def x 1) <NEWLINE>
; my comment)
==

oops.

> --
> 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
>

Meikel Brandmeyer (kotarak)

unread,
Nov 17, 2011, 1:36:28 AM11/17/11
to clo...@googlegroups.com
Hi,

Consider this file. The * marks the not there \n.

Foo
; Bar*

And slurp and the str call, you get a string which looks like this "(Foo\n;Bar)". And this gives an error because the closing ) is in the comment.

Here you see the effect:

user=> (with-open [w (writer "x")] (binding [*out* w] (print "Foo\n;Bar") (flush)))
nil
user=> (read-string (str "(" (slurp "x") ")"))
RuntimeException EOF while reading  clojure.lang.Util.runtimeException (Util.java:156)
user=> (with-open [r (LineNumberingPushbackReader. (reader "x"))]
         (let [eof (Object.)]
           (->> #(read r false eof)
             repeatedly
             (take-while (complement #{eof}))
             doall)))
(Foo)
user=>

Sincerely
Meikel

Reply all
Reply to author
Forward
0 new messages