hyrule icon indicating copy to clipboard operation
hyrule copied to clipboard

Interest for some-> and some->> threading macros?

Open ekaschalk opened this issue 8 years ago • 11 comments

I have just finished implementing and writing tests for some-> and some->> threading macros for personal use.

Is there interest in adding these macros to hy.core.macros, before I PR?

I've found myself wanting for them often, even toolz has a PR open for a some thread.

ekaschalk avatar Dec 22 '17 21:12 ekaschalk

What do they do?

Kodiologist avatar Dec 22 '17 21:12 Kodiologist

It is a builtin threading macro in clojure, along with -> and cond->.

https://clojuredocs.org/clojure.core/some-%3E

If a form evaluates to None then short-circuit the thread and return None.

Eg.

(assert (none? (some-> None inc)))

and

(assert (none? (some-> 1 ((constantly None)) inc)))

ekaschalk avatar Dec 22 '17 21:12 ekaschalk

I'd support putting it in a hy.contrib or hy.extra module if you'd like. I wouldn't put it in core because core is a bit bloated right now and should have some things removed or moved to hy.extra, although, admittedly, the bloat is mostly from functions rather than macros.

Kodiologist avatar Dec 22 '17 21:12 Kodiologist

Flat is better than nested. --The Zen of Python.

I'm OK with having a fairly large core, but it has to have things that actually get used a lot, like Clojure's core. It's a pain to import these things every time. I'd support putting most of Clojure's core macros in Hy core. I'd also support putting most of Clojure's core functions in Hy extra or even Hy core if they're sufficiently important.

That said, I think a large core can make Hy harder to learn, because new users can't find the absolute essentials inside all the other useful stuff. Clojure had the same problem. The solution is probably better documentation highlighting these rather than more nested namespaces.

We do have -> ->> and as-> hylang/hy#1047. I also had a condp implementation in hylang/hy#1328 (but it doesn't belong there).

gilch avatar Dec 23 '17 22:12 gilch

It's a pain to import these things every time.

This is exasperated by require not being transitive so one cannot just collect all macro requires they use project-wide in a single module.

Hy harder to learn.

I see your point with core functions like trampoline and with-in-string.

I'd argue that some-> is core as the use-case its addressing, the same as the Maybe monad, is a core concept.

ekaschalk avatar Dec 23 '17 22:12 ekaschalk

This is exasperated by require not being transitive

I am reconsidering the current macro namespace implementation. hylang/hy#1416, we might be able to get transitivity by adding to the __macros__ object.

In the meantime, couldn't you write a macro that expands to all your require statements? Then you'd only have to require that (and invoke it). I haven't actually tried this, but I think it would work.

gilch avatar Dec 23 '17 23:12 gilch

couldn't you write a macro that expands to all your require statements?

Yes I realized that while writing that comment. It's a bit funny to have such a macro though.

ekaschalk avatar Dec 23 '17 23:12 ekaschalk

For anyone arriving at this Issue expecting to see Hylang's some->> or some-> implementations (as I did):

(defmacro some->> [expr #* forms]
  "When expr is not None, thread it to the first form (via ->>)
  and then when the result is not None, through the next, and so on."
  (setv g (gensym))
  (setv steps (map (fn [step] `(if (is ~g None)
                                 (setv ~g None)
                                 (setv ~g (->> ~g ~step))))
                   (if (is forms None)
                     []
                     forms)))
  `(do
     (setv ~g ~expr)
     ~@steps
     ~g))

This implementation is tested using the REPL:

=> (assert (= 1 (some->> 1)))
=> (assert (= None (some->> None)))
=> (assert (= 1 (some->> 1 (- 2))))
=> (assert (= None (some->> 1 ((fn [x] None)) (- 2))))

m1nhtu99-hoan9 avatar Jun 22 '21 20:06 m1nhtu99-hoan9

Is there still any objection to add some-> and some->> into Hyrule?

wrobell avatar Sep 20 '23 22:09 wrobell

No, there isn't.

Kodiologist avatar Sep 21 '23 11:09 Kodiologist

some-> is now implemented, but some->> is not.

Kodiologist avatar Mar 26 '24 16:03 Kodiologist