JanetDocsI'm feeling luckyGithub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

# 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
(os/execute
  @("/usr/bin/bash" "-c" "set")
  :e
  @{"SOME" "value"
    "OTHER" "one"})
# => 0

# execute bash and prints environment variables
# which contains SOME=value and Other=one
os/executegoldenHairDafo
(os/execute
  @("python" "-c" "print('Hello Janet'"))
  :p) 
# => 0

# execute python -c "print('Hello Janet') while
# searching path on the current path
os/executegoldenHairDafo
(defn output [x]
  (case x
    :a "a"
    "b"))

(output :a) # => "a"
(output "anything else") # => "b"
caseswlkr
(first [4 5 6])  # => 4
firstcellularmitosis
(flatten [1 [2 3 [4]] 5])  # => @[1 2 3 4 5]
flattencellularmitosis
> (eprin "there is a boo-boo on line " 33 "\n")
there is a boo-boo on line 33
nil
eprincellularmitosis
(update @[3 4 5] 1 dec)  # => @[3 3 5]
(update (update @[3 4 5] 1 dec) 2 inc)  # => @[3 3 6]
updatecellularmitosis
(get @"A"                      0)  # => 65 (0b01000001)
(get (buffer/bit-clear @"A" 0) 0)  # => 64 (0b01000000)
(get (buffer/bit-clear @"A" 6) 0)  # =>  1 (0b00000001)
buffer/bit-clearcellularmitosis
(defn square [x] (* x x))
(defn square-then-dec [x] ((comp dec square) x))
(defn dec-then-square [x] ((comp square dec) x))
(square-then-dec 3)  # => 8
(dec-then-square 3)  # => 4
compcellularmitosis
(any? [false false nil]) => nil
(any? [false false nil 1]) => 1
(any? [false false nil true]) => true
any?swlkr
(buffer/bit (buffer/new-filled 1 (chr "0")) 4)
# => true
buffer/bitsogaiu
(buffer/bit (buffer/new-filled 1 48) 4)
# => true
buffer/bitsogaiu
(buffer/bit @"0" 4)
# => true
buffer/bitsogaiu
(buffer/bit @"1" 0)
# => true
buffer/bitsogaiu