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

(as-> [1 2 3] _ 
  (map inc _)
  (sum _)
  (- _ 10)
  (< _ 0))
# -> true

# same as
(< (- (sum (map inc [1 2 3])) 10) 0)
as->felixrPlayground
(get-in  {:a {:cc 2} :b {:dd 3}}  [:a]     42)  # => {:cc 2}
(get-in  {:a {:cc 2} :b {:dd 3}}  [:a :cc] 42)  # => 2

(get-in  {:a {:cc 2} :b {:dd 3}}  [0]  42)  # => 42
(get-in  {:a {:cc 2} :b {:dd 3}}  [:z] 42)  # => 42
get-incellularmitosisPlayground
(defn fizzbuzz [n]
  (cond 
    (and
      (= 0 (% n 3))
      (= 0 (% n 5))) "fizzbuzz"
    (= 0 (% n 3)) "fizz"
    (= 0 (% n 5)) "buzz"
    :else n))

(fizzbuzz 1)   # 1
(fizzbuzz 3)   # "fizz"
(fizzbuzz 5)   # "buzz"
(fizzbuzz 15)  # "fizzbuzz"
%cellularmitosisPlayground
(def a @[1 2])
(array/pop a)  # => 2
a              # => @[1]
(array/pop a)  # => 1
a              # => @[]
(array/pop a)  # => nil
a              # => @[]
array/popcellularmitosisPlayground
(import joy)

# given a handler
(defn handle-func [request]
  (joy/text/plain (string "Hi " ((request :params) :name))))

# joy/routes will make handlers
(def r (joy/routes [:get "/hi/:name" :handle-func]))
# which you can pass to joy/app
(joy/server (joy/app {:routes r}) 9002)
# visit at localhost:9002/hi/bob
joy/routesveqqqPlayground
$ janet -e '(os/exit 42)' ; echo $?
42
os/exitcellularmitosisPlayground
(/ 1 2 3)
# => 0.166667
/sogaiuPlayground
(os/cryptorand 1)  # => @"\n"
(os/cryptorand 1)  # => @"<"
(os/cryptorand 1)  # => @"\xC0"
(os/cryptorand 1)  # => @"\x89"

(os/cryptorand 8)  # => @"\x87\x13\x99\x95\x10su\e"
os/cryptorandcellularmitosisPlayground
# 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
(def p (os/spawn ["ls"] :p {:in :pipe :out :pipe})) # define core/process with selfpipe
(pp (:read (p :out) :all)) # => prints the ls output
(pp (:wait p)) # => waits for the process to finish, prints 0 if succesful 
os/spawnpepePlayground
(last 'hello)
# => 111
lastsogaiuPlayground
(string/slice "hello")    # => "hello"

(string/slice "hello" 0)  # => "hello"
(string/slice "hello" 1)  # => "ello"
(string/slice "hello" 4)  # => "o"
(string/slice "hello" 5)  # => ""

(string/slice "hello" -1)  # => ""
(string/slice "hello" -2)  # => "o"
(string/slice "hello" -5)  # => "ello"
(string/slice "hello" -6)  # => "hello"

(string/slice "hello" 1 1)  # => ""
(string/slice "hello" 1 2)  # => "e"
(string/slice "hello" 1 5)  # => "ello"

(string/slice "hello" -5 -4)  # => "e"
(string/slice "hello" -6 -4)  # => "he"

(string/slice "hello"  2  1)  # => ""
(string/slice "hello" -1 -2)  # => ""
string/slicecellularmitosisPlayground
(let [len 8
      rand-string (string/join (map |(string/format "%02x" $)
                                    (os/cryptorand len)))]
  (= (length rand-string) (* 2 len)))
# => true
os/cryptorandsogaiuPlayground
(buffer/format @"0 - 1 = " "%d" -1)
# =>
@"0 - 1 = -1"
buffer/formatsogaiuPlayground
(os/lstat "t.janet")
# @{:size 249 :permissions "rw-r--r--" :nlink 1 :blocks 8 :dev 16777221 :accessed 1606236760 :modified 1606236759 :uid 501 :mode :file :blocksize 4096 :changed 1606236759 :inode 14801850 :rdev 0 :int-permissions 420 :gid 501}

(get (os/lstat "t.janet") :size)  # 249
os/lstatbtbytesPlayground