Hi.
Can planck handle stdin when run as a (Unix) shell script?
Would like to create a clojure equivalent to, say, “wc -l” (count lines in stream) Unix command. Tried this:
which fails.
@gamecubate You need a closing brace and closing paren:
#!/usr/bin/env planck
(require '[planck.core :as core])
(println (count (core/line-seq core/*in*)))
Sorry, was writing from scratch; silly mistake but script still fails.
Am doing more tests
Now this one works:
I think that could be it.
$ cat /etc/services | wcl
returns same as Unix “wc -l” command.So, all good.
Right. Planck 1.17 is based on ClojureScript 1.9.229, and in that version of ClojureScript, require
doesn’t exist (it is a REPL special). So, your solution works with Planck 1.17. On the other hand, the Planck 2.0.0 release candidate is based on a newer ClojureScript, where require
is a macro, and can be used as in your first example.
I had opened a git issue requesting some documentation on just that. Will edit it to include this code if you think it’s correct.
Great.
Yeah, to address your ticket, perhaps we can add some more to the http://planck-repl.org site content
In the updated site document for 2.0.0, require
has already been removed from the docs for Planck’s REPL specials (https://github.com/mfikes/planck/blob/master/site/src/repl.md#repl-specials), but perhaps an example showing your use of *in*
is still a good addition.
Would be really helpful to help Unix scripters like I.
Took a while (30 mins or so) to get it right.
Doc would have shaved that to 1. 🙂
Yeah, perhaps a section on stdin in here https://github.com/mfikes/planck/blob/master/site/src/scripts.md
Yes, exactly.
Thanks for a great tool BTW
Now trying something more demanding: a “sum stdin numbers” script. Something invoked like
echo 1 2 3 | tally
, where tally would be a planck script. This next script doesn’t work (of course):You might want to use the reader to parse the integers
Yes.
(require '[cljs.tools.reader :as r])
(r/read-string “1”)
What if string = “1 2 3”. Assuming I will have to split it then map r/read-string over each element.
(clojure.string/split "1 2 3" #" “)
Exactly 🙂
(reduce + (map r/read-string (clojure.string/split “1 2 3”)))
Ah yes. Thanks a bunch.
👍 @niamu
Better.
cat nums.txt | tally
will return 6
15
24
where tally = script above.