Closed blak3mill3r closed 8 years ago
Hi,
I like this contribution also, but I do have one question: Did you choose parameters to be a list instead of a vector for a reason? Because I would have expected (s-de/fn [k v] ...) instead of (s-de/fn (k v) ...).
But again: Thanks for the contribution, I most definitively will include it (after you enlightened me ;) )
Thanks,
Chris
I kind of like the list syntax; it makes it clear to me that I'm destructuring a tuple (or some non clojure data structure).
Just my 2¢
H
On 17 December 2015 at 08:23, chris_betz notifications@github.com wrote:
Hi,
I like this contribution also, but I do have one question: Did you choose parameters to be a list instead of a vector for a reason? Because I would have expected (s-de/fn [k v] ...) instead of (s-de/fn (k v) ...).
But again: Thanks for the contribution, I most definitively will include it (after you enlightened me ;) )
Thanks,
Chris
— Reply to this email directly or view it on GitHub https://github.com/gorillalabs/sparkling/pull/32#issuecomment-165381616.
I did do it that way for a reason: I wanted to make it extra clear that its behavior is very different from standard destructuring forms. I would be glad to change it to a vector if you think that's better, I have no strong opinion when it comes to (s-de/fn [k v] ...)
vs (s-de/fn (k v) ...)
... however after considering it further just now, I feel this third option is worth considering: (s-de/fn [(k v)] ...)
It may be more expressive that it's defining a unary fn expecting a tuple. I'm thinking that if we wanted to leave open the possibility of supporting fns accepting multiple tuple arguments, then this third option would be a good choice to make right now, because the other two wouldn't be able to express that, and any change to allow it would not be backwards-compatible. I've never had a need for a fn taking multiple tuples. I'm curious to hear what others think.
I changed the syntax to (s-de/fn [(k v)] ...)
which has the following advantages:
-
and ?
to occur outside a tuple (could be used in a map-values after a group-by-key, for example)and I also added support for map & vector destructuring, by delegating to clojure.core/destructuring
, those can be mixed/nested with the tuple forms, so you can get at the keys inside a Clojure record inside a tuple without needing a let
.
I added tests for all new features, and updated the explanation in getting_started.md
.
that's a nice one. Really, really appreciated, because it simplifies things. Thanks a lot :)
This is an extraction from a sparkling project I've been working on, and I've found it generally useful.
It's a simple & flexible replacement for the fn wrappers in the
destructuring
namespace such askey-value-fn
The basic idea is that these two are equivalent:
(s-de/key-value-fn (fn [k v] [k v]))
(s-de/fn (k v) [k v])
There's more to it than that (it can unwrap
Optional
andIterableWrapper
, and nested tuples). Tests are included, and I added a section togetting_started.md
.I also took the liberty of adding clojure syntax highlighting to
getting_started.md
.