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

# Run this in a file.
# Notice how each thread gets its own copy of the environment,
# including the global 'counter' variable.

(var counter 0)
(defn start-thread [name sleep]
  (def chan (ev/thread-chan))
  (ev/spawn-thread
    (repeat 10
      (ev/sleep sleep)
      (++ counter)
      (print name " counter is " counter))
    (print name " has finished")
    (ev/give chan "done"))
  chan)

# Spawn two threads that increment counter
(def chan-a (start-thread "Slow thread" 0.8))
(def chan-b (start-thread "Fast thread" 0.45))

# Wait for both threads to finish
(ev/take chan-a)
(ev/take chan-b)
(print "Global counter is still " counter)
ev/spawn-threadfuxoftPlayground
# Contrived example returning the variadic arguments passed in.
(defmacro example-macro [& args] ~(tuple ,;args))

(example-macro 1 2 3) # => (1 2 3)
(def args [1 2 3])

# `apply` is for functions, but there's always `eval`.
(assert (= (example-macro 1 2 3)
           (eval ~(example-macro ,;args))))
eval4kbytePlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(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/roundcellularmitosisPlayground
(* 2 3)      # -> 6
(* 2 3.3)    # -> 6.6
(* 2.2 3.3)  # -> 7.26

(def pi 3.14159)
(* 2 pi)  # -> 6.28318

(* 2)  # -> 2
(*)    # -> 1

(* 2 3 4)             # -> 24
(apply * [2 3 4])     # -> 24
(* (splice [2 3 4]))  # -> 24
(* ;[2 3 4])          # -> 24
(def a [2 3 4])
(* ;a)                # -> 24
*cellularmitosisPlayground
(string/find "a" "abcdefa" 1)
# not yet documented start ^ position
# => 6
string/findpepePlayground
(os/execute
  ["python" "-c" "print('Hello Janet'"])
  :p
os/executegoldenHairDafoPlayground
(freeze @{:a @[1 2] 
          :b @{:x @[8 9] 
               :y :smile}})
# => {:a (1 2) :b {:x (8 9) :y :smile}}
freezesogaiuPlayground
(math/exp2 8)  # => 256
math/exp2cellularmitosisPlayground
(os/getenv "TERM")
# => "xterm-256color"
os/getenvsogaiuPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->>cellularmitosisPlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
(neg? -42)  # => true
(map neg? [-1 0 1])  # => @[true false false]
neg?cellularmitosisPlayground
(map  |($ {:a 7 :b 8} )   [  keys      values    kvs             pairs                 ])
# =>                     @[  @[:a :b]  @[7 8]    @[:a 7 :b 8]    @[(:a 7) (:b 8)]      ]

(map  |($ [4 5 6] )       [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1 2]  @[4 5 6]  @[0 4 1 5 2 6]  @[(0 4) (1 5) (2 6)]  ]

(map  |($ 'ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ :ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ "ab" )          [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]
keyscellularmitosisPlayground
# This turns the current REPL session (environment) with all bindings, including synced closures,
# into a loadable or compilable image.
# It is the same as e.g.: `janet -c test.janet test.jimage` after (spit "test.janet" (currenv))

 (spit "test.jimage" (make-image (curenv)))
make-imageveqqqPlayground