-
Notifications
You must be signed in to change notification settings - Fork 370
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
Remove car
/cdr
in favor of first
/rest
#909
Comments
Can you open a PR for this? 👍 from me. |
I'm getting a strange "internal compiler bug" error I haven't been able to pin down when I try to replace
(long stack trace omitted for brevity) I'm worried that attempting to remove This is the version that causes the error: (defmacro defmacro! [name args &rest body]
"like defmacro/g! plus args that start with o! will automatically evaluate
once only and be bound to the equivalent g!"
(setv os (genexpr s [s args] (.startswith s "o!"))
gs (genexpr (HySymbol (+ "g!" (cut s 2))) [s os]))
`(defmacro/g! ~name ~args
`(do (setv ~@(interleave ~gs ~os))
~@~body))) I'm not absolutely certain that this is an error in Hy, and not just in (defmacro defmacro! [name args &rest body]
"like defmacro/g! plus args that start with o! will automatically evaluate
once only and be bound to the equivalent g!"
(setv os (list-comp s [s args] (.startswith s "o!"))
gs (list-comp (HySymbol (+ "g!" (cut s 2))) [s os]))
`(defmacro/g! ~name ~args
`(do (setv ~@(interleave ~gs ~os))
~@~body))) |
Well, the error could be a bit nicer (!), but the reason is simple. Hy automatically converted the list returned from |
A lot of Python's sequence manipulation tools return generators, not lists. You need sequence manipulation (LISt Processing) in macros most of all, but you're telling me that they're completely incompatible? It would be a pain to have to use Can we teach Hy to "do that" with generators? Clojure seems to be able to use lazy sequences without this problem. Maybe a |
I prefer being explicit here. It's happened to me before where I passed the wrong type that happened to be an iterable to an API that implicitly converted it to a list. |
I'm a lot more confused with the way Hy isn't working now than I would be if something implicitly became a list, and like I said, I haven't run into this kind of problem with Clojure's lazy sequences. At the very least this needs to be better documented. I still don't understand what's triggering this error. Can you demonstrate with a minimal example? |
Essentially, this is not being used; https://github.com/hylang/hy/blob/master/hy/models/__init__.py#L39 |
One of the subissues from #240. I think we'll make more progress discussing one subissue at a time.
rest
/cdr
are not quite the same, but still redundant.(cdr coll)
is a macro that expands into a slice likecoll[1:]
.rest
is a partial application of the functionislice
that does about the same thing ascdr
. As a function,rest
can be assigned to variables, passed to other functions, etc. As a macro,cdr
cannot. Slices also don't work on generators socdr
doesn't work, but islices do, and so doesrest
.rest
will do in almost all cases we currently usecdr
.The main problem with replacing all slices with islices, is that islice will always be a generator, but slices return another instance of the same collection type. Most of the time this isn't a problem. Python's libraries are very good about generally accepting iterables where you might have used a collection before. The main exception is strings, which really need to stay as strings.
If you do run into a case like this, you can still use
(cut coll 1)
for the same effect.The case for dropping
car
in favor offirst
is even stronger.car
is a macro likecoll[0]
, whilefirst
is a function likenext(islice(coll, 0, None))
, which should perhaps be simplified tonext(iter(coll))
. Either way, you get the first element. Exceptcar
doesn't work on generators, and can't be passed as a higher-order function, etc. There is absolutely no reason to keepcar
except for symmetry withcdr
, which we should also drop.The text was updated successfully, but these errors were encountered: