Janet 1.32.1-cc5beda Documentation
(Other Versions: 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 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

# You can also collect trailing destructured values into a "more"
# tuple, though note that it copies, and so may be costly.
(def [p q & more] my-array)
(print p) # :mary
(print q) # :had
(pp more) # (:a :little :lamb)

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

Destructuring works in many places in Janet, including 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.