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

# catch and propagate an error with fiber

(try
  (+ 1 nil)
  ([err fib]
   (propagate err fib)))
tryswlkrPlayground
(keyword :)
# => :
keywordsogaiuPlayground
(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/ceilcellularmitosisPlayground
# Find all bindings (in the current environment) with "push" in the name.
(doc "push")

#    Bindings:
#
#    array/push buffer/push buffer/push-byte buffer/push-string
#    buffer/push-word
#
#    Dynamics:
#
#
#
#    Use (doc sym) for more information on a binding.
docuvtcPlayground
(os/dir "./")                   # => @[]
(os/shell "touch foo bar baz")  # => nil
(os/dir "./")                   # => @["foo" "bar" "baz"]
os/dircellularmitosisPlayground
math/e 
# => 2.71828
math/eleobmPlayground
(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
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
returnsogaiuPlayground
#1) start janet normally, work in the repl, encounter something to debug:

(debug)
# debug: 
#   in thunk [repl] (tail call) on line 4, column 1
# nil
# repl:5:> 

#2) because the -d flag was missing on start, debug is skipping back out into the repl. now i really DO want to debug here and now, without restarting the vm:

(dyn *debug*)
# => nil

#3) we can just set the flag ourself and thus activate the debugger:
(setdyn *debug* :anything_truthy)


(debug)

# debug: 
#   in thunk [repl] (tail call) on line 2, column 1
# entering debug[1] - (quit) to exit
# debug[1]:1:>
*debug*kamisoriPlayground
# channels that can be used for communication between os threads
(def chan-a (ev/thread-chan 10))
(def chan-b (ev/thread-chan 10))

# one thread
(ev/thread
  (fiber/new
    (fn []
      (def msg "hi")
      (print "thread 1 sending: " msg)
      (ev/give chan-a msg)
      #
      (print "thread 1 received: " (ev/take chan-b))))
  :1
  :n)

# another thread
(ev/thread
  (fiber/new
    (fn []
      (print "thread 2 received: " (ev/take chan-a))
      #
      (def msg "peace")
      (print "thread 2 sending: " msg)
      (ev/give chan-b msg)))
  :2
  :n)

# expected output
#
# thread 1 sending: hi
# thread 2 received: hi
# thread 2 sending: peace
# thread 1 received: peace
ev/threadsogaiuPlayground
(sort (keys default-peg-grammar))
# => @[:A :D :H :S :W :a :a* :a+ :d :d* :d+ :h :h* :h+ :s :s* :s+ :w :w* :w+]
default-peg-grammarsogaiuPlayground
(scan-number "1_000_000")
# => 1000000
scan-numbersogaiuPlayground
(string/find-all "duck" "duck duck duck goose!") # => @[0 5 10]
string/find-allsogaiuPlayground
# janet 1.10.1

# note that os/cd does not appear to update PWD in the shell's environment.
(os/cwd)           # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(os/cd "/tmp")     # => nil
(os/cwd)           # => "/private/tmp"  (on Apple, /tmp is actually /private/tmp)
(os/getenv "PWD")  # => "/Users/cell/tmp"
os/cdcellularmitosisPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->cellularmitosisPlayground