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

# access tuple values

(def t [:a :b :c]) # test tuple

(first t)
# => :a

(last t)
# => :c

(slice t 0 2)
# => (:a :b)

# Index as function
(0 t)
# => :a

# Tuple as function
(t 0)
# => :a

# With destructuring
(let [[_ b _] t] 
    b)
# => :b

# With pattern matching
(match t
    [:a _ :d] (print "does not match")
    [:a b :c] (print b)
    _         (print "anything"))

# => :b
tupleleobmPlayground
(def [pipe-r pipe-w] (os/pipe))
(:write pipe-w "hello")
(:read pipe-r 5)
# => @"hello"

# send the string "hello" into the writable stream
# part and read it back from the readable one
os/pipeYohananDiamondPlayground
(var x 5)
(%= x 3)
(pp x) # 2
%=skuzzymigletPlayground
(ev/spawn (os/sleep 1) (print "Hard work is done!"))

# prints "Hard work is done!" after one second
# this is the easiest way to put some forms on the event loop
# but do not forget REPL is blocking, for now, so run the example with `janet -e`
ev/spawnpepePlayground
(string/find "needle"
             "hay hay hay needle hay")
# => 12
string/findsogaiuPlayground
(defn dataframe 
  ``Create an empty dataframe with the given columns``
  [& columns]
  (tabseq [c :in columns] c @[]))
tabseqveqqqPlayground
(string/repeat "moshi" 2)  # => "moshimoshi"
string/repeatcellularmitosisPlayground
(disasm (fn []) :bytecode)
# => @[(retn)]
disasmsogaiuPlayground
(os/shell "uptime > /tmp/uptime.txt")  # => 0
(slurp "/tmp/uptime.txt")
# => @"22:33  up 5 days,  9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt")  # => nil
os/shellcellularmitosisPlayground
# sh/$ can't expand "~" so you must build it:

# (def key "D8E4DB18BF87FLEW7402BBE3AA91B16F4A65C4C9") # use your gpg key ID
(defn copy-and-encrypt-password-store [key-id]
    (with [out (file/open "pass-backup.tar.gz.gpg" :w)]
        (sh/$ tar -czf - ,(string (os/getenv "HOME") "/.password-store")
            | gpg --encrypt --recipient ,key-id > ,out)))

# tar -cz ~/.password-store/ | gpg --encrypt --recipient YOUR_KEY_ID > pass-backup.tar.gz.gpg
sh/$veqqqPlayground
(fiber/root) # => <fiber 0x562FF22F10A0> your hex number will differ
fiber/rootpepePlayground
(file/open "iamfile.txt" :r)
(file/open "iamfile.txt" :w)
(file/open "iamfile.txt" :a)
file/openjgartePlayground
(filter (partial string/has-prefix? "z") (all-bindings))  # => @[zero? zipcoll]
all-bindingscellularmitosisPlayground
(defn walker
  `Simple walker function, that prints non-sequential 
   members of the form or prints "Sequence" and walks 
   recursively sequential members of the tree.` 
  [form] 
  (if (or (indexed? form) (dictionary? form))
    (do (print "Sequence")
        (walk walker form))
    (print form)))

(walk walker [[[[0 1 3]] 16 7 [3 [3 5]] 3 4] 1 [3 4]])

# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
walkpepePlayground
$ janet -e '(os/exit 42)' ; echo $?
42
os/exitcellularmitosisPlayground