Janet 1.5.0-719f7ba Documentation
(Other Versions: 1.5.0 1.4.0 1.3.1)

Destructuring

Janet uses the get function to retrieve values from inside data structures. In many cases, however, you do not need the get function at all. Janet supports destructuring, which means both the def and var special forms can extract values from inside structures themselves.

# Without destructuring, we might do
(def my-array @[:mary :had :a :little :lamb])
(def lamb (get my-array 4))
(print lamb) # Prints :lamb

# Now, with destructuring,
(def [_ _ _ _ lamb] my-array)
(print lamb) # Again, prints :lamb

# Destructuring works with tables as well
(def person @{:name "Bob Dylan" :age 77})
(def
  {:name person-name
   :age person-age} person)

Destructuring will work in many places in Janet, including in let expressions, function parameters, and var. It is a useful shorthand for extracting values from data structures, and is the recommended way to get values out of small structures.