Janet Notes and Examples

Functions

You can define functions inside of other functions, and can also create def’s and var’s in general inside of functions too, if you like.

If you pass a mutable object to a function, you can mutate it inside the function and changes are seen by the caller.

# variadic functions (taking a variable number of args)
(defn foo
  [a b & c]  # Extra args passed in are put into the `c` tuple.
  ...)

# optional args
(defn foo
  [a b &opt c d]  # `c` and `d` are optional (`nil` if not supplied)
  (default c 42)  # Give `c` a default value.
  ...)

# Ignore the rest of the args.
(defn foo
  [a b &]  # Must pass at least two args.
  ...)

# keyword-style args
(defn foo
  [&keys {:size size
          :color color
          :style style}]
  (pp size)
  (pp color)
  (pp style)
  ...)
# and call it like
(foo :size 8 :color "red" :style "retro")

# Note, you *could* skip using `&keys` and only use destructuring by itself:
(defn foo
  [{:size size
    :color color
    :style style}]
  (pp size)
  (pp color)
  (pp style)
  ...)
# But then calling would look like:
(foo {:size 8 :color "red" :style "retro"})

Also supported:

  • destructuring
  • defn- for not-to-be-exported functions