planck

Planck ClojureScript REPL
gamecubate 2017-02-03T16:12:27.000492Z

Hi.

gamecubate 2017-02-03T16:12:49.000493Z

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

gamecubate 2017-02-03T16:13:33.000494Z

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

gamecubate 2017-02-03T16:15:35.000499Z

which fails.

mfikes 2017-02-03T16:32:43.000502Z

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

gamecubate 2017-02-03T16:33:47.000503Z

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

gamecubate 2017-02-03T16:33:52.000504Z

Am doing more tests

gamecubate 2017-02-03T16:35:28.000505Z

Now this one works:

gamecubate 2017-02-03T16:36:05.000507Z

I think that could be it.

gamecubate 2017-02-03T16:37:12.000508Z

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

gamecubate 2017-02-03T16:37:19.000509Z

So, all good.

mfikes 2017-02-03T16:38:34.000510Z

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.

gamecubate 2017-02-03T16:38:35.000511Z

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.

gamecubate 2017-02-03T16:38:49.000512Z

Great.

mfikes 2017-02-03T16:39:22.000513Z

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

mfikes 2017-02-03T16:40:47.000514Z

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.

gamecubate 2017-02-03T16:41:04.000517Z

Would be really helpful to help Unix scripters like I.

gamecubate 2017-02-03T16:41:18.000518Z

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

gamecubate 2017-02-03T16:41:37.000519Z

Doc would have shaved that to 1. 🙂

mfikes 2017-02-03T16:41:51.000520Z

Yeah, perhaps a section on stdin in here https://github.com/mfikes/planck/blob/master/site/src/scripts.md

gamecubate 2017-02-03T16:42:10.000523Z

Yes, exactly.

gamecubate 2017-02-03T16:43:04.000524Z

Thanks for a great tool BTW

gamecubate 2017-02-03T16:46:38.000525Z

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

mfikes 2017-02-03T16:47:44.000529Z

You might want to use the reader to parse the integers

gamecubate 2017-02-03T16:47:54.000530Z

Yes.

mfikes 2017-02-03T16:48:48.000531Z

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

gamecubate 2017-02-03T16:49:40.000532Z

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

mfikes 2017-02-03T16:50:05.000533Z

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

mfikes 2017-02-03T16:50:14.000534Z

Exactly 🙂

gamecubate 2017-02-03T16:50:24.000535Z

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

gamecubate 2017-02-03T16:50:45.000537Z

Ah yes. Thanks a bunch.

gamecubate 2017-02-03T17:02:40.000542Z

👍 @niamu

gamecubate 2017-02-03T17:07:27.000544Z

Better.

gamecubate 2017-02-03T17:31:13.000547Z

cat nums.txt | tally
will return
6
15
24

gamecubate 2017-02-03T17:31:24.000548Z

where tally = script above.