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

(string/split "\n" @"works\nfor\nbuffers\ntoo")
# => @["works" "for" "buffers" "too"]
string/splitsogaiuPlayground
(import joy)

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

# joy/defroutes will prepare routes
(joy/defroutes r [:get "/hi/:name" :handle-func])
# which you can pass to joy/app
(joy/server (joy/app {:routes r}) 9002)
# visit localhost:9002/hi/bob
joy/defroutesveqqqPlayground
# Due to: https://github.com/swlkr/janetdocs/issues/50

(defn parse-date
  "Parses YYYY-MM-DD date format to time"
  [date]
  (let [[year month day] (map scan-number (string/split "-" date))]
    (os/mktime {:year year :month (dec month) :month-day (dec day)})))

(print (parse-date "2021-01-01")) # 1609459200
(print (os/strftime "%c" (parse-date "2021-01-01"))) # Fri Jan  1 00:00:00 2021
os/mktimeveqqqPlayground
(print
  (prompt :a 
          (for i 0 1000000000000 
            (print (string "i=" i))
            (if (= i 2) 
                (return :a 10)))))
# output:
# i=0
# i=1
# i=2
# 10
returnfelixrPlayground
(def f (fiber/new (fn [] (yield 2) 3)))
(pp (resume f)) # => 2
(resume f)
(pp (fiber/last-value f)) # => 3
fiber/last-valuepepePlayground
(os/sleep 1)    # => nil
(os/sleep 0.1)  # => nil
(os/sleep 0)    # => nil
os/sleepcellularmitosisPlayground
# can execute query from a file like:
(db/query (slurp "db/sql/random.sql"))

# In context:
(defn show [request]
  (when-let [[name] (request :wildcard)
             name (uri/unescape name) # escaping is important for = and especially %
             name (string/replace "_q" "?" name)
             binding (first (db/query (slurp "db/sql/search.sql") [name]))]

    [:vstack {:spacing "m"}
     (binding-header binding)
     (examples/index (merge request {:binding binding}))]))

# From https://codeberg.org/veqq/janetdocs/src/commit/ac1dc9e3e82f17e8e9ac047297b00803b68034d0/routes/examples.janet#L217
joy/db/queryveqqqPlayground
(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/trunccellularmitosisPlayground
(last [1 1 2 3 5 8])
# => 8
lastsogaiuPlayground
# './foo.janet' is a file with contents '(def bar 42)'.
(use foo)
bar  # => 42
usecellularmitosisPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->>cellularmitosisPlayground
(map |(+ $0 $1) [1 2 3] [4 5 6]) # @[5 7 9] - uses the fn shorthand
mapahungryPlayground
(let [a 1 b 2 c 3] (+ a b c))  # => 6

(let
  [a 1
   b (+ a 1)
   c (+ b 1)]
  (+ a b c))  # => 6
letcellularmitosisPlayground
(type (fiber/new (fn [])))
# =>
:fiber
typesogaiuPlayground
(nat? 1)       # => true
(nat? 3.14159) # => false
nat?cellularmitosisPlayground