-
Notifications
You must be signed in to change notification settings - Fork 58
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
Document &keys
better.
#205
Comments
Thanks for elaborating. Perhaps the text could expand on things a bit more. May be a PR would be accepted toward that end. Note though that currently the following text exists at the bottom of the destructuring page:
|
I didn't recognize that there was struct destructuring after |
I thought the same thing! I saw your original issue over in janet-lang/janet (agree that this is a better place for it). Before that I didn't realize that This is a useful thing to know, because sometimes I need to pass the whole struct received after (defn foo [&keys {:a apples :b bananas}]
(print apples)
(print bananas)
(count-calories {:a apples :b bananas})) This small example is already annoying, but it's much worse the more keys are in the struct. That kinda turned me off from using (defn foo [fruitmap]
(print (fruitmap :a))
(print (fruitmap :b))
(count-calories fruitmap)) But it turns out, this is valid: (defn foo [&keys fruitmap]
(print (fruitmap :a))
(print (fruitmap :b))
(count-calories fruitmap)) So What would be really nice is if I could have the best of both worlds: (defn foo [&keys {:a apples :b bananas} :as fruitmap]
(print apples)
(print bananas)
(count-calories fruitmap)) But I don't think Janet's |
Yeah an Not sure of the specifics though. For reference, the fennel folks seem to do this sort of thing (inspired by Clojure perhaps?): {:expr expr#
:res result#
:debugger-id ,(tostring debugger-id)
:id id#
&as data#} Ignore the |
I see |
I'm sure if there was anything really bothering me I could find a way to fix it with macros, at least to my own satisfaction. 🙂 No need to change the language, unless more people than just me would find something of that kind useful. More on the topic of this issue, I do think it would be helpful to clarify the docs around |
Not sure there is really a nice solution, but IIUC ATM there is a trade-off one must make between:
I think you can't have your cake and eat it too...though may be this cake is not tasty enough (^^; Back to the topic (as CFiggers mentioned), perhaps an additional example in the docs (plus some text) that demonstrates the point would be worthwhile. |
Haskell provides an enhanced destructuring syntax. For example, in haskell, something like this is possible
|
Regarding possible changes to the documentation, perhaps an additional example can be added to the end of the keyword-style arguments section along with some explanatory text. Here's a first draft:
|
That's what I meant. |
Thanks for clarifying. I submitted PR #214. |
Explain use of symbol after &keys (#205)
Looks like this issue may have been address by the merging of #214. Perhaps it can be closed. |
Until recently, I didn't recognize the example for
&keys
on https://janet-lang.org/docs/functions.html was using destructuring. I thought the destructured struct was the only allowed syntax for&keys
.I didn't think it was a destructured struct. I thought it was just the only syntax for
&keys
.For years, I didn't know something like this was possible.
https://janet-lang.org/docs/functions.html should mention that what follows after
&keys
is actually a destructured struct.The text was updated successfully, but these errors were encountered: