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

(let [tbl @{:a 1}]
  (merge-into tbl {:b 2})
  tbl)
# => @{:a 1 :b 2}

# real-world example: https://git.sr.ht/~subsetpark/bagatto/tree/19aea03fe23fe5486890912df7dc4a936ce617a3/item/main.janet#L23
merge-intosogaiuPlayground
(def ds @[@{:a 1 :b 2}
          @{:a 8 :b 9}])

(defn thrice [x] (* x 3))

(update-in ds [0 :b] thrice)

# `ds` is now
@[@{:a 1 :b 6}
  @{:a 8 :b 9}]
update-inuvtcPlayground
# Run this in a file.
# Notice how each thread gets its own copy of the environment,
# including the global 'counter' variable.

(var counter 0)
(defn start-thread [name sleep]
  (def chan (ev/thread-chan))
  (ev/spawn-thread
    (repeat 10
      (ev/sleep sleep)
      (++ counter)
      (print name " counter is " counter))
    (print name " has finished")
    (ev/give chan "done"))
  chan)

# Spawn two threads that increment counter
(def chan-a (start-thread "Slow thread" 0.8))
(def chan-b (start-thread "Fast thread" 0.45))

# Wait for both threads to finish
(ev/take chan-a)
(ev/take chan-b)
(print "Global counter is still " counter)
ev/spawn-threadfuxoftPlayground
# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(var a nil)

(var b nil)

(varfn a
  [x]
  (if (zero? x)
    0
    (b x)))
    
(varfn b
  [y]
  (if (pos? y)
    1
    (a y)))
    
(a 1)
# =>
1

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

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground
(print (doc-format "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." 30))

    Lorem ipsum dolor
    sit amet, consectetur
    adipiscing elit, sed
    do eiusmod tempor
    incididunt ut labore
    et dolore magna
    aliqua. Ut enim ad
    minim veniam, quis
    nostrud exercitation
    ullamco laboris nisi
    ut aliquip ex ea
    commodo consequat.
doc-formatbtbytesPlayground
(defn fizzbuzz [n]
  (cond 
    (and
      (= 0 (% n 3))
      (= 0 (% n 5))) "fizzbuzz"
    (= 0 (% n 3)) "fizz"
    (= 0 (% n 5)) "buzz"
    :else n))

(fizzbuzz 1)   # 1
(fizzbuzz 3)   # "fizz"
(fizzbuzz 5)   # "buzz"
(fizzbuzz 15)  # "fizzbuzz"
%cellularmitosisPlayground
(doc doc) # works
(doc* doc) # symbol <function doc> not found
(doc* "doc") # same as (doc doc)
doc*skuzzymigletPlayground
(tuple 1 2.3 :a "foo" true nil [] {} (fn []))
# =>  (1 2.3 :a "foo" true nil () {} <function 0x7FB2A3D030B0>)
tuplecellularmitosisPlayground
(filter (fn [x] (> x 2)) [1 2 3 4 5])  # @[3 4 5]
filterbtbytesPlayground
(def a @[1 2])           # => @[1 2]
(array/fill a @[3 4 5])  # => @[@[3 4 5] @[3 4 5]]
(array/concat (0 a) 6)   # => @[3 4 5 6]
a                        # => @[@[3 4 5 6] @[3 4 5 6]]
array/fillcellularmitosisPlayground
# 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
(defn ls-sockets
  "List the sockets in the current directory."
  []
  (filter
    (fn [fname] (= :socket (os/stat fname :mode)))
    (os/dir ".")))
os/statcellularmitosisPlayground
(var x 1)
(loop [n :range-to [1 5]]
  (*= x n))
x # => 120
*=quexxonPlayground