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

# 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
(doc doc) # works
(doc* doc) # symbol <function doc> not found
(doc* "doc") # same as (doc doc)
doc*skuzzymigletPlayground
(os/perm-string 420)    # => "rw-r--r--"
(os/perm-string 8r644)  # => "rw-r--r--"
(os/perm-string 493)    # => "rwxr-xr-x"
(os/perm-string 8r755)  # => "rwxr-xr-x"
os/perm-stringcellularmitosisPlayground
(sort-by
  (fn [x] (get x :b))
   @[{:a 1 :b 100} {:a 100 :b 1} {:a -1 :b 50}])

# -> @[{:a 100 :b 1} {:a -1 :b 50} {:a 1 :b 100}]
sort-byfelixrPlayground
# create a channel without buffer, default is 0
(def channel (ev/chan))

(ev/spawn
  (ev/sleep 5)
  (ev/give channel "Hard work is done!"))

(print "do anything")
(for i 0 5
  (print i)
  (ev/sleep 0.5))

(print (ev/take channel)) # blocks here, until there is a result in the channel
(print "done")

ev/chanleobmPlayground
#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
(def a-tuple-1 [:number 10])
(def a-tuple-2 [:string "Hello there!"])
(defn show [what]
  (match what
    [:number n] (printf "It's a number! %d" n)
    [:string s] (printf "Here's a string: %s" s)
    _ (printf "I dunno what this is: %q" what)))

(show a-tuple-1)
(show a-tuple-2)
(show [:array @[]])
matchroobiePlayground
(let [c0 (ev/chan)
      c1 (ev/chan 1)]
  [(ev/capacity c0) (ev/capacity c1)])
# => '(0 1)
ev/capacitysogaiuPlayground
(let [c (ev/chan 2)]
  (ev/give c :one)
  (def first-check (ev/full c))
  (ev/give c :two)
  (def second-check (ev/full c))
  (ev/take c)
  (def third-check (ev/full c))
  [first-check second-check third-check])
# => '(false true false)
ev/fullsogaiuPlayground
(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
# :when modifier argument example

(let [even-numbers  @[]]
    (loop [i :range [0 50] :when (even? i)]
        (array/push even-numbers i))
    even-numbers)

# => @[0 2 4 6 8 10 12 14 16 1.....]
loopleobmPlayground
(spit "/tmp/hello.sh" "#!/bin/bash\necho 'Hello from Bash!'\n")
(os/chmod "/tmp/hello.sh" "rwx------")
(os/setenv "PATH" (string (os/getenv "PATH") ":/tmp"))
(os/shell "hello.sh")
os/chmodcellularmitosisPlayground
(map dictionary? [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         true          ]

(map struct?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         false         ]

(map table?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     false        true          ]
dictionary?cellularmitosisPlayground
(group-by 
  (fn [i] (i :label)) 
  [{:label 'A :value 4} {:label 'A :value 3} {:label 'B :value 5}])
# @{A @[{:label A :value 4} {:label A :value 3}] B @[{:label B :value 5}]}
group-byaquilaxPlayground
(os/shell "seq 12 | head -n 1") # => 1
os/shelljgartePlayground