This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-15
Channels
- # 100-days-of-code (7)
- # beginners (58)
- # boot (1)
- # cider (12)
- # clojure (69)
- # clojure-nl (1)
- # clojure-serbia (1)
- # clojure-spec (34)
- # clojure-uk (3)
- # clojurescript (14)
- # clojutre (3)
- # core-async (1)
- # core-logic (6)
- # cursive (44)
- # datomic (1)
- # defnpodcast (1)
- # emacs (16)
- # figwheel-main (3)
- # fulcro (14)
- # immutant (3)
- # leiningen (9)
- # mount (1)
- # nrepl (19)
- # off-topic (4)
- # pedestal (2)
- # re-frame (73)
- # ring (2)
- # shadow-cljs (40)
- # tools-deps (14)
- # yada (4)
@jcf Is this still too much code?
(! 550)-> echo 4 | clj -e '(-> (read-line) (Long/parseLong) inc)'
5
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
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…
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.
With Ruby, it can just take the expression and interpret it repeatedly...
...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.
OK, @jcf how about this for Ruby-like conciseness?
(! 584)-> (echo 4; echo 13) | clj -Apne -e '($ (inc (Long/parseLong $_)))'
#'user/$
5
14
OK, @jcf how about this for Ruby-like conciseness?
(! 584)-> (echo 4; echo 13) | clj -Apne -e '($ (inc (Long/parseLong $_)))'
#'user/$
5
14
$
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...
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))),'$_)))"]}}}
(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... 🙂 )
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!
@seancorfield Maybe instead of that one can add a dependency that populates user
and make :main-opts
require
it?
@dottedmag I'm sure there are several ways to solve the problem... this was just the first thing that popped into my head 🙂
Feel free to post your solution too...