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...

Recent Examples

(defn- parse-timestamp
  ``Parse a date or datetime string into a unix epoch int``
  [s]
  (if (string/find " " s)
    # datetime: "yyyy-MM-dd HH:mm"
    (let [[date-part time-part] (string/split " " s)
          [y m d]               (string/split "-" date-part)
          [hh mm]               (string/split ":" time-part)]
      (os/mktime {:year         (scan-number y)
                  :month        (scan-number m)
                  :month-day    (scan-number d)
                  :hours        (scan-number hh)
                  :minutes      (scan-number mm)
                  :seconds      0}))
    # date: "yyyy-MM-dd"
    (let [[y m d] (string/split "-" s)]
      (os/mktime {:year      (scan-number y)
                  :month     (scan-number m)
                  :month-day (scan-number d)
                  :hours 0 :minutes 0 :seconds 0}))))
os/mktimeveqqqPlayground
(defn unix->date-label
  ``Format a unix timestamp as date or datetime string``
  [ts]
  (let [d (os/date ts)]
    (if (or (pos? (d :hours)) (pos? (d :minutes)))
      (string/format "%d-%02d-%02d %02d:%02d"
                     (d :year) (d :month) (d :month-day)
                     (d :hours) (d :minutes))
      (string/format "%d-%02d-%02d"
                     (d :year) (d :month) (d :month-day)))))
os/dateveqqqPlayground
(defn dataframe 
  ``Create an empty dataframe with the given columns``
  [& columns]
  (tabseq [c :in columns] c @[]))
tabseqveqqqPlayground
# 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
# 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
(sandbox :fs) # disable access to the sandbox
(os/mkdir "lol")

# error: operation forbidden by sandbox
#   in os/mkdir [src/core/os.c] on line 2146
#   in thunk [repl] (tail call) on line 30, column 1
sandboxveqqqPlayground
(import spork/charts :as c)
(c/line-chart
  :width 400 :height 200
  :data {:timestamp [1 2 3 5 40 60]
         :temperature-1 [75.1 75.2 75.4 75.5 75.5 75.4]
         :temperature-2 [55.1 55.4 55.7 60.0 60.4 60.9]}
  :save-as "bla.png"
  :x-column :timestamp
  :y-column [:temperature-1 :temperature-2])

# then open bla.png to view
# bakpakin shared this example
spork/charts/line-chartveqqqPlayground
(import spork/charts :as c)
(import spork/gfx2d :as g)
(g/save "bla.png" (c/plot-line-graph
                   :canvas (g/blank 100 100)
                   :data {:timestamp [1 2 3 40 5 60]
                          :temperature-1 [75.1 75.2 75.4 75.5 75.5 75.4]
                          :temperature-2 [55.1 55.4 55.7 60.0 60.4 60.9]}
                   :x-column :timestamp
                   :y-column [:temperature-1 :temperature-2]))

# then you can look at bla.png
spork/charts/plot-line-graphveqqqPlayground
(range 10000) # @[0 1 2 ... 9997 9998 9999]

# By default, pp truncates. You can change its behavior:
(setdyn :pretty-format "%j") # jdn format
(setdyn :pretty-format "%m") # full printing without truncation
# For a list of all print formats: https://janet-lang.org/capi/writing-c-functions.html#Panicking

(range 10000) # Prints all of them according to your dyn
ppveqqqPlayground
(def f (file/temp))
(file/write f "ok stuff")
(file/seek f :set 0)
(file/read f :all)

# N.b. file/temp's file is not accessible outside of the process
file/tempveqqqPlayground
# 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
# :as sets a /

(import /deeper/inside :prefix "" :export true)
# @{_ @{:value <cycle 0>} cat @{:private false}}
(import /deeper/inside :as "" :export true)
# @{/cat @{:private false} _ @{:value <cycle 0>} cat @{:private false}}

importveqqqPlayground
path/delim # => ":" on Unix and Linux, ";" on Windows
spork/path/delimclementiPlayground
  (->> (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
# The optional array arg is used as the basis for the returned array

(os/dir "/tmp/dir_1" @["file_3" "file_4"]) # => @["file_3" "file_4" "file_1" "file_2"]
os/dirsnltdPlayground