Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
# 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 (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 (string/split "," "bruce,scott,tiger,woods" )
# => @["bruce" "scott" "tiger" "woods"] (reduce + 1 [2 3 4 ]) # -> 10
(accumulate + 1 [2 3 4 ]) # -> @[3 6 10] (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 }](keyword )
# => : (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" 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"
(parse "[:a :b :c]" ) # => [:a :b :c] (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" ))(let [c0 (ev/chan )
c1 (ev/chan 1 )]
[(ev/capacity c0 ) (ev/capacity c1 )])
# => '(0 1) (/ 0 )
# => inf
# 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})"
(map tuple ["a" "b" "c" "d" ] [1 2 3 4 ] "abcd" )
# => @[("a" 1 97) ("b" 2 98) ("c" 3 99) ("d" 4 100)] (empty? "" )
# => true