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

(partition-by even? [1 1 2 3 5 8 13 21])
# => @[@[1 1] @[2] @[3 5] @[8] @[13 21]]
partition-bysogaiuPlayground
(filter (partial string/has-prefix? "z") (all-bindings))  # => @[zero? zipcoll]
all-bindingscellularmitosisPlayground
(os/date)
# => {:month 6 :dst false :year-day 185 :seconds 38 :minutes 44 :week-day 6 :year 2020 :hours 4 :month-day 3}
os/datecellularmitosisPlayground
(def a @[1 2 3])
(array/fill a 17)   # => @[17 17 17]
(array/fill a "n")  # => @["b" "b" "b"]
(array/fill a)      # => @[nil nil nil]
a                   # => @[nil nil nil]
array/fillcellularmitosisPlayground
(string/bytes "hello") # => (104 101 108 108 111)
string/bytesswlkrPlayground
(drop-until |(> $ 8)
            [1 1 2 3 4 5 8 13 21])
# => '(13 21)
drop-untilsogaiuPlayground
# 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
(dec 42)  # => 41
(map dec [1 2 3])  # => @[0 1 2]
deccellularmitosisPlayground
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepePlayground
(def buf-bytes 12)

(var new-buffer (buffer/new buf-bytes)) #--> @""

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
# in a terminal:
# $ while true; do date | nc 0.0.0.0 1234 -w 1; sleep 1; done

# in a janet repl:
(net/server "0.0.0.0" 1234
  (fn [conn]
    (prin (net/read conn 4096))
    (net/close conn)))
# note: output doesn't actually start until you (quit) your repl's fiber:
(quit)
net/readcellularmitosisPlayground
(os/lstat "t.janet")
# @{:size 249 :permissions "rw-r--r--" :nlink 1 :blocks 8 :dev 16777221 :accessed 1606236760 :modified 1606236759 :uid 501 :mode :file :blocksize 4096 :changed 1606236759 :inode 14801850 :rdev 0 :int-permissions 420 :gid 501}

(get (os/lstat "t.janet") :size)  # 249
os/lstatbtbytesPlayground
# 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
(string ;(range 12)) # => "01234567891011"
stringjgartePlayground
(buffer "dude, " "where's " "my " "car?") # => @"dude, where's my car?"
bufferjgartePlayground