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

# *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
(do
  (def before (dyn :expression))
  (setdyn :expression :smile)
  [before (dyn :expression)])
# => '(nil :smile)
setdynsogaiuPlayground
(nat? 1)       # => true
(nat? 3.14159) # => false
nat?cellularmitosisPlayground
(import joy)

# Given a handler
(defn handle-func [request]
  (joy/text/plain (string "Hi " ((request :params) :name))))
# joy/route will attach it to a route
(joy/route :get "/hi/:name" :handle-func)
# without needing to pass the route(s) to joy/app
(def app (joy/app {}))
(joy/server app 9002)
# visit at localhost:9002/hi/bob
joy/routeveqqqPlayground
(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
(take-while number? 
            (fiber/new
              |(each x [1 2 3 :hi]
                 (yield x))))
# => @[1 2 3]
take-whilesogaiuPlayground
  (->> (all-bindings)
       (keep (fn [sym]
               (when (string/has-prefix? "peg/" (string sym))
                 (string sym))))
       sort)

  # =>
  @["peg/compile"
    "peg/find"
    "peg/find-all"
    "peg/match"
    "peg/replace"
    "peg/replace-all"]

# from https://github.com/sogaiu/margaret/blob/master/tutorials/tutorial.janet
all-bindingsveqqqPlayground
# on a Core i5-4590:
(os/arch)  # => :x64

# on an Intel Atom N270:
(os/arch)  # => :x86

# on a raspberry pi:
(os/arch)  # => :arm
os/archcellularmitosisPlayground
(var x 1)
(+= x 2 3 4)
# x = 10
+=erichaneyPlayground
(brshift 32 2)  # => 8
brshiftcellularmitosisPlayground
(def h ["a" "b" :c]) # => ("a" "b" :c)

(find (fn [a] (= "a" a)) h) # => "a"
findfaywongPlayground
(os/shell "uptime > /tmp/uptime.txt")  # => 0
(slurp "/tmp/uptime.txt")
# => @"22:33  up 5 days,  9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt")  # => nil
os/shellcellularmitosisPlayground
(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground
(get-in  [[4 5] [6 7]]  [0]    42)  # => (4 5)
(get-in  [[4 5] [6 7]]  [0 1]  42)  # => 5

(get-in  [[4 5] [6 7]]  [-1]     42)  # => 42
(get-in  [[4 5] [6 7]]  [9 9 9]  42)  # => 42
get-incellularmitosisPlayground
(first [])  # => nil
(first "abc")  # => 97
firstAndriamanitraPlayground