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

(peg/find ':d "Battery temperature: 40 °C")
# => 21 index of the first number
peg/findpepePlayground
(string/ascii-upper "hello")  # => "HELLO"
string/ascii-uppercellularmitosisPlayground
(def a @[])
(array/insert a  1 :a)  # error: index out of range
(array/insert a -2 :a)  # error: index out of range
array/insertcellularmitosisPlayground
# janet 1.10.1

# note that os/cd does not appear to update PWD in the shell's environment.
(os/cwd)           # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(os/cd "/tmp")     # => nil
(os/cwd)           # => "/private/tmp"  (on Apple, /tmp is actually /private/tmp)
(os/getenv "PWD")  # => "/Users/cell/tmp"
os/cdcellularmitosisPlayground
(defn unix->date-label
  ``Format a unix timestamp as date or datetime string``
  [ts]
  (let [d (os/date ts)]
    (if (or (pos? (d :hours)) (pos? (d :minutes)))
      (string/format "%d-%02d-%02d %02d:%02d"
                     (d :year) (d :month) (d :month-day)
                     (d :hours) (d :minutes))
      (string/format "%d-%02d-%02d"
                     (d :year) (d :month) (d :month-day)))))
os/dateveqqqPlayground
(one? (math/next 1 math/inf))  # => false
math/nextcellularmitosisPlayground
(array/pop @[]) # => nil
array/popjgartePlayground
(defn walker
  `Simple walker function, that prints non-sequential 
   members of the form or prints "Sequence" and walks 
   recursively sequential members of the tree.` 
  [form] 
  (if (or (indexed? form) (dictionary? form))
    (do (print "Sequence")
        (walk walker form))
    (print form)))

(walk walker [[[[0 1 3]] 16 7 [3 [3 5]] 3 4] 1 [3 4]])

# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
walkpepePlayground
(freeze @{:a @[1 2] 
          :b @{:x @[8 9] 
               :y :smile}})
# => {:a (1 2) :b {:x (8 9) :y :smile}}
freezesogaiuPlayground
(forever
  (print (os/time))
  (ev/sleep 1))

# => epoch clocks, prints epoch timestamp every second
foreverpepePlayground
(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
(find |(= 2 $) {:a 1 :b 2 :c 3})
# => 2

(get @"Hello" 1)
# => 101

(find |(= 101 $)  @"Hello")
# => 101

(find |(> $ 3)  {:a 1 :b 2 :c 3})
# => nil
findleobmPlayground
(os/shell "uptime > /tmp/uptime.txt")  # => 0
(slurp "/tmp/uptime.txt")
# => @"22:33  up 5 days,  9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt")  # => nil
os/readlinkcellularmitosisPlayground
(string/format "%f" (os/clock)) # => "1627746483.991000"
os/clockdbreadyPlayground
(any? [false false nil]) => nil
(any? [false false nil 1]) => 1
(any? [false false nil true]) => true
any?swlkrPlayground