-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring back apply
for argmove
?
#52
Comments
Here's a candidate (defmacro apply [expr args &optional [kwargs {}]]
`(~@(if (isinstance expr HyExpression)
expr
[expr])
#* ~args
#** ~kwargs)) Python's distinction between args and kwargs made the old apply awkward compared to the usual Lisp version. I like this candidate better than our old one. hylang/hy#891. It still works like before: => (apply print [4 5 6] {'sep "::"})
from hy import HySymbol
print(*[4, 5, 6], None={HySymbol('sep'): '::',})
4::5::6 But now the final dict is optional, and you can partially apply things before unpacking. => (setv args [4 5 6])
args = [4, 5, 6]
None
=> (apply (print 1 2 :sep "::") args)
print(1, 2, *args, sep='::', None={})
1::2::4::5::6 My original |
On second thought, you can use a => (-> (range 10) iter , (* 2) (->> (apply zip)) list) list(zip(*((iter(range(10)),) * 2), None={}))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] You can also use => (-> (range 10) iter , (* 2) (as-> it (apply zip it)) list) it = ((iter(range(10)),) * 2)
it = zip(*it, None={})
list(it)
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] But this isn't really required for the candidate It's certainly usable like this, but I'm not sure if a different ordering would be better. |
I just realized that the => (-> (range 10) iter , (* 2) (as-> it (zip #* it)) list) it = ((iter(range(10)),) * 2)
it = zip(*it)
list(it)
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] The compilation is not as nice as with We might also want a shadow (defn apply [f args &optional [kwargs {}]]
(f #* args #** kwargs)) This lacks the partial syntax of the macro, which is not possible for a function, but if you're using HOF anyway, you could just use (defn apply [f &rest args &kwargs kwargs]
(fn [args2 &optional [kwargs2 {}]]
(f #* (chain args args2)
#** (doto (.copy kwargs)
(.update kwargs2))))) |
Possibly, using more compiler peeking into child forms. The thing is that |
Exactly what I was thinking. |
My guess from Matthew's original example here and from this Stack Overflow question is that the remaining need for |
My first attempt at implementing
partition
looked like this:This doesn't work anymore because we removed
apply
. I wasn't sure if it was redundant or not at the time. But let's try it with the new syntax.Not good.
apply
still has uses, apparently. I'm not sure how well I liked the old version, but it feels like something should replace it, since#*
/#**
can't always do it.You can sort of work around this with
xi
But this adds a useless lambda in the compilation.
apply
didn't require an extra call like this. Maybe some kind of macro could work.The text was updated successfully, but these errors were encountered: