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

(keyword/slice "some crazy keyword" 11 -1)
# returns :keyword 
keyword/slicepepePlayground
# Contrived examples returning the variadic arguments passed in.
(defn example-function [& args] args)
(defmacro example-macro [& args] ~(tuple ,;args))

(macex '(example-macro 1 2 3))

(assert (= (example-function 1 2 3)
           (example-macro 1 2 3)))

(def args [1 2 3])

# `apply` is for functions, but there's always `eval`.
(assert (= (apply example-function args)
           (eval ~(example-macro ,;args))))

# Same return for both.
# => (1 2 3)
apply4kbytePlayground
(def add17 (partial + 17))
(add17 3)            # -> 20
(add17 3 4)          # -> 24
(map add17 [1 2 3])  # -> @[18 19 20]
partialcellularmitosisPlayground
(flatten [1 [2 3 [4]] 5])  # => @[1 2 3 4 5]
flattencellularmitosisPlayground
# Interactively read a line from STDIN
(file/read stdin :line)

# Type abcd and then press ENTER
abcd

# Returns:
@"abcd\n"
stdinsemperosPlayground
(get @"A"                      0)  # => 65 (0b01000001)
(get (buffer/bit-clear @"A" 0) 0)  # => 64 (0b01000000)
(get (buffer/bit-clear @"A" 6) 0)  # =>  1 (0b00000001)
buffer/bit-clearcellularmitosisPlayground
(put  @{:a 4 :b 5}  :c 6         )  # => @{:a 4 :b 5 :c 6}
(put  @{:a 4 :b 5}  :b nil       )  # => @{:a 4}
(put  @{:a 4 :b 5}  :z nil       )  # => @{:a 4 :b 5}

(put  @[:a :b :c]   0  :z        )  # => @[:z :b :c]
(put  @[:a :b :c]   1  nil       )  # => @[:a nil :c]

(put  @[:a :b :c]   5  :d        )  # => @[:a :b :c nil nil :d]

(put  @"hello"      0  "z"       )  # error: can only put integers in buffers
(defn ord [ch] (first (string/bytes ch)))
(ord "z")  # => 122
(put  @"hello"      0  122       )  # => @"zello"
(put  @"hello"      0  (ord "z") )  # => @"zello"
(put  @"hello"      8  (ord "y") )  # => @"hello\0\0\0y"
putcellularmitosisPlayground
(let [x 10]
  (unless (= x 10)
    (print "x is not 10!")
  )
)

# => nil

(let [x 5]
  (unless (= x 10)
    (print "x is not 10!")
  )
)

# => "x is not 10!"
unlessHoangTuan110Playground
(distinct [1 1 2 3])  # => @[1 2 3]

(distinct "hello")  # => @[104 101 108 111]
(string/from-bytes (splice (distinct "hello")))  # => "helo"
distinctcellularmitosisPlayground
(cmp [1 2] [1 2 3])
# => -1
cmpsogaiuPlayground
(defn bench `Feed bench a wrapped func and int, receive int for time in ns`
  [thunk times] 
  (def start (os/clock :cputime :tuple)) 
  (loop [_ :range [times]] 
    (thunk))
  (def end (os/clock :cputime :tuple)) 
  (/ (+ (* (- (end 0) (start 0)) 1e9) 
        (- (end 1) (start 1)))
     times))

# it turns out os/clock is pretty darn fast (comparatively)
(def iterations 2000000)
(bench |(os/clock :cputime :tuple) iterations) # 1283.30053 ns
(bench |(slurp "/proc/self/schedstat") iterations) # 7881.451760 ns
# these basically benchmark slurp
(bench |(do (def f (file/open "/proc/self/schedstat" :r))
            (def content (file/read f :all))
            (file/close f))
       iterations) # 4894.832760 ns
# even without opening and closing the file, reading in Janet's slower than os/clock
(def f (file/open "/proc/self/schedstat" :r)) 
(bench |(do (file/seek f :set 0)  
            (def content (file/read f :all))) iterations)  # 1802.511470 ns
(file/close f)

# Of course bench has some overhead, but it's amortized across iterations anyway
(bench (fn []) 10000000) # 42.030338 ns
os/clockveqqqPlayground
# janet 1.10.1

(= :a :a)    # => true
(= :a "a")   # => false
(= :a ":a")  # => false

(= "a" "a")    # => true
(= "a" @"a")   # => false
(= @"a" @"a")  # => false

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

(= {:a 1} {:a 1})    # => true
(= {:a 1} @{:a 1})   # => false
(= @{:a 1} @{:a 1})  # => false

(= (fn []) (fn []))  # => false
=cellularmitosisPlayground
(def b @"")
(def v (* 1000 (math/random)))
# => 912.753 can differ for you
(xprintf b "Value reached level %f" v)
# => nil
b
# => @"Value reached level 912.752790\n"
xprintfpepePlayground
# :when modifier argument example

(let [even-numbers  @[]]
    (loop [i :range [0 50] :when (even? i)]
        (array/push even-numbers i))
    even-numbers)

# => @[0 2 4 6 8 10 12 14 16 1.....]
loopleobmPlayground
(string/find "needle"
             "hay hay hay needle hay")
# => 12
string/findsogaiuPlayground