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

# channel that can be used for communication between os threads
(def chan (ev/thread-chan 10))

# one thread for sending a message
(ev/do-thread
  (def msg "hi")
  (print "sending: " msg)
  (ev/give chan msg))

# another thread for receiving a message
(ev/do-thread
  (print "received: " (ev/take chan)))

# expected output
#
# sending: hi
# received: hi
ev/do-threadsogaiuPlayground
(type (fiber/new (fn [])))
# =>
:fiber
typesogaiuPlayground
(os/sleep 1)    # => nil
(os/sleep 0.1)  # => nil
(os/sleep 0)    # => nil
os/sleepcellularmitosisPlayground
(get default-peg-grammar :a)
# => '(range "az" "AZ")
default-peg-grammarsogaiuPlayground
# Demonstrate file/flush -- tail %flushtest.csv to see new
# entries appended as the program runs. Otherwise, entries
# wouldn't be visible until file was closed. @oofoe

(def fp (file/open "flushtest.csv" :wb))

(file/write fp "Timestamp,Fiducial\n")
(for i 0 5
  (print "-- Writing " i)
  (file/flush (file/write fp (string (os/time) "," i "\n")))
  (os/sleep (* 5 (math/random))))

(file/close fp)
file/flushoofoePlayground
(get-in  [[4 5] [6 7]]  [0]    42)  # => (4 5)
(get-in  [[4 5] [6 7]]  [0 1]  42)  # => 5

(get-in  [[4 5] [6 7]]  [-1]     42)  # => 42
(get-in  [[4 5] [6 7]]  [9 9 9]  42)  # => 42
get-incellularmitosisPlayground
(math/abs -42)  # => 42
(map math/abs [-1 0 1])  # => @[1 0 1]
math/abscellularmitosisPlayground
(map math/abs   [-2.9 -2.1 2.1 2.9])  # => @[ 2.9  2.1  2.1  2.9 ]
(map math/floor [-2.9 -2.1 2.1 2.9])  # => @[ -3   -3   2    2   ]
(map math/ceil  [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   3    3   ]
(map math/round [-2.9 -2.1 2.1 2.9])  # => @[ -3   -2   2    3   ]
(map math/trunc [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   2    2   ]
math/trunccellularmitosisPlayground
(string/repeat "moshi" 2)  # => "moshimoshi"
string/repeatcellularmitosisPlayground
# There is a list of formatters here: https://janet-lang.org/capi/writing-c-functions.html


(string/format "With terminal colors: %M" [:array {:key-in "struct"}]) # => "With terminal colors: (\e[33m:array\e[0m {\e[33m:key-in\e[0m \e[35m\"struct\"\e[0m})"
string/formatroobiePlayground
(/ 1 2 3)
# => 0.166667
/sogaiuPlayground
(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
(extreme < [1 2 3])  # => 1
(extreme > [1 2 3])  # => 3
extremecellularmitosisPlayground
(buffer/format @"0 - 1 = " "%d" -1)
# =>
@"0 - 1 = -1"
buffer/formatsogaiuPlayground
(defn greet-me [] (print "hey programmer!"))
(defn greet-stranger [] (print "hey stranger!"))

(varfn greet [] (greet-me))
(greet) # prints "hey programmer!"
(varfn greet [] (greet-stranger))
(greet) # prints "hey stranger!"

# kind of analogous to (def greet (fn [] (some-thunk))), but with built-in
# sugar to automatically update 'greet' if varfn is called with a name
# already bound
varfnwrqPlayground