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

(max 1 2 3)             # => 3
(max (splice [1 2 3]))  # => 3
(max ;[1 2 3])          # => 3
(apply max [1 2 3])     # => 3
maxcellularmitosisPlayground
(band 7 11)  # => 3

#     0111  (7)
# and 1011  (11)
# --------
#     0011  (3)
bandcellularmitosisPlayground
(table/to-struct @{:a 1}) # => {:a 1}
table/to-structswlkrPlayground
(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
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
(/ 1 2 3)
# => 0.166667
/sogaiuPlayground
(defn capitalize [str]
  (string (string/ascii-upper (string/slice str 0 1)) (string/slice str 1)))
string/ascii-upperveqqqPlayground
# demo of (@ <sym>) feature
(let [content "# => label"]
  (match [:comment @{} "# => label"]
    [:comment _ (@ content)]
    :found
    nil))
# => :found
matchsogaiuPlayground
(any? "")
# => nil

(any? "Hello")
# => 72

(any? @"Hello")
# => 72

(any? @[])
# => nil

(any? [])
# => nil

(any? {})
# => nil

(any? @{})
# => nil

(any? @[1 2 3 4])
# => 1 

(any? @{:a 2 :b 3})
# => 2
any?leobmPlayground
(map true? [ true nil   false 0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>      @[ true false false false false false false false false false false   false   ]
true?cellularmitosisPlayground
# Linux pipes | send data through stdin
# To make linux programs accepting varied input forms:

(defn main [_ & args]
  # If no arguments, read from stdin
  (let [data (if (empty? args) (file/read stdin :all) (string/join args " "))]
    (print (sum (flatten (parse-all data))))))

# This accepts: 1 2 3 or "1" "2" "3" or "1 2 3" or "[1 2 3]" besides piping data in
# janet fib.janet 5 | janet sum.janet

# Allow file inputs also:

(defn main [_ & args]
  (let [data (cond (empty? args) (file/read stdin :all)
                   (os/stat (first args)) (slurp (first args))
                   (string/join args " "))]
    (print (sum (flatten (parse-all data))))))
stdinveqqqPlayground
(def cc (compile '(print "Janet"))) # => <function _thunk> on success returns function
(cc) # => prints Janet
compilepepePlayground
(loop [x :range [1 10]
       :let [square (* x x)]
       :until (> square 9)
       :before (print "before")
       :after (print "after")
       :repeat 2]
  (print (string "square: " square)))

# before
# square: 1
# square: 1
# after
# before
# square: 4
# square: 4
# after
# before
# square: 9
# square: 9
# after

loopstaabPlayground
(try
  (int/u64 "18446744073709551616")
  ([e] e))
# => "bad u64 initializer: string"
int/u64sogaiuPlayground
(/)
# => 1
/sogaiuPlayground