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

(get @"A"                      0)  # => 65 (0b01000001)
(get (buffer/bit-clear @"A" 0) 0)  # => 64 (0b01000000)
(get (buffer/bit-clear @"A" 6) 0)  # =>  1 (0b00000001)
buffer/bit-clearcellularmitosisPlayground
# janet 1.10.1

(pos? 1)      # => true
(pos? 1.618)  # => true

(pos? 0)      # => false
(pos? -1)     # => false

(def pi 3.14159)
(pos? pi)       # => true

(pos? nil)      # => true
(pos? false)    # => true
(pos? "hello")  # => true
(pos? :heyo)    # => true
(pos? [-1])     # => true
(pos? {:a 1})   # => true
(pos? (fn []))  # => true
pos?cellularmitosisPlayground
(pp (all-bindings))
# => prints @[% %= * ... yield zero? zipcoll]

(def a "A")
(pp (all-bindings (curenv) true))
# => prints @[_ a] - only local bindings are listed
all-bindingspepePlayground
# From the REPL, always nil
repl> (dyn *current-file*)
nil

# As an expression to the CLI, always nil
$ janet -e '(pp (dyn *current-file*))'
nil

# Given the file /tmp/janet/test.janet with the following contents:
(pp (dyn *current-file*))

# Note how the value reflects the given file path
~ $ janet /tmp/janet/test.janet
"/tmp/janet/test.janet"

/tmp/janet $ janet ./test.janet
"./test.janet"

/tmp/janet $ janet test.janet
"test.janet"

/tmp/janet $ janet ../janet/test.janet
"../janet/test.janet"

# The following are equivalent - the first is preferred.
# The key insight is that dynamic bindings are just keyword/value
# pairs in the current fiber's environment table.
(dyn *current-file*)
(dyn :current-file)
((curenv) *current-file*)
((curenv) :current-file)
*current-file*quexxonPlayground
(table/clear @{:a 1 :b 2})
# => @{}
table/clearsogaiuPlayground
# 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
deep=cellularmitosisPlayground
(<= 1 2 3) # => true
(<= 1 2 1) # => false
<=pepePlayground
(def conn-chan (ev/thread-chan 1000))

(defn producer [no]
  (forever
    (ev/sleep 5)
    (print "Adding data from producer num:" no)
    (ev/give conn-chan (math/random))))

(defn consumer [no]
  (forever
    (ev/sleep 0.5)
    (def num (ev/take conn-chan))
    (print num ": Printing from consumer:" no)))

(defn main [& args]
  (ev/spawn-thread (producer 1))
  (ev/spawn-thread (consumer 1))
  (ev/spawn-thread (consumer 2))
  (ev/sleep 20)
  (print "exiting"))
ev/thread-chanGeo-7Playground
# based on: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/seed.janet#L16 

(def core-api (all-bindings))
(def alldocs @[])

(loop [b :in core-api]
  (when (not= "allbindings" (string b))
    (do
      (def buf @"")
      (with-dyns [:out buf]
        (doc* b)
        (array/push alldocs {:name (string b) :docstring (string buf)})))))

(pp alldocs)
with-dynsveqqqPlayground
(as?-> [1 2 3] _ 
  (sum _)
  (when (> 6 _) _))
# => nil

(as?-> [1 2 3] _ 
  (sum _)
  (when (>= 6 _) _))

# => 6
as?->leobmPlayground
(repeat 12 (-> 12 os/cryptorand pp))

# => @"\xA7li[ \xED\xD2\xF7O\xD6\x15="
# => @">\"-w+\x04\x1C\xC1KG\x9C\xE4"
# => @"\x06b\f\xBD\x12\x19\xB6\x1A\xCA\xB9[\x85"
# => @"\xBE`R\t\x13\x81\xED\x9D#\xD0\x11!"
# => @"\xE2\xC1\xD8\x7F\\\xA7\x84\xC0\v\x8B'\x98"
# => @"\xD6\x0Fz\x86\xE2\xB2\x1D}\xC6'{\xB5"
# => @"\x9D\x97\xA1\x07i\x9FW\x83h4n2"
# => @"d\x8E\xB8\xBA \xA6\x9C\f\xC6\xAD{g"
# => @"\r\xB5\xF84#\xB8c~V\xD7d>"
# => @"\xBB\x19\xB2\xDC\x8B\xD9\x7F\xDC\xBE\f\x88\xE3"
# => @"w\xB50\xF9\xFD\xEB\x1D\xFF:j]\xB8"
# => @"\x8F$\xEBKL~\xFD\t\xA8\xD1\x8C\xC5"
# => nil
repeatjgartePlayground
In order to use :fresh, write:
(import path :fresh true)
importSaikyunPlayground
(array 1 2.3 :a "foo" true nil [] {} (fn []))
# => @[1 2.3 :a "foo" true nil () {} <function 0x7FB2A3F02170>]
arraycellularmitosisPlayground
(next  [4 5 6]    )  # => 0
(next  [4 5 6]  0 )  # => 1
(next  [4 5 6]  1 )  # => 2
(next  [4 5 6]  2 )  # => nil

# note that dictionary keys are not necessarily in the same order
# as the corresponding literal.
(next  {:a 5 :b 6 :c 7}     )  # => :a
(next  {:a 5 :b 6 :c 7}  :a )  # => :c
(next  {:a 5 :b 6 :c 7}  :c )  # => :b
(next  {:a 5 :b 6 :c 7}  :b )  # => nil
nextcellularmitosisPlayground
(buffer/bit @"0" 4)
# => true
buffer/bitsogaiuPlayground