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 ':s "Battery temperature: 40 °C")

# => 7 index of the first whitespace character

# :s (set " \t\r\n\0\f\v")

peg/findjgartePlayground
(printf "%s %s!" "Hello" "World") # Hello World!
printfaquilaxPlayground
# Suppose you have a fiber that yields chunks of paginated api results:
(def api-results (fiber/new (fn [] (yield [1 2 3]) (yield [4 5 6]))))

# Using :iterate, the right side of the binding is evaluated each time the loop is run,
# which allows for running a side-effecting expression that may be different each time.
(loop [_ :iterate (fiber/can-resume? api-results)] (pp (resume api-results)))

# This example can be simplified using :generate
(loop [chunk :generate api-results] (pp chunk))
loopstaabPlayground
(peg/find-all ':d "Battery temperature: 40 °C")

# => @[21 22] indices containing digits

# :d (range "09")

# peg grammars are contained in a table @{} (mutable) located in boot.janet source file.

peg/find-alljgartePlayground
(kvs (struct :a 1 :b 2))
# => @[:a 1 :b 2]
kvssogaiuPlayground
(math/random)  # 0.487181
math/randombtbytesPlayground
(sort-by
  (fn [x] (get x :b))
   @[{:a 1 :b 100} {:a 100 :b 1} {:a -1 :b 50}])

# -> @[{:a 100 :b 1} {:a -1 :b 50} {:a 1 :b 100}]
sort-byfelixrPlayground
(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
# 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
=cellularmitosisPlayground
(map  (fn [x] (< x 10))    [8 9 10 11])  # => @[true true false false]
(map  (short-fn (< $ 10))  [8 9 10 11])  # => @[true true false false]
(map  |(< $ 10)            [8 9 10 11])  # => @[true true false false]
short-fncellularmitosisPlayground
(map false? [ false nil   true  0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>       @[ true  false false false false false false false false false false   false   ]
false?cellularmitosisPlayground
janet:2:> (all true? [true true false])
false
janet:3:> (all even? [2 4 6 7])
false
janet:5:> (all even? [2 4 6])
true
allswlkrPlayground
(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
(empty? "")
# => true
empty?sogaiuPlayground