Fork me on GitHub
#tools-deps
<
2018-09-15
>
seancorfield00:09:57

@jcf Is this still too much code?

(! 550)-> echo 4 | clj -e '(-> (read-line) (Long/parseLong) inc)'
5

seancorfield00:09:47

If you want to read every line:

(! 552)-> (echo 4; echo 13) | clj -e '(while (some-> (read-line) (Long/parseLong) (inc) (doto (println))))'
5
14

👍 8
jcf00:09:31

Definitely more terse than my snippet. I like the use of read-line and a threading macro for sure! I think we're missing a trick still by not having the switches provided by Ruby. The whole loop-and-read boilerplate becomes redundant when you pass -n, and with -p you don't have to print…

seancorfield00:09:05

Clojure is a fundamentally different execution model tho': you're building a classpath, firing up a JVM, compiling a set of forms, and then running the compiled code.

seancorfield00:09:54

With Ruby, it can just take the expression and interpret it repeatedly...

seancorfield00:09:02

...for Clojure, you'd need some in-JVM process that takes your expression and does the looping/reading for you -- you can't do it at the script level.

seancorfield01:09:42

OK, @jcf how about this for Ruby-like conciseness?

(! 584)-> (echo 4; echo 13) | clj -Apne -e '($ (inc (Long/parseLong $_)))'
#'user/$
5
14

💯 8
seancorfield01:09:42

OK, @jcf how about this for Ruby-like conciseness?

(! 584)-> (echo 4; echo 13) | clj -Apne -e '($ (inc (Long/parseLong $_)))'
#'user/$
5
14

💯 8
seancorfield01:09:41

$ is a macro defined by the pne alias that takes the following expression and evaluates and prints it repeatedly in the context of $_ bound to subsequent (read-line) calls...

seancorfield01:09:35

As for the pne alias... be afraid... be very afraid...

(! 585)-> cat deps.edn 
{:aliases,{:pne,{:main-opts,["-e","(defmacro,$,[&,body],(list,'while,(list,'when-let,'[$_,(read-line)],(let,[v,(gensym)],(list,'let,(vector,v,(list*,'do,body)),(list,'println,v))),'$_)))"]}}}

seancorfield01:09:35

(you can't use backtick easily with clj due to bash issues so you have to laboriously build the entire body with list and vector -- and you have to use , for whitespace in :main-opts because of reasons... 🙂 )

jcf07:09:12

That is gnarly, but I love the novelty of your solution. I'm guessing a solution with similar behaviour to this won't land in tools.deps, but you've inspirited me to think about writing a small library that adds this functionality. Thanks, @seancorfield!

dottedmag06:09:17

@seancorfield Maybe instead of that one can add a dependency that populates user and make :main-opts require it?

seancorfield06:09:48

@dottedmag I'm sure there are several ways to solve the problem... this was just the first thing that popped into my head 🙂

seancorfield06:09:18

Feel free to post your solution too...