Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Unpacking is a limited form of what is called destructuring in other languages like Clojure. I would say that, in terms of feature-richness: unpacking < destructuring < pattern matching.


I'd extend that one step further

    unpacking < destructuring < pattern matching < first-class patterns
where first-class patterns are increasingly becoming available in some languages which offer pattern matching (in particular, Haskell will have them soon).


I did a little googling, but am finding it difficult to find good clear information - do you have any articles where I can read about first-class patterns?


https://ghc.haskell.org/trac/ghc/wiki/ViewPatterns

That's the haskell extension.

To see a very nice use of them, check out this paper (pdf) http://strictlypositive.org/CJ.pdf


How do view patterns make patterns "first-class"? To me that means able to manipulate them as values, I don't see how view patterns allow that, they're just syntactic sugar for case expressions.


http://www.reddit.com/r/haskell/comments/1vpaey/pattern_syno...

I spoke too eagerly—the new feature is just named and namespaced patterns. It's a bit of a bump in power, but it's not fully general yet.

For true(-ish) first-class patterns take a look at Prisms in the lens package or some of the other first-class pattern libraries.


I don't know Clojure, but I do know Python, and I'd like to say that unpacking is more flexible than you may realize. For example, you can do this:

  a,(b,c),d = [1,[2,3],4]


I don't mean to belittle Python here, I think its a great language and I find its unpacking useful. I'm only trying to demonstrate that the concept can be (and is in some other languages, like Clojure) taken further to make it more useful still.

I think that Python's unpacking allows most, if not all, of Clojures sequence destructuring for tuples and lists. Clojure takes it a bit further, however, by applying it to all sequences. For example, you could do this:

    a,b,c = "XYZ"
because strings are also sequences. You can also do something like this (excuse the awkward syntax as I try to express it in pseudo-Python):

    a,b : c as d = [1,2,3,4,5]
    # a = 1
    # b = 2
    # c = [3, 4, 5]
    # d = [1, 2, 3, 4, 5]
This might not be so useful in python, since the list already is d and c is simply slicing the end from the list, but Clojure allows you to destructure function arguments: (defn foo [[a, b & c :as d]] ...) when passed the above list (foo [1 2 3 4 5]) would bind the variables as shown in the above comments.

Where destructuring really shines, though, is that you can destructure maps (dictionaries in Python) and vectors can also be treated as maps (their keys are the indices), so you can do stuff like this:

    a, {[b {:keys [c, d]} :foo}, e = [1, {foo: [2, {c: 3, d: 4}, bar: 9}, 5]
    # a = 1
    # b = 2
    # c = 3
    # d = 4
    # e = 5

http://clojure.org/special_forms#binding-forms


neat!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: