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

# 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
(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
(string/split "," "bruce,scott,tiger,woods")
# => @["bruce" "scott" "tiger" "woods"]
string/splitsogaiuPlayground
(reduce     + 1 [2 3 4])  # -> 10
(accumulate + 1 [2 3 4])  # -> @[3 6 10]
accumulatecellularmitosisPlayground
(def ds @[@{:a 1 :b 2}
          @{:a 8 :b 9}])

(defn thrice [x] (* x 3))

(update-in ds [0 :b] thrice)

# `ds` is now
@[@{:a 1 :b 6}
  @{:a 8 :b 9}]
update-inuvtcPlayground
(keyword)
# => :
keywordsogaiuPlayground
(defn fizzbuzz [n]
  (cond 
    (and
      (= 0 (% n 3))
      (= 0 (% n 5))) "fizzbuzz"
    (= 0 (% n 3)) "fizz"
    (= 0 (% n 5)) "buzz"
    :else n))

(fizzbuzz 1)   # 1
(fizzbuzz 3)   # "fizz"
(fizzbuzz 5)   # "buzz"
(fizzbuzz 15)  # "fizzbuzz"
%cellularmitosisPlayground
Example usage


janet:1:> (def buf @"ABCDE")
@"ABCDE"
janet:2:> (buffer/slice 0)
error: bad slot #0, expected string|symbol|keyword|buffer, got 0
  in buffer/slice
  in _thunk [repl] (tailcall) on line 2, column 1
janet:3:> (buffer/slice buf 0)
@"ABCDE"
janet:4:> (buffer/slice buf 0 1)
@"A"
janet:5:> (buffer/slice buf 0 -1)
@"ABCDE"
janet:6:> (buffer/slice buf 1 3)
@"BC"
buffer/sliceroobiePlayground
(parse "[:a :b :c]") # => [:a :b :c]
parsesogaiuPlayground
(def conn-chan (ev/thread-chan 1000))

(defn producer [no]
  (forever
    (ev/sleep 5)
    (print "Adding data from producer num:" no)
    (ev/give conn-chan (math/random))))

(defn consumer [no]
  (forever
    (ev/sleep 0.5)
    (def num (ev/take conn-chan))
    (print num ": Printing from consumer:" no)))

(defn main [& args]
  (ev/spawn-thread (producer 1))
  (ev/spawn-thread (consumer 1))
  (ev/spawn-thread (consumer 2))
  (ev/sleep 20)
  (print "exiting"))
ev/thread-chanGeo-7Playground
(let [c0 (ev/chan)
      c1 (ev/chan 1)]
  [(ev/capacity c0) (ev/capacity c1)])
# => '(0 1)
ev/capacitysogaiuPlayground
(/ 0)
# => inf
/sogaiuPlayground
# There is a list of formatters here: https://janet-lang.org/capi/writing-c-functions.html


(string/format "With terminal colors: %M" [:array {:key-in "struct"}]) # => "With terminal colors: (\e[33m:array\e[0m {\e[33m:key-in\e[0m \e[35m\"struct\"\e[0m})"
string/formatroobiePlayground
(map tuple ["a" "b" "c" "d"] [1 2 3 4] "abcd")

# => @[("a" 1 97) ("b" 2 98) ("c" 3 99) ("d" 4 100)]
mapfelixrPlayground
(empty? "")
# => true
empty?sogaiuPlayground