Comments (12)
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 &keys
syntax.
from janet-lang.org.
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:
Destructuring works in many places in Janet, including let expressions, function parameters, and var.
from janet-lang.org.
I didn't recognize that there was struct destructuring after &keys
....
I thought it was a special syntax...
from janet-lang.org.
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 [&keys foo]
was valid syntax.
This is a useful thing to know, because sometimes I need to pass the whole struct received after &keys
to another function. But if I've destructured it in the main function's params, then there's no symbol bound to the whole struct and I have to manually reconstruct it in the arguments of the sub function:
(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 &keys
at all almost ever:
(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 &keys
might get more use in my Janet code moving forward.
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 defn
/approach to destructuring in general currently supports this.
from janet-lang.org.
Yeah an :as
sort of thing could be nice.
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 #
and ,
stuff -- it's from within a macro definition (^^;
from janet-lang.org.
I see :as
as Clojurism :-), as it brings another layer of magic into the restructuring. All the puns intended :-D.
from janet-lang.org.
Not sure there is really a nice solution, but IIUC ATM there is a trade-off one must make between:
- documenting keys using destructuring in the function signature
- use of passed struct within the function body
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.
from janet-lang.org.
Haskell provides an enhanced destructuring syntax.
For example, in haskell, something like this is possible
(defn
[arg1 &keys attrs@{:abc abc :bcd bcd}]
(pp attrs)
(pp abc)
...)
from janet-lang.org.
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:
Note that what follows `&keys` does not have to be a
struct and can instead be a single symbol. The
symbol can then be used as a name within the function
body for a struct containing the passed keys and values:
(defn feline-counter
[&keys animals]
(if-let [n-furries (get animals :cat)]
(printf "Awww, there are %d cats!" n-furries)
(print "Shucks, where are my friends?")))
(feline-counter :ant 1 :bee 2 :cat 3)
from janet-lang.org.
That's what I meant.
from janet-lang.org.
Thanks for clarifying.
I submitted PR #214.
from janet-lang.org.
Looks like this issue may have been address by the merging of #214. Perhaps it can be closed.
from janet-lang.org.
Related Issues (20)
- Auto-focusing search means you can't link to specific API functions HOT 1
- Example in "Installing a module" points at archived repository
- PEG specials `true` and `false` lack docs on PEG page HOT 2
- Related Windows instructions out-of-sync
- Bytecode reference table lacks entry for divf HOT 1
- Several (probably all) community examples links of spork functions are broken. HOT 9
- Bytecode reference table lacks entry for subim
- Non-ideal discoverability of Janet divergences from other programming languages HOT 9
- Extra dots in some operator symbols in ref table for abstract machine instructions?
- Document spork/utf8. HOT 4
- Document `init.janet` for `import`. HOT 2
- Document how to import an absolute path with `import`. HOT 8
- Document `declare-bin`. HOT 5
- Subprocess management appears under-documented HOT 2
- Identifiers with slashes / solidi can end up across multiple lines HOT 1
- The `split` PEG special lacks documentation
- Regarding keeping editor and tooling info on the front-page up-to-date HOT 15
- Docs don't mention that function literals can be named with keywords
- lenprefix docs mention a tag, but feature unsupported
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from janet-lang.org.