9 ms·
looks even better with threading in imaginary lisp dialect: (-> read eval print loop)
by keymone 8y ago
looks even better with threading in imaginary lisp dialect:
(-> read eval print loop)
- earenndil 8y agoI think clojure has that; I could be wrong, though, I've never used clojure.
- intertextuality 8y agoClojure does indeed have -> (thread-first), ->> (thread-last), some->, some->>, cond->, and as->. [0] (->) inserts each form's value between the function name & the first argument in the next form. (->>) inserts each form's value as the last argument in the next form. The latter forms are much more niche and I haven't found need for them yet. [0]: https://clojure.org/guides/threading_macros https://clojure.org/guides/threading_macros
- iterati 8y agoas-> is great for when you have to mix thread-first and thread-last. cond-> is great for building maps where some keys might not be needed: (cond-> {:k1 v1} v2 (assoc :k2 v2) ...) some-> and some->> I don't use as often, but when mixing in some java code that could throw an NPE, these will avoid that and just return nil early (they short-circuit when getting a nil value).
- dustingetz 8y agoYou can next ->> inside ->, took far too many years for me to realize: @(-> ctx :hypercrud.browser/fiddle (reactive/cursor [:fiddle/ident]) (->> (reactive/fmap name))) another (-> [10 11] (conj 12) (as-> xs (map - xs [3 2 1])) (reverse))
- scns 8y agoBucklescript/ReasonML have that too: CLJ:(->)/BS(|.)/RML(->) called Fast Pipe. CLJ:(->>)/BS(|>)/RML(|>) called Pipe. plus some nifty placeholders for other positions: https://reasonml.github.io/docs/en/fast-pipe#pipe-placeholders https://reasonml.github.io/docs/en/fast-pipe#pipe-placeholde...
- keymone 8y agoIt does, sort of. As another commenter pointed out, -> chains the operands at the beginning, but also ‘loop’ form requires list of bindings. It is totally possible to write custom -> macro that works correctly as used in my example.
- samatman 8y agoAgree to disagree; to write lisp one must know how to read it. The threading macro is great, but the GP one-liner tells me more about lisp.
- gnulinux 8y agoLisp is all about being able to compile the latter to the former in compile-time, if you want it. That's the major problem lisp solves.
- samatman 8y agoHard agree! Still one is the latter and the other, the former. Hence I think the first tells more of the lisp story, with just a few more parens. ;-) "Why is it called REPL when the code says LPER? Well..."
- kazinator 8y agoWhy is it called "root mean square" when you square first, then take the mean, and then the root?
- ves 8y agoWe can just cut straight to “why is function composition backwards?”
- gnulinux 8y ago`(root (mean (square x)))` Is this more readable? `x ~> square . mean . root` Where `~>` "sends" a value to a function `x ~> f := f(x)` and `.` is the "backwards" function composition `f . g := \x (g (f x)) I think I prefer the "backwards" notation ("root mean square"), but I can see the appeal of "square mean root"
- Silfen 8y ago
- aidenn0 8y ago(defmacro -> (&rest args) (loop for item in (reverse args) for result = (list item) then (list item result) finally (return result)))
- keymone 8y agoNo longer imaginary. Power of LISP right there.