-
Notifications
You must be signed in to change notification settings - Fork 11
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
Change destructuring syntax to align with "match" #29
Comments
What happens if you have dicts/maps
In Clojure, you can destructure the former as
With your proposal, you can destructure the former as
Or, since I don't know how to program telepathy 🪄 🧠,
Did I understand your proposal correctly? |
as far as my idea for this proposal is concerned, you'd quote it. Same as we do with (assert (= :as (match 1 1 :if True ':as))) so here it would be (let+ [{':keys [a b c]} {:keys [1 2 3]}]
[a b c]) I have not tried to implement this yet, but that's the idea |
About implementing it, it shouldn't be too hard (apparently reordering the arguments of dicts is done in This is an amazing idea because (let+ [{"key" foo}
{"key" "quux"}]
(print foo)) ; => "quux" better reflects the structure we are trying to match than: (let+ [{foo "key"}
{"key" "quux"}]
(print foo)) ; => "quux" Allowing the (discouraged) use of keywords as keys may be useful in some edge cases. But it probably should be discouraged, so (let+ [{"with" foo}
{"this" "is" "a" "big" "dictionary" "with" "many" "many" "similar" "things"}]
(print foo)) ; => None
(let+ [{:with foo}
{:this "is" :a "different" :big "dictionary" :with "many" :similar "things"}]
(print foo)) ; => many |
It's encouraged in Hy to write dict literals with two spaces between key value pairs (clojure uses a comma + space for large maps, but commas are not whitespace in Hy) which makes cases like that easier to deal with. (let+ [{"with" foo} {"this" "is" "a" "big" "dictionary" "with" "many" "many" "similar" "things"}]
(print foo)) ; => None Or use the (let+ [{"with" foo} (dict :this "is" :a "big" :dictionary "with" :many "many" :similar "things")]
(print foo)) ; => None |
Double spaces and While we are making the destructuring macros more Hy-like, I would suggest replacing (let+ [[a b #* rest :as full]
[0 1 2 3 4]]
[a b rest full]) ; => [0 1 [2 3 4] [0 1 2 3 4]] vs (let+ [[a b :& rest :as full]
[0 1 2 3 4]]
[a b rest full]) ; => [0 1 [2 3 4] [0 1 2 3 4]] Maybe there's potential for I've been looking around |
Very much agree with this. I'm sure it's possible, but I have some other PR's in the main repo to deal with right now. If you want to take a stab at this, make sure to look over the |
I've been reading the PEP that @allison-casey linked, What's New in Python 3.10, and the Apparently If I understood python (def skynet-widgets
[{:basic-info {:producer-code "Cyberdyne"}
:widgets [{:widget-code "Model-101"
:widget-type-code "t800"}
{:widget-code "Model-102"
:widget-type-code "t800"}
{:widget-code "Model-201"
:widget-type-code "t1000"}]
:widget-types [{:widget-type-code "t800"
:description "Resistance Infiltrator"}
{:widget-type-code "t1000"
:description "Mimetic polyalloy"}]}
{:basic-info {:producer-code "ACME"}
:widgets [{:widget-code "Dynamite"
:widget-type-code "c40"}]
:widget-types [{:widget-type-code "c40"
:description "Boom!"}]}])
(require '[meander.match.alpha :as m])
(m/search skynet-widgets
(scan {:basic-info {:producer-code ?producer-code}
:widgets (scan {:widget-code ?widget-code
:widget-type-code ?widget-type-code})
:widget-types (scan {:widget-type-code ?widget-type-code
:description ?description})})
[?producer-code ?widget-code ?description])
;;; Which results in:
(["Cyberdyne" "Model-101" "Resistance Infiltrator"]
["Cyberdyne" "Model-102" "Resistance Infiltrator"]
["Cyberdyne" "Model-201" "Mimetic polyalloy"]
["ACME" "Dynamite" "Boom!"]) There are some examples using While I don't like Meander's syntax, it serves to show that a few primitives for destructuring data can go a long way when dealing with complex and cumbersome data. So I think that adding |
(match some-value
[1 _ a] :as arr [a arr]
1 ':as
anything-else :not-found) Doesn't bind the I've never seen that kind |
Python 3.10 introduced a canonical destructuring syntax with match. I think we should change the existing clojure like syntax to align with the syntax introduced by Python match which is already used by our existing
match
implementation.Here's what I had in mind.
This has key and binding symbol swapped compared to clojure, but Hy isn't Clojure 🤷♀️ and I like the consistent syntax with our existing
match
implementation. the only addition here is the:keys
option. Which, looks up by string keys and maintains the idiom of keywords being arguments not identifiers themselves (even though there's nothign stopping you from destructuring keywords here or inmatch
)The text was updated successfully, but these errors were encountered: