JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules
Loading...

Recent examples

(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
(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
(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
(defn array-difference [array-1 array-2]
  (filter |(not (has-value? array-2 $)) array-1))

(array-difference [1 2 3 4 5] [2 3 4]) # => @[1 5]
has-value?veqqqPlayground
# based on: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/seed.janet#L16 

(def core-api (all-bindings))
(def alldocs @[])

(loop [b :in core-api]
  (when (not= "allbindings" (string b))
    (do
      (def buf @"")
      (with-dyns [:out buf]
        (doc* b)
        (array/push alldocs {:name (string b) :docstring (string buf)})))))

(pp alldocs)
with-dynsveqqqPlayground
# From: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/main.janet#L30

# where in .env you have:
# DATABASE_URL=examples-db.sqlite3
# PORT=9025


(defn main [& args]
  (joy/db/connect (joy/env :database-url))
  (server app (joy/env :port) "0.0.0.0")
  (joy/db/disconnect))
joy/envveqqqPlayground
# From: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/helpers.janet#L52

(defn current-account [request]
  (def login (get-in request [:session :login] ""))
  (joy/db/find-by :account :where {:login login}))
joy/db/find-byveqqqPlayground
# 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
(assertf true "this sentence is %n" false)
# => true
assertfsogaiuPlayground
# Demonstrate file/flush -- tail %flushtest.csv to see new
# entries appended as the program runs. Otherwise, entries
# wouldn't be visible until file was closed. @oofoe

(def fp (file/open "flushtest.csv" :wb))

(file/write fp "Timestamp,Fiducial\n")
(for i 0 5
  (print "-- Writing " i)
  (file/flush (file/write fp (string (os/time) "," i "\n")))
  (os/sleep (* 5 (math/random))))

(file/close fp)
file/flushoofoePlayground
# 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
# To resume an image's environment:

# With this example image:
# echo "(def a 1)" > test.janet
# janet -c test.janet test.jimage
# Now open the REPL:

# To enter an image, for continued development or exploration. N.b. this does not work as expected:
# See: https://janet.zulipchat.com/#narrow/channel/409517-help/topic/Image.20Based.20Development.3F/with/529177765

(defn restore-image [image]
  (loop [[k v] :pairs image]
    (put (curenv) k v)))

(restore-image (load-image (slurp "test.jimage")))
load-imageveqqqPlayground
(find |(string/has-prefix? "a" $) ["be" "cat" "art" "apple"])

# => "art"

findveqqqPlayground
#1) start janet normally, work in the repl, encounter something to debug:

(debug)
# debug: 
#   in thunk [repl] (tail call) on line 4, column 1
# nil
# repl:5:> 

#2) because the -d flag was missing on start, debug is skipping back out into the repl. now i really DO want to debug here and now, without restarting the vm:

(dyn *debug*)
# => nil

#3) we can just set the flag ourself and thus activate the debugger:
(setdyn *debug* :anything_truthy)


(debug)

# debug: 
#   in thunk [repl] (tail call) on line 2, column 1
# entering debug[1] - (quit) to exit
# debug[1]:1:>
*debug*kamisoriPlayground
(defn knorkulate [a b]
  (tracev (+ a (* b b ))))
# <function knorkulate>

(knorkulate 2 34)
# trace on line 2, column 1: (+ a (* b b)) is 1158
# 1158
tracevkamisoriPlayground