gorillalabs / sparkling

A Clojure library for Apache Spark: fast, fully-features, and developer friendly
https://gorillalabs.github.io/sparkling/
Eclipse Public License 1.0
448 stars 68 forks source link

add composable destructuring binding form #32

Closed blak3mill3r closed 8 years ago

blak3mill3r commented 8 years ago

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 as key-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 and IterableWrapper, and nested tuples). Tests are included, and I added a section to getting_started.md.

I also took the liberty of adding clojure syntax highlighting to getting_started.md.

chrisbetz commented 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

retnuh commented 8 years ago

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.

blak3mill3r commented 8 years ago

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.

blak3mill3r commented 8 years ago

I changed the syntax to (s-de/fn [(k v)] ...) which has the following advantages:

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.

chrisbetz commented 8 years ago

that's a nice one. Really, really appreciated, because it simplifies things. Thanks a lot :)