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

# Expanding the example from https://janetdocs.org/core-api/stdin
# We use argparse to provide automatically add flags
# --help comes for free, try it!

(import spork/argparse)

(defn main [&]
  (let [opts (argparse/argparse "Sum numbers from files, stdin or data."
              "multiply" {:kind :option :short "m" :default "1" :map scan-number :help "Multiply the result"}
              "verbose"  {:kind :flag :help "Print the input first"}
              :default   {:kind :accumulate})

        _ (unless opts (os/exit 0)) # lest it crash on help etc.
        args (or (opts :default) []) # give empty? empty array of :default is nil
        data (cond (empty? args) (file/read stdin :all)
                   (os/stat (first args)) (slurp (first args))
                   (string/join args " "))
        total (* (opts "multiply") (sum (flatten (parse-all data))))]

    (if (opts "verbose") (print "Data: " data))
    (print total)))
argparse/argparseveqqqPlayground
(let [p @{:a 1}
      t @{:b 2}]
  (table/setproto t p)
  [(get t :a) (table/rawget t :a)])
# => '(1 nil)
table/rawgetsogaiuPlayground
(take-while number? @[1 2 3 "4" 5 6 7 8 9 "10" 11 "12"]) # => (1 2 3)
take-whilejgartePlayground
(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
(var x 1)
(+= x 2 3 4)
# x = 10
+=erichaneyPlayground
(math/sqrt 9)  # => 3
math/sqrtcellularmitosisPlayground
(all-dynamics)
# => @[:args :err-color :executable :peg-grammar :pretty-format :syspath]

(setdyn :fixed true)
(all-dynamics)
# => @[:args :err-color :executable :fixed :peg-grammar :pretty-format :syspath]
all-dynamicspepePlayground
(symbol? sum)          # => false
(symbol? (quote sum))  # => true
(symbol? 'sum)         # => true
symbol?cellularmitosisPlayground
 new Math.seedrandom('hello.');
math/seedrandomMonif2009Playground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
# Note that the earlier sort func is executed later

(update @{:data @[:cherry :orange]} :data (comp sort |(array/push $ :apple)))
# => @{:data @[:apple :cherry :orange]}
compveqqqPlayground
# *doc-width* is bound to the keyword :doc-width - it can be used in place of
# :doc-width in a call to `dyn`, `setdyn`, or `with-dyns` and is preferable
# to using the keyword directly. When set to a number, it indicates the
# maximum width (in columns) of a row of text returned by `doc-format`.

# - Like *doc-color*, *doc-width* can be used to adjust the output of
#   `doc-format`.
# - When the :doc-width dynamic binding is not set, the default width is 80.
# - By default, `doc-format` adds 4 space indentation and subtracts 8 from
#   the value of the :doc-width dynamic binding to calculate a max width.

# Default:
# repl> (doc doc)
# 
# 
#     macro
#     boot.janet on line 3573, column 1
# 
#     (doc &opt sym)
# 
#     Shows documentation for the given symbol, or can show a list of 
#     available bindings. If sym is a symbol, will look for documentation 
#     for that symbol. If sym is a string or is not provided, will show 
#     all lexical and dynamic bindings in the current environment 
#     containing that string (all bindings will be shown if no string is 
#     given).

# With *doc-width*:
# repl> (with-dyns [*doc-width* 40] (doc doc))
# 
# 
#     macro
#     boot.janet on line 3573, column 1
# 
#     (doc &opt sym)
# 
#     Shows documentation for the 
#     given symbol, or can show a 
#     list of available bindings. 
#     If sym is a symbol, will 
#     look for documentation for 
#     that symbol. If sym is a 
#     string or is not provided, 
#     will show all lexical and 
#     dynamic bindings in the 
#     current environment 
#     containing that string (all 
#     bindings will be shown if 
#     no string is given).
*doc-width*quexxonPlayground
(bnot 255)  # => -256
bnotcellularmitosisPlayground
> (eprin "there is a boo-boo on line " 33 "\n")
there is a boo-boo on line 33
nil
eprincellularmitosisPlayground
# Walk from the API is defined using a case 

(defn walk
  `Iterate over the values in ast and apply f
  to them. Collect the results in a data structure. If ast is not a
  table, struct, array, or tuple,
  returns form.`
  [f form]
  (case (type form)
    :table (walk-dict f form)
    :struct (table/to-struct (walk-dict f form))
    :array (walk-ind f form)
    :tuple (let [x (walk-ind f form)]
             (if (= :parens (tuple/type form))
               (tuple/slice x)
               (tuple/brackets ;x)))
    form))
casepingiunPlayground