JanetDocsSign in with Github

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

(def request {:params {:id 1}}) # => {:params {:id 1}}

(get-in request [:params :id]) # => 1

(get-in request [:params :name]) # => nil

(get-in request [:params :name] "N/A") # => "N/A"
get-ininchingforward
(def a-tuple-1 [:number 10])
(def a-tuple-2 [:string "Hello there!"])
(defn show [what]
  (match what
    [:number n] (printf "It's a number! %d" n)
    [:string s] (printf "Here's a string: %s" s)
    _ (printf "I dunno what this is: %q" what)))

(show a-tuple-1)
(show a-tuple-2)
(show [:array @[]])
matchroobie
(loop [x :range [1 10]
       :let [square (* x x)]
       :until (> square 9)
       :before (print "before")
       :after (print "after")
       :repeat 2]
  (print (string "square: " square)))

# before
# square: 1
# square: 1
# after
# before
# square: 4
# square: 4
# after
# before
# square: 9
# square: 9
# after

loopstaab
# Suppose you have a fiber that yields chunks of paginated api results:
(def api-results (fiber/new (fn [] (yield [1 2 3]) (yield [4 5 6]))))

# Using :iterate, the right side of the binding is evaluated each time the loop is run,
# which allows for running a side-effecting expression that may be different each time.
(loop [_ :iterate (fiber/can-resume? api-results)] (pp (resume api-results)))

# This example can be simplified using :generate
(loop [chunk :generate api-results] (pp chunk))
loopstaab
(peg/match ~{:main (capture (some :S))}
           "hello world")              # => @["hello"]
peg/matchsogaiu
(def h ["a" "b" :c]) # => ("a" "b" :c)

(find (fn [a] (= "a" a)) h) # => "a"
findfaywong
(odd? 2) # => false
(odd? nil) # throws an error
(odd? 1) # => true
(odd? "a") # => throws an error
odd?swlkr
(string/bytes "hello") # => (104 101 108 108 111)
string/bytesswlkr
# 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/formatroobie
(map |(+ $0 $1) [1 2 3] [4 5 6]) # @[5 7 9] - uses the fn shorthand
mapahungry
## Quadratic Formula

(defn qform
  "Use the quadratic formula to solve for x. Returns all real solutions."
  [a b c]
  (def det (- (* b b) (* 4 a c)))
  (def factor (/ 0.5 a))
  (cond
    (neg? det) []
    (zero? det) [(* factor (- b))]
    (let [root-det (math/sqrt det)]
        [(* factor (- (- b) root-det)) (* factor (+ (- b) root-det))])))

    (qform 1 4 3) # -> (-3 -1)
defnbakpakin
(map string/from-bytes "Hello, world!")  # => @["H" "e" "l" "l" "o" "," " " "w" "o" "r" "l" "d" "!"]
mapGrayJack
(os/getenv "TERM") # => "xterm-256color"
os/getenvsogaiu
janet:2:> (all true? [true true false])
false
janet:3:> (all even? [2 4 6 7])
false
janet:5:> (all even? [2 4 6])
true
allswlkr
(string/ascii-lower "HELLO") #=> "hello"
string/ascii-lowerswlkr
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/sliceroobie
(scan-number "123") # => 123
scan-numberswlkr
(string "hello " "world") # => "hello world"
stringswlkr
You can see janet datastructure values by typing:

(string/format "%q" {:a 1 :b 2}) # => {:a 1 :b 2} 
string/formatswlkr