Fork me on GitHub
#planck
<
2017-02-03
>
gamecubate16:02:49

Can planck handle stdin when run as a (Unix) shell script?

gamecubate16:02:33

Would like to create a clojure equivalent to, say, “wc -l” (count lines in stream) Unix command. Tried this:

gamecubate16:02:35

which fails.

mfikes16:02:43

@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*)))

gamecubate16:02:47

Sorry, was writing from scratch; silly mistake but script still fails.

gamecubate16:02:52

Am doing more tests

gamecubate16:02:28

Now this one works:

gamecubate16:02:05

I think that could be it.

gamecubate16:02:12

$ cat /etc/services | wcl
returns same as Unix “wc -l” command.

gamecubate16:02:19

So, all good.

mfikes16:02:34

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.

gamecubate16:02:35

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.

mfikes16:02:22

Yeah, to address your ticket, perhaps we can add some more to the http://planck-repl.org site content

mfikes16:02:47

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.

gamecubate16:02:04

Would be really helpful to help Unix scripters like I.

gamecubate16:02:18

Took a while (30 mins or so) to get it right.

gamecubate16:02:37

Doc would have shaved that to 1. 🙂

gamecubate16:02:10

Yes, exactly.

gamecubate16:02:04

Thanks for a great tool BTW

gamecubate16:02:38

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

mfikes16:02:44

You might want to use the reader to parse the integers

mfikes16:02:48

(require '[cljs.tools.reader :as r])
(r/read-string “1”)

gamecubate16:02:40

What if string = “1 2 3”. Assuming I will have to split it then map r/read-string over each element.

mfikes16:02:05

(clojure.string/split "1 2 3" #" “)

mfikes16:02:14

Exactly 🙂

gamecubate16:02:24

(reduce + (map r/read-string (clojure.string/split “1 2 3”)))

gamecubate16:02:45

Ah yes. Thanks a bunch.

gamecubate17:02:13

cat nums.txt | tally
will return
6
15
24

gamecubate17:02:24

where tally = script above.