JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

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

Loading...

Random Examples

(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
(print math/int-min)
-9007199254740992
nil
math/int-minbtbytesPlayground
(map length  [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  ])
# =>        @[  1   1   1    1     1      1       1        ]
lengthcellularmitosisPlayground
(interpose 0 [1 2 3])
# -> @[1 0 2 0 3]
interposeKrasjetPlayground
# Print out the current time in Last-Modified header-compliant 
# form. (reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified)
(def weekdayMap [ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" ])
(def monthMap ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"])
(def dt (os/date (os/time) true))
(string/format "%s, %02d %s %04d %02d:%02d:%02d" 
     (weekdayMap (dt :week-day)) 
     (dt :month-day)
     (monthMap (dt :month))
     (dt :year)
     (dt :hours)
     (dt :minutes)
     (dt :seconds))
string/formatyumaikasPlayground
(defn slurp-lines [path]
  (string/split "\n" (slurp path)))
string/splitcellularmitosisPlayground
(->> "X"
     (string "a" "b")
     (string "c" "d")
     (string "e" "f"))  # => "efcdabX"
->>uvtcPlayground
(def cc (compile '(print "Janet"))) # => <function _thunk> on success returns function
(cc) # => prints Janet
compilepepePlayground
(empty? [])
# => true
empty?sogaiuPlayground
(partition-by even? [1 1 2 3 5 8 13 21])
# => @[@[1 1] @[2] @[3 5] @[8] @[13 21]]
partition-bysogaiuPlayground
(-> "X"
    (string "a" "b")
    (string "c" "d")
    (string "e" "f"))  # => "Xabcdef"
->uvtcPlayground
(repeat 12 (-> 12 os/cryptorand pp))

# => @"\xA7li[ \xED\xD2\xF7O\xD6\x15="
# => @">\"-w+\x04\x1C\xC1KG\x9C\xE4"
# => @"\x06b\f\xBD\x12\x19\xB6\x1A\xCA\xB9[\x85"
# => @"\xBE`R\t\x13\x81\xED\x9D#\xD0\x11!"
# => @"\xE2\xC1\xD8\x7F\\\xA7\x84\xC0\v\x8B'\x98"
# => @"\xD6\x0Fz\x86\xE2\xB2\x1D}\xC6'{\xB5"
# => @"\x9D\x97\xA1\x07i\x9FW\x83h4n2"
# => @"d\x8E\xB8\xBA \xA6\x9C\f\xC6\xAD{g"
# => @"\r\xB5\xF84#\xB8c~V\xD7d>"
# => @"\xBB\x19\xB2\xDC\x8B\xD9\x7F\xDC\xBE\f\x88\xE3"
# => @"w\xB50\xF9\xFD\xEB\x1D\xFF:j]\xB8"
# => @"\x8F$\xEBKL~\xFD\t\xA8\xD1\x8C\xC5"
# => nil
repeatjgartePlayground
(math/pow 10 3)   # => 1000
(math/cbrt 1000)  # => 10
math/cbrtcellularmitosisPlayground
(spit "/tmp/hello.sh" "#!/bin/bash\necho 'Hello from Bash!'\n")
(os/chmod "/tmp/hello.sh" "rwx------")
(os/setenv "PATH" (string (os/getenv "PATH") ":/tmp"))
(os/shell "hello.sh")
os/chmodcellularmitosisPlayground
# janet 1.10.1

(=       [1 1]   [1 1])  # => true
(=       [1 1]   [2 3])  # => false
(=       [1 1]  @[1 1])  # => false
(=       [1 1]  @[2 3])  # => false
(=      @[1 1]  @[1 1])  # => false
(=      @[1 1]  @[2 3])  # => false

(deep=   [1 1]   [1 1])  # => true
(deep=   [1 1]   [2 3])  # => false
(deep=   [1 1]  @[1 1])  # => false
(deep=   [1 1]  @[2 3])  # => false
(deep=  @[1 1]  @[1 1])  # => true
(deep=  @[1 1]  @[2 3])  # => false
deep=cellularmitosisPlayground
 new Math.seedrandom('hello.');
math/seedrandomMonif2009Playground
(string/join @["1" "2" "3"]) # => "123"
# The esp argument is optional, and defaults to empty string
string/joinyumaikasPlayground
(band 7 11)  # => 3

#     0111  (7)
# and 1011  (11)
# --------
#     0011  (3)
bandcellularmitosisPlayground
(max 1 2 3)             # => 3
(max (splice [1 2 3]))  # => 3
(max ;[1 2 3])          # => 3
(apply max [1 2 3])     # => 3
maxcellularmitosisPlayground
(trace +)
# <function +>

((trace +) 5 3)
# trace (+ 5 3)
# 8

(defn knorkulate [a b]
  ((trace +) a ((trace *) b b )))
# <function knorkulate>

(knorkulate 2 34)
# trace (* 34 34)
# trace (+ 2 1156)
# 1158
tracekamisoriPlayground