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

(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
(defn ls-sockets
  "List the sockets in the current directory."
  []
  (filter
    (fn [fname] (= :socket (os/stat fname :mode)))
    (os/dir ".")))
os/statcellularmitosisPlayground
(peg/find ':d "Battery temperature: 40 °C")
# => 21 index of the first number
peg/findpepePlayground
(defn knorkulate [a b]
  (tracev (+ a (* b b ))))
# <function knorkulate>

(knorkulate 2 34)
# trace on line 2, column 1: (+ a (* b b)) is 1158
# 1158
tracevkamisoriPlayground
(string/slice "hello")    # => "hello"

(string/slice "hello" 0)  # => "hello"
(string/slice "hello" 1)  # => "ello"
(string/slice "hello" 4)  # => "o"
(string/slice "hello" 5)  # => ""

(string/slice "hello" -1)  # => ""
(string/slice "hello" -2)  # => "o"
(string/slice "hello" -5)  # => "ello"
(string/slice "hello" -6)  # => "hello"

(string/slice "hello" 1 1)  # => ""
(string/slice "hello" 1 2)  # => "e"
(string/slice "hello" 1 5)  # => "ello"

(string/slice "hello" -5 -4)  # => "e"
(string/slice "hello" -6 -4)  # => "he"

(string/slice "hello"  2  1)  # => ""
(string/slice "hello" -1 -2)  # => ""
string/slicecellularmitosisPlayground
(<)        # -> true
(< 1)      # -> true
(< 1 2)    # -> true
(< 2 1)    # -> false
(< 1 2 3)  # -> true
(< 1 3 2)  # -> false
<cellularmitosisPlayground
(var x 1)
(loop [n :range-to [1 5]]
  (*= x n))
x # => 120
*=quexxonPlayground
# create a channel without buffer, default is 0
(def channel (ev/chan))

(ev/spawn
  (ev/sleep 5)
  (ev/give channel "Hard work is done!"))

(print "do anything")
(for i 0 5
  (print i)
  (ev/sleep 0.5))

(print (ev/take channel)) # blocks here, until there is a result in the channel
(print "done")

ev/chanleobmPlayground
(peg/find ':a "Battery temperature: 40 °C")

# => 0 index of the first upper/lower case character

# :a (range "az" "AZ")
peg/findjgartePlayground
(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
not=cellularmitosisPlayground
(print
  (prompt :a 
          (for i 0 1000000000000 
            (print (string "i=" i))
            (if (= i 2) 
                (return :a 10)))))
# output:
# i=0
# i=1
# i=2
# 10
promptfelixrPlayground
(find-index |(= $ "b") ["a" "b" "c" "d"]) #=> 1
find-indexAlecTroemelPlayground
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepePlayground
(def channel (ev/chan))

(ev/spawn
 (do
   (for i 0 10
     (ev/give channel (math/random))
     (ev/sleep 0.25))
   (ev/chan-close channel)))

(defn consumer [name delay]
  (loop [item :iterate (ev/take channel)]
    (match item
      [:close _] nil
      v (print name " got " v))))

(ev/call consumer "bob" 1)
(ev/call consumer "alice" 3)
ev/chan-closetxgruppiPlayground
# Get all matches, similar to a global regular expression

(peg/match '(any (+ (<- :a+) 1)) "Hello, world!")
# => @["Hello" "world"]

(peg/match '(any (+ (number :d) 1)) "1A2B3C4D5")
# => @[1 2 3 4 5]

(peg/match '(any (+ (<- :a+) 1)) "12345")
# => @[]
peg/matchTheLastZombiePlayground