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...

Recent Examples

(let (sinlu (array/new 10))  # 10 element look up table.
  (for i 0 10 (put sinlu i (math/sin (* math/pi (/ i 10)))))
  (pp sinlu))
array/newoofoePlayground
(let (x @["This" "is" "a"]     # Must be array.
      y @["test" "of"]       
      z  ["the" "emergency!"])  # Tuple OK for part.
  (pp (array/join x y z)))

# @["This" "is" "a" "test" "of" "the" "emergency!"]
array/joinoofoePlayground
(import spork/json)
(def input `` {"name": "person", "unknown": null, "happy": true, "sad": false} ``)

When calling, the json dictionary becomes a mutable table in janet.

(json/decode input)
@{"happy" true "name" "person" "sad" false "unknown" :null}

pass `true` as the first arg, and json keys become janet keywords.

(json/decode input true)
@{:happy true :name "person" :sad false :unknown :null}

pass `true` as the second arg, and json keys that map to `null` are not hydrated.

(json/decode input true true)
@{:happy true :name "person" :sad false}



spork/json/decodenickfunPlayground
(print "The build hash of your Janet executable is " janet/build ".")
janet/buildoofoePlayground
# Janet v1.42.0 -- See %src/include/janet.h.
(defn set? [position] (["not set" "set"] (band janet/config-bits position)))
(printf ```
Configurations:
* NANBOX %s.
* SINGLE_THREADED %s.
* NANBOX_64_POINTER_SHIFT %s.``` (set? 0x1) (set? 0x2) (set? 0x4))
janet/config-bitsoofoePlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)     # Watch for all changes.
(filewatch/add ww "fadd.janet"  :modify)  # Only care if modified.
(filewatch/listen ww)         # Start filewatcher listening for changes.

# Note that we may still get events for files that have been removed
# so we have to make sure we don't remove them twice.

(var careless "Have we already removed %fadd.janet?" false)
(forever
 (let (item (ev/take cc))
   (pp (ev/take cc))
   (when (and (not careless) (= "fadd.janet" (item :wd-path)))
     (print "I don't care about this file anymore.")
     (filewatch/remove ww (item :wd-path))
     (set careless true)
     )))
filewatch/removeoofoePlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)     # Watch for all changes.
(filewatch/add ww "fadd.janet"  :modify)  # Only care if modified (Linux-specific flag).
(filewatch/listen ww)         # Start filewatcher listening for changes.

(pp (ev/take cc))             # Print info on first file to change.
(filewatch/unlisten ww)       # Stop caring about changes.
filewatch/addoofoePlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)     # Watch for all changes.
(filewatch/add ww "fadd.janet"  :modify)  # Only care if modified.
(filewatch/listen ww)         # Start filewatcher listening for changes.

(pp (ev/take cc))             # Print info on first file to change.
(filewatch/unlisten ww)       # Stop caring about changes.
                              # This allows the program to exit -- otherwise
                              # filewatcher listening thread keeps running.
filewatch/unlistenoofoePlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)  # File to watch.
(filewatch/listen ww)         # Start filewatcher listening for changes.

(ev/spawn-thread              # Spawn thread to catch change events.
 (print "Waiting...")
 (forever (pp (ev/take cc))))
filewatch/listenoofoePlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)  # File to watch.
(filewatch/listen ww)         # Start filewatcher listening for changes.

(ev/spawn-thread              # Spawn thread to catch change events.
 (print "Waiting...")
 (forever (pp (ev/take cc))))
filewatch/newoofoePlayground
(when (def x 10)
  (string "Got value: " x))
# "Got value: 10"

(when (def x nil)
  (string "Got value: " x))
# nil
whenyohannd1Playground
# Since table/clone produces a shallow copy and composing freeze and thaw makes everything mutable/immutable
(defn deep-clone [ds]
  (prewalk
    |(cond
       (table? $) (table/clone $)
       (array? $) (array/slice $)
       (buffer? $) (buffer/slice $)
       $)
  ds))
# CalebF wrote this: https://janet.zulipchat.com/#narrow/channel/409517-help/topic/table.2Fclone.20doesn.27t.20make.20a.20deep.20copy.3F/near/617675442
prewalkveqqqPlayground
(import spork/misc)
(def d (misc/capout (doc misc/capout)))

d # returns:
@"\n\n    macro\n    /usr/local/lib/janet/spork/misc.janet on 
line 340, column 1\n\n    \e[97m(capout & body)\e[39m\n\n   
 Captures the standard output of the variadic \e[97mbody\e[39m
 and returns it as \n    a buffer.\n\n\n"
spork/misc/capoutveqqqPlayground
# 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
(mapcat
  |[$0 $1 (* $0 $1)]
  [1 2 3]
  [100 200 300])
# => @[1 100 100 2 200 400 3 300 900]
mapcattaoeffectPlayground