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

(one? (math/next 1 math/inf))  # => false
math/nextcellularmitosisPlayground
# 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
(def a @[1 2])
(array/concat a 3 [4 5] @[6 7] [] @[] 8)
a  # => @[1 2 3 4 5 6 7 8]
array/concatcellularmitosisPlayground

# sh/$'s contents are quasiquoted, allowing direct or string arguments
# so you need to unquote , variables:

(def out (file/open "trust-db.txt" :w))
(sh/$ "gpg" "--export-ownertrust" > ,out) # > requires an opened file object
(file/close out)

# note how > requires an opened file object
(with [out (file/open "trust-db.txt" :w)]
  (sh/$ gpg --export-ownertrust > ,out))
sh/$veqqqPlayground
(os/lstat "t.janet")
# @{:size 249 :permissions "rw-r--r--" :nlink 1 :blocks 8 :dev 16777221 :accessed 1606236760 :modified 1606236759 :uid 501 :mode :file :blocksize 4096 :changed 1606236759 :inode 14801850 :rdev 0 :int-permissions 420 :gid 501}

(get (os/lstat "t.janet") :size)  # 249
os/lstatbtbytesPlayground
(bnot 255)  # => -256
bnotcellularmitosisPlayground
(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
(sorted [1 -2 2 3 9 -10])  # @[-10 -2 1 2 3 9]
sortedbtbytesPlayground
(string? "hello")   # => true
(string? @"hello")  # => false
(string? :hello)    # => false
string?cellularmitosisPlayground
(invert {:a 1 :b 2 :c 3})
# => @{3 :c 1 :a 2 :b}
invertsogaiuPlayground
(var buf @"")
(buffer/push-word buf 2147483647)

(+
  (get buf 0)                # byte 1
  (blshift (get buf 1) 8)    # byte 2
  (blshift (get buf 2) 16)   # byte 3
  (blshift (get buf 3) 24))  # byte 4

# => 2147483647
buffer/push-wordleobmPlayground
(get default-peg-grammar :h)
# => '(range "09" "af" "AF")
default-peg-grammarsogaiuPlayground
(take 2 [1 -2 2 3 9 -10])  # (1 -2)
takebtbytesPlayground
(repeat 3 (print "HO"))
# => prints
# HO
# HO
# HO
repeatpepePlayground
(<= 1 2 3) # => true
(<= 1 2 1) # => false
<=pepePlayground