List Destructuring
I was reading the Clojure for Brave and True book, when I stumbled upon destructring feature in Clojure.
Clojure does it neatly! https://clojure.org/guides/destructuring
This example is taken directly from the book.
|
|
Now, I wanted to see how other languages support this.
Scala
Scala has powerful pattern matching abilities for destructuring a list.
|
|
Scala's pattern matching is so powerful that you can go further with it. For example you can find the second last element, or third last element and what ever is inbetween them.
|
|
Ruby
As you know Ruby has this ability as well!
|
|
|
|
|
|
Python
Python also does it beautifully.
|
|
Javascript
Javascript also support's destructuring.
|
|
Java
Are you kidding me? 😄
References
- https://clojure.org/guides/destructuring
- https://www.braveclojure.com/do-things/#Destructuring
- https://gist.github.com/john2x/e1dca953548bfdfb9844
- https://blog.brunobonacci.com/2014/11/16/clojure-complete-guide-to-destructuring/
- https://alvinalexander.com/scala/how-to-use-lists-nil-cons-scala-match-case-expressions
- https://tony.pitluga.com/2011/08/08/destructuring-with-ruby.html
- https://blog.fogus.me/2011/01/12/pattern-matching-vs-destructuring-to-the-death/