JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!

Loading...

Random Examples

# If your tooling or the compiler doesn't believe a symbol will be bound e.g.:
(def a b) # if b e.g. gets bound by some macro or pushed directly to the env

# You can wrap it with compwhen on a bool:
(var- bla false)
(compwhen bla
          (def a b))

# Whatever defines b should then set bla to true
# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet
compwhenveqqqPlayground
# './foo.janet' is a file with contents '(def bar 42)'.
(use foo)
bar  # => 42
usecellularmitosisPlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)     # Watch for all changes.
(filewatch/add ww "fadd.janet"  :modify)  # Only care if modified (Linux-specific flag).
(filewatch/listen ww)         # Start filewatcher listening for changes.

(pp (ev/take cc))             # Print info on first file to change.
(filewatch/unlisten ww)       # Stop caring about changes.
filewatch/addoofoePlayground
(parse "(+ 4 5)") # => (+ 4 5)
(type (parse "(+ 4 5)")) # => :tuple
parseskuzzymigletPlayground
(os/shell "seq 12 | head -n 1") # => 1
os/shelljgartePlayground
(string/repeat "moshi" 2)  # => "moshimoshi"
string/repeatcellularmitosisPlayground
(table/to-struct @{:a 1}) # => {:a 1}
table/to-structswlkrPlayground
(table ;[:a 1 :b 2 :c 3])
# => @{:a 1 :b 2 :c 3}
tablesogaiuPlayground
(do
  (var a 88)
  (+= a 12))  # 100
+=btbytesPlayground
(dec 42)  # => 41
(map dec [1 2 3])  # => @[0 1 2]
deccellularmitosisPlayground
(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
(group-by 
  (fn [i] (i :label)) 
  [{:label 'A :value 4} {:label 'A :value 3} {:label 'B :value 5}])
# @{A @[{:label A :value 4} {:label A :value 3}] B @[{:label B :value 5}]}
group-byaquilaxPlayground
# due to 0 indexing, you will often want to add 1 to things:
(defn short-date [d]
  (let [{:year y :month mon :month-day d} d]
    (string y "-" (+ 1 mon) "-" (+ 1 d))))

# Makes os/date like 2025-12-25
(short-date (os//date))


# From: https://codeberg.org/veqq/deforester/src/branch/master/deforester.janet
(defn- time-string 
  ``Gives current time as ISO 8601 string: https://en.wikipedia.org/wiki/ISO_8601
  This accounts for `os/date` 0-indexing month and days which are 1-indexed in ISO 8601.``
  []
  (let [now (os/clock :realtime)
        ms (math/floor (* 1000 (mod now 1)))
        {:year y :month mon :month-day d :hours h :minutes min :seconds s}
        (os/date (math/floor now))]
    (string/format "%d-%02d-%02dT%02d:%02d:%02d.%03dZ" y (inc mon) (inc d) h min s ms)))
os/dateveqqqPlayground
# 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
# note that 'not' works as an implementation of 'falsey?'
(map not     [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ true  true  false false false false false false false false false   ]

(map truthy? [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ false false true  true  true  true  true  true  true  true  true    ]
notcellularmitosisPlayground
(in :yo 0)
# => 121
insogaiuPlayground
(os/sleep 1)    # => nil
(os/sleep 0.1)  # => nil
(os/sleep 0)    # => nil
os/sleepcellularmitosisPlayground
# Convert an array of k/v pairs into a table

(def kvp @[[:foo 1] [:bar 2]])
(table ;(mapcat identity kvp))  # => @{:foo 1 :bar 2}
mapcatMikeBellerPlayground
(describe @[:a :b]) # => "<array 0x55EC375CF440>"
describesogaiuPlayground
(string/trim " foo ")  # => "foo"
(string/trim "_!_foo_!_" "_!")  # => "foo"
string/trimcellularmitosisPlayground