-
Notifications
You must be signed in to change notification settings - Fork 372
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
apply not good enough to replace Python's unpacking #891
Comments
apply takes up to 3 arguments - callable, args and kwargs. (apply foo (+ [1 2 3] [4 5 6])) ; works |
in particular, CL isn't dealing with what we have to deal with with |
=> (defn foo [&rest args] (print args))
=> (apply foo (+ [1 2 3][4 5 6])) ; works
(1, 2, 3, 4, 5, 6)
=> (apply foo (+ [1 2 3](, 4 5 6))) ; fail
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
=> (apply foo (+ [1 2 3](genexpr x [x [4 5 6]]))) ; fail
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only concatenate list (not "generator") to list >>> def foo(*args):
print(args)
>>> foo(1,2,3,*[4,5,6]) # works
(1, 2, 3, 4, 5, 6)
>>> foo(1,2,3,*(4,5,6)) # works
(1, 2, 3, 4, 5, 6)
>>> foo(1,2,3,*(x for x in [4,5,6])) # works
(1, 2, 3, 4, 5, 6)
Yeah, the kwargs make it more difficult than in other Lisps, but just a little bit. I still think we can do better than what we've got. |
Here's option one: (apply foo 1 2 3 :* (, 4 5 6) :** {"this is key" 42})
;; They're optional
(apply foo 1 2 3 :** {"this is key" 42})
(apply foo 1 2 3 :* (, 4 5 6))
(apply foo 1 2 3)
;; the other order would also work
(apply foo 1 2 3 :** {"this is key" 42} :* (, 4 5 6)) In this version, the args and kwargs are optional keyword-only arguments to Here's option two: (apply foo 1 2 3 (, 4 5 6) {})
(apply foo 1 2 3 (,) {}) In this version, the last two arguments are always the args and kwargs, but they can be empty if not needed. Just like how an Option three. (apply foo [1 2 3] (, 4 5 6) {"k" "v"}) Here, |
Slapped a Grand Language Cleanup milestone on this one. |
Short of adding vectors around args and kwargs, I don't know how to do this And if + isn't good enough when you mix types, try itertools.chain.
|
Something like this? (apply foo 1 2 3 [(, 4 5 6) {"k" "v"}]) You'd still need an empty args if you only wanted to unpack kwargs. (apply foo 1 2 3 [(,) {"k" "v"}]) This doesn't seem any better than option two.
Option three might be a little confusing, but the other two are very predictable.
|
No, I mean (apply [[1 2](, 3 r)])
|
In both Common Lisp and Python, I know that I can apply some parameters and then unpack the rest from a list.
Python.
Here's a similar example in Common Lisp.
Let's try that in Hy
Hmnope. Hy's apply won't accept arguments before the list. Can we get a fix for this please?
The text was updated successfully, but these errors were encountered: