JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!

Loading...

Random Examples

(range 12) # => @[0 1 2 3 4 5 6 7 8 9 10 11]

(range 0 12) # => @[0 1 2 3 4 5 6 7 8 9 10 11]

(range 0 12 1) # => @[0 1 2 3 4 5 6 7 8 9 10 11]

(range 0 12 2) # => @[0 2 4 6 8 10]

(range 0 12 3) # => @[0 3 6 9]

(range 0 12 4) # => @[0 4 8]

(range 0 12 6) # => @[0 6]
rangejgartePlayground
# read input at compile-time and create a custom func to check whether a number is in one of the
# given ranges. This turned out to be almost 4x faster than writing it as a func
# from https://abhinavsarkar.net/notes/2025-aoc-1/#day-2

(defmacro in-range? [n & _]
  (def ranges (parse-input (slurp input-path)))
  ~(or ,;(map (fn [[s e]] ~(<= ,s ,n ,e)) ranges)))
defmacroveqqqPlayground
 new Math.seedrandom('hello.');
math/seedrandomMonif2009Playground
(take-while number? 
            (fiber/new
              |(each x [1 2 3 :hi]
                 (yield x))))
# => @[1 2 3]
take-whilesogaiuPlayground
(string/trimr " foo ")  # => " foo"
(string/trimr "_!_foo_!_" "_!")  # => "_!_foo"
string/trimrcellularmitosisPlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(pairs {:a 1 :b 2 :c 3})
# =>
@[(:c 3) (:a 1) (:b 2)]
pairsuvtcPlayground
(os/dir "./")                   # => @[]
(os/shell "touch foo bar baz")  # => nil
(os/dir "./")                   # => @["foo" "bar" "baz"]
os/dircellularmitosisPlayground
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
# demo of (@ <sym>) feature
(let [content "# => label"]
  (match [:comment @{} "# => label"]
    [:comment _ (@ content)]
    :found
    nil))
# => :found
matchsogaiuPlayground
(reduce     + 1 [2 3 4])  # -> 10
(accumulate + 1 [2 3 4])  # -> @[3 6 10]
accumulatecellularmitosisPlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(peg/find-all ':d "hi 1 bye 2") # => @[3 9]
peg/find-allpepePlayground
# When the :doc-color dynamic binding referenced by *doc-color* is truthy,
# the doc-format function replaces a minimal subset of Markdown markup with
# the corresponding ANSI escape codes.
#
# The following markup is supported:
# - *this will be underlined*
# - **this will be bold**
# - `(+ 1 2 3)` <- backticks for code
#
# You may be surprised by *underline* since the same markup is used to
# indicate italics in Markdown. This is likely a tradeoff for compatibility;
# historically, the italic attribute has not been widely supported by
# terminal emulators.
#
# The best way to see the effect of *doc-color* is try the following examples
# in the Janet REPL.

# By default, *doc-color* is enabled.
(print (doc-format "*underline*. **bold**. `(code)`."))

# Set the dynamic binding to a falsy value to disable doc-format's ANSI
# escape code substition.
(with-dyns [*doc-color* false]
  (print (doc-format "*underline*. **bold**. `(code)`.")))

# N.B.: At the time of writing, no docstrings in the core API take advantage of
# the bold or underline markup As a result, you may not see any difference in
# the doc formatting if your terminal theme uses the same hue for white and
# bright white (a few terminals that I tested on Linux make no distinction
# between the two colors in their default configuration).
*doc-color*quexxonPlayground
# will recursively flatten all indexed-like values
(flatten [1 @[2 [3]] [@[4] 5]])
# => @[1 2 3 4 5]

# only applies to indexed values, other values are untouched
(flatten ["a" :b [:c :d] :e "f"])

# be careful: dictionaries are considered indexed since they define next
(flatten {:a :b})

# however, this only applies to the top value, otherwise it remains untouched
(flatten [{:a :b} {:c :d}])

# if you want only "one cycle of flatten", you want reduce
(reduce array/concat @[] (pairs {:a [:b :c] :d [:e [:f]]}))
# => @[:d (:e (:f)) :a (:b :c)]
flattenCosmicToastPlayground