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

# janet 1.10.1

(all pos? [1 2 3])     # => true
(all pos? [1 2 3 -4])  # => false
(all pos? [1 2 3 0])   # => false

(all (partial string/has-prefix? "a") ["aa" "ab"])       # => true
(all (partial string/has-prefix? "a") ["aa" "ab" "bb"])  # => false

(all truthy? [1 2])              # => true
(all truthy? [1 2 3])            # => true
(all truthy? [1 2 nil 3])        # => false
(all truthy? [1 false 2 nil 3])  # => false

(all (fn [x] x) [1 2])              # => 2
(all (fn [x] x) [1 2 3])            # => 3
(all (fn [x] x) [1 2 nil 3])        # => nil
(all (fn [x] x) [1 false 2 nil 3])  # => false
allcellularmitosisPlayground
(not=        [1 1]   [1 1])  # => false
(not=        [1 1]   [2 3])  # => true
(not=        [1 1]  @[1 1])  # => true
(not=        [1 1]  @[2 3])  # => true
(not=       @[1 1]  @[1 1])  # => true
(not=       @[1 1]  @[2 3])  # => true

(deep-not=   [1 1]   [1 1])  # => nil
(deep-not=   [1 1]   [2 3])  # => true
(deep-not=   [1 1]  @[1 1])  # => true
(deep-not=   [1 1]  @[2 3])  # => true
(deep-not=  @[1 1]  @[1 1])  # => nil
(deep-not=  @[1 1]  @[2 3])  # => true
deep-not=cellularmitosisPlayground
# Flatten single level of nested collections

(def foo [ [[1 2] [3 4]]   
           [[5 6] [7 8]] ])

(apply array/concat @[] foo) # => @[(1 2) (3 4) (5 6) (7 8)]
array/concatmaradPlayground
# nested iteration
(loop [a :in [100 200 300]
       b :in [1 2 3]]
   (print (+ a b)))

# 101
# 102
# 103
# 201
# 202
# 203
# 301
# 302
# 303
looperichaneyPlayground
(string? "hello")   # => true
(string? @"hello")  # => false
(string? :hello)    # => false
string?cellularmitosisPlayground
(def a @[1 2])
(array/concat a 3 [4 5] @[6 7] [] @[] 8)
a  # => @[1 2 3 4 5 6 7 8]
array/concatcellularmitosisPlayground
(math/log (* math/e math/e))  # => 2
(math/log2 256)               # => 8
(math/log10 1000)             # => 3
math/log2cellularmitosisPlayground
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:c]          9)  # => @{:a @{:aa 5} :b @{:bb 6} :c 9}
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:a :aa]      9)  # => @{:a @{:aa 9} :b @{:bb 6}}
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:a :cc :ddd] 9)  # => @{:a @{:cc @{:ddd 9} :aa 5} :b @{:bb 6}}

(put-in  @[:a :b @[:aa :bb :cc]]  [2 1] :zz )  # => @[:a :b @[:aa :zz :cc]]
(put-in  @[:a :b @[:aa :bb :cc]]  [5]   :z  )  # => @[:a :b @[:aa :bb :cc] nil nil :z]

(put-in  @[:a :b @[:aa :bb :cc]]  [4 0]   :zz )  # => @[:a :b @[:aa :bb :cc] nil @{0 :zz}]
(put-in  @[:a :b @[:aa :bb :cc]]  [4 :yy] :zz )  # => @[:a :b @[:aa :bb :cc] nil @{:yy :zz}]

put-incellularmitosisPlayground
(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
(filter (partial string/has-prefix? "z") (all-bindings))  # => @[zero? zipcoll]
all-bindingscellularmitosisPlayground
# janet 1.10.1

(array/remove @[1 2 3] 0)  # => @[2 3]
(array/remove @[1 2 3] 1)  # => @[1 3]
(array/remove @[1 2 3] 2)  # => @[1 2]
(array/remove @[1 2 3] 3)  # => @[1 2 3]
(array/remove @[1 2 3] 4)  # error: index out of range

(array/remove @[1 2 3] -1)  # => @[1 2 3]
(array/remove @[1 2 3] -2)  # => @[1 2]
(array/remove @[1 2 3] -3)  # => @[1 3]
(array/remove @[1 2 3] -4)  # => @[2 3]
(array/remove @[1 2 3] -5)  # error: index out of range

(array/remove @[1 2 3 4] 1 1)   # => @[1 3 4]
(array/remove @[1 2 3 4] 1 2)   # => @[1 4]
(array/remove @[1 2 3 4] 1 3)   # => @[1]
(array/remove @[1 2 3 4] 1 4)   # => @[1]
(array/remove @[1 2 3 4] 1 99)  # => @[1]
array/removecellularmitosisPlayground
# wrap short-fn / | 
(-> 10
    (|(/ $ 2)))
# =>
5

# also wrap fn
(-> 10
    ((fn [n] (/ n 2))))
# =>
5
->sogaiuPlayground
# opens a file named filename for writing, and writes Hello World!
(def f (file/open "filename" :w))
(file/write f "Hello World!")
(file/flush f)
(file/close f)
file/writeterminalcommandPlayground
(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
(defmacro timeit [& body]
    # generate unique symbols to use in the macro so they can't conflict with anything used in `body`
    (with-syms [$t0 $t1]
        ~(do
            (def $t0 (os/clock :monotonic :double))
            (do ,;body)
            (def $t1 (os/clock :monotonic :double))
            (- $t1 $t0))))

(def time-taken (timeit (os/sleep 0.5)))
(printf "Took %.3f seconds" time-taken)
with-symsAndriamanitraPlayground