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

(drop-while |(> 3 $) [0 1 2 3 4 5 6 7 8 ])
# => (3 4 5 6 7 8)
drop-whileleobmPlayground
(cmp 1.0 2)
# => -1
cmpsogaiuPlayground
(array/pop @[]) # => nil
array/popjgartePlayground
# If your tooling or the compiler doesn't believe a symbol will be bound e.g.:
(def a b) # if b e.g. gets bound by some macro or pushed directly to the env

# You can wrap it with compwhen on a bool:
(var- bla false)
(compwhen bla
          (def a b))

# Whatever defines b should then set bla to true
# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet
compwhenveqqqPlayground
(+ 1 2 3)      # => 6
(sum [1 2 3])  # => 6
sumcellularmitosisPlayground
(get-in  [[4 5] [6 7]]  [0]    42)  # => (4 5)
(get-in  [[4 5] [6 7]]  [0 1]  42)  # => 5

(get-in  [[4 5] [6 7]]  [-1]     42)  # => 42
(get-in  [[4 5] [6 7]]  [9 9 9]  42)  # => 42
get-incellularmitosisPlayground
(some even? [1 3 5 7 11 18])
# =>
true
somesogaiuPlayground
(seq [v :in (coro
                (yield :hi)
                (yield :bye))]
    v)
# => @[:hi :bye]
coropepePlayground
(var enabled false)
(toggle enabled)
(print enabled)
# => true
toggleFuriouZzPlayground
(string "hello" nil true 42 :bar)  # => "helloniltrue42bar"
(string :a :b :c)  # => "abc"
stringcellularmitosisPlayground
(let [c (ev/chan 1)
      before (ev/count c)]
  (ev/give c :hi)
  [before (ev/count c)])
# =>
'(0 1)
ev/countsogaiuPlayground
(def arr (array :a [:b :c] :d))
# => @[:a (:b :c) :d]
(0 arr)
# => :a
(1 arr)
# => (:b :c)

# out-of-bounds access causes an error
(try
  (3 arr)
  ([err] err))
# => "expected integer key for array in range [0, 3), got 3"

# you may use `get` to avoid the error
(get arr 3)
# => nil
arrayAndriamanitraPlayground
(def b @"")
(def v (* 1000 (math/random)))
# => 487.181 your number can differ
(xprinf b "Value reached level %f" v)
# => nil
b
# => @"Value reached level 487.181188"
xprinfpepePlayground
(band 7 11)  # => 3

#     0111  (7)
# and 1011  (11)
# --------
#     0011  (3)
bandcellularmitosisPlayground
(flatten-into @[1 2 3] @[[4 5 6] @[7 8 9] @[10 11 12]]) # => @[1 2 3 4 5 6 7 8 9 10 11 12]
flatten-intojgartePlayground