JanetDocsI'm feeling luckyGithub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

(reduce (fn [s1 s2]
          (string "[" s1 "+" s2 "]"))
        "x"
        ["a" "b" "c"])

#=> "[[[x+a]+b]+c]"
reduceuvtc
(filter (fn [x] (> x 2)) [1 2 3 4 5])  # @[3 4 5]
filterbtbytes
(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/lstatbtbytes
(do
  (var a 88)
  (+= a 12))  # 100
+=btbytes
(print (doc-format "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." 30))

    Lorem ipsum dolor
    sit amet, consectetur
    adipiscing elit, sed
    do eiusmod tempor
    incididunt ut labore
    et dolore magna
    aliqua. Ut enim ad
    minim veniam, quis
    nostrud exercitation
    ullamco laboris nisi
    ut aliquip ex ea
    commodo consequat.
doc-formatbtbytes
(mod 13 5)  # 3
modbtbytes
(math/random)  # 0.487181
math/randombtbytes
(sorted [1 -2 2 3 9 -10])  # @[-10 -2 1 2 3 9]
sortedbtbytes
(take 2 [1 -2 2 3 9 -10])  # (1 -2)
takebtbytes
(math/atan 1)    # 0.785398
(math/atan 0.5)  # 0.463648
math/atanbtbytes
# catch and propagate an error with fiber

(try
  (+ 1 nil)
  ([err fib]
   (propagate err fib)))
tryswlkr
(compare> 1 0) # => true
(compare> 10 9.9) # => true
(compare> :a :b) # => false
(compare> :b :a) # => true
compare>swlkr
(defn read-from-file [file-path]
  (let [f (file/open file-path :r)
        content  (file/read f :all)]
    (file/close f)
    content))

### USAGE 

(read-from-file "/path/to/file-read-example.janet")
# => @"(defn read-from-file [file-path]\n  (let [f (file/open file-path :r)\n        content  (file/read f :all)]\n    (file/close f)\n    content))\n"
file/readharryvederci
(defn to-double-digit-string [digit]
  (string/slice (string "0" digit) -3))

(defn get-date-time-string [time]
  (let [date (os/date time)
        year (get date :year)
        month (to-double-digit-string (get date :month))
        day (to-double-digit-string (get date :month-day))
        hours (to-double-digit-string (get date :hours))
        minutes (to-double-digit-string (get date :minutes))
        seconds (to-double-digit-string (get date :seconds))]
    (string year "-" month "-" day "__" hours ":" minutes ":" seconds)))

(defn get-current-date-time-string []
  (get-date-time-string (os/time)))


### USAGE

(get-current-date-time-string)
# => "2020-09-23__17:20:00"
os/dateharryvederci
# opens a file named filename for writing, and writes Hello World!
(def f (file/open "filename" :w))
(file/write f "Hello World!")
(file/flush f)
(file/close f)
file/writeterminalcommand
(comment this is a
         multiline line comment.
         It won't do anything)
commentoz123
(defn ddup [ds ks val]
  (update-in ds ks
    (fn [x]
      (if (= nil x)
        @[val]
        (array/push x val)))))

(var a @{})
(ddup a [:a] 1)
(ddup a [:a] 2)
(ddup a [:a] 3)
# @{:a @[1 2 3]}

update-insbjaver
(update-in @{:a @{:b 1}} [:a :b] (fn [x] (+ 1 x)))
# @{:a @{:b 2}}
update-insbjaver
(update-in @{:a 1} [:a] (fn [x] (+ 1 x)))
# @{:a 2}
update-insbjaver
(eval-string "(+ 1 2 3 4)") # -> 10
(eval-string ")") # -> parse error
(eval-string "(bloop)") # -> compile error
(eval-string "(+ nil nil)") # -> runtime error
eval-stringswlkr
(if-let [x true 
         y (not (not x))]
  :a
  :b)
# => :a
if-letsogaiu
(if-let [x true 
         y (not x)]
  :a
  :b)
# => :b
if-letsogaiu
(partition 2 [:a 1 :b 2 :c 3])
# => @[(:a 1) (:b 2) (:c 3)]
partitionsogaiu
(interleave [:a :b :c] 
            [1 2 3] 
            ["x" "y" "z"])
# => @[:a 1 "x" :b 2 "y" :c 3 "z"]
interleavesogaiu
(invert {:a 1 :b 2 :c 3})
# => @{3 :c 1 :a 2 :b}
invertsogaiu
(eachk [k v] {:a "a val" :b "b val" :c "c val"} (print k " - " v))
# prints c - c val
# prints a - a val
# prints b - b val
eachppepe
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepe
(eachk k {:a "a val" :b "b val" :c "c val"} (print k))
# prints c
# prints a
# prints b
eachkpepe
(each item [1 2 3 4] (print item))
# prints 1
# prints 2
# prints 3
# prints 4
# => nil
eachpepe
(let [len 8
      rand-string (string/join (map |(string/format "%02x" $)
                                    (os/cryptorand len)))]
  (= (length rand-string) (* 2 len))) # => true
os/cryptorandsogaiu