ยท 2 min read in clojure, scala, ruby, python, javascript, programming
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.
(defn chooser [[first-choice second-choice & unimportant-choices]] (println (str "Your first choice is: " first-choice)) (println (str "Your second choice is: " second-choice)) (println (str "We're ignoring the rest of your choices. " "Here they are in case you need to cry over them: " (clojure.string/join ", " unimportant-choices))))
(chooser ["Marmalade", "Handsome Jack", "Pigpen", "Aquaman"]); => Your first choice is: Marmalade; => Your second choice is: Handsome Jack; => We're ignoring the rest of your choices. Here they are in case \; you need to cry over them: Pigpen, AquamanNow, I wanted to see how other languages support this.
Scala
Scala has powerful pattern matching abilities for destructuring a list.
val first::second::rest = List(1,2,3,4,5,6,7,8,9,10)// first: Int = 1// second: Int = 2// rest: List[Int] = List(3, 4, 5, 6, 7, 8, 9, 10)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.
val first :: second:: (inbetween :+ thirdLast :+ secondLast :+ last) = List(1,2,3,4,5,6,7,8,9,10)// first: Int = 1// second: Int = 2// inbetween: List[Int] = List(3, 4, 5, 6, 7, 8)// secondLast: Int = 9// last: Int = 10Ruby
As you know Ruby has this ability as well!
> first, second = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> first=> 1> second=> 2> first, second, *rest = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> first=> 1> second=> 2> rest=> [3, 4, 5, 6, 7, 8, 9, 10]> first, second, *inbetween, secondLast, last = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> last=> 10> secondLast=> 9> inbetween=> [3, 4, 5, 6, 7, 8]> first=> 1> second=> 2Python
Python also does it beautifully.
[first, second, rest] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][first, second, *inbetween, secondLast, last] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# >>> secondLast# 9# >>> last# 10# >>> second# 2# >>> first# 1# >>> inbetween# [3, 4, 5, 6, 7, 8]Javascript
Javascript also support's destructuring.
const [first, second, ...rest] = [1, 2, 3, 4, 5, 6, 7];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/