10 ms·
I have two questions. 1. I'm unfamiliar with the term 'unpacking'. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)
by robinh 13y ago
I have two questions.
1. I'm unfamiliar with the term 'unpacking'. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)?
2. Aren't slices pretty much a staple in Python? I didn't think using them was considered a 'trick'.
- pekk 13y agoTuple unpacking: t = (1, 2) a, b = t # a == 1 and b == 2 Using slices is normal with lists
- dkersten 13y agoUnpacking 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.
- tel 13y agoI'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).
- dkersten 13y agoI 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?
- Tyr42 13y agohttps://ghc.haskell.org/trac/ghc/wiki/ViewPatterns 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 http://strictlypositive.org/CJ.pdf
- anaphor 13y agoHow 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.
- tel 13y agohttp://www.reddit.com/r/haskell/comments/1vpaey/pattern_synonyms_merged_into_ghchead/ 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.
- marvy 13y agoI 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]
- dkersten 13y agoI 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 http://clojure.org/special_forms#binding-forms
- marvy 12y agoneat!