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

(invert "yo")
# => @{111 1 121 0}
invertsogaiuPlayground
(band 7 11)  # => 3

#     0111  (7)
# and 1011  (11)
# --------
#     0011  (3)
bandcellularmitosisPlayground
(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
# can execute query from a file like:
(db/query (slurp "db/sql/random.sql"))

# In context:
(defn show [request]
  (when-let [[name] (request :wildcard)
             name (uri/unescape name) # escaping is important for = and especially %
             name (string/replace "_q" "?" name)
             binding (first (db/query (slurp "db/sql/search.sql") [name]))]

    [:vstack {:spacing "m"}
     (binding-header binding)
     (examples/index (merge request {:binding binding}))]))

# From https://codeberg.org/veqq/janetdocs/src/commit/ac1dc9e3e82f17e8e9ac047297b00803b68034d0/routes/examples.janet#L217
joy/db/queryveqqqPlayground
# from https://janet.guide/control-flow/
(defn calculate [expr]
  (match expr
    [:add x y] (+ x y)
    [:subtract x y] (- x y)
    [:multiply x y] (* x y)
    ([:divide x y] (= y 0)) (error "division by zero!")
    [:divide x y] (/ x y)))

(calculate [:subtract 5 10])

# Note, it checks prefixes, so you must order things this way:
(match [1 2]
  [x y z] "three elements"
  [x y] "two elements"
  [x] "one element"
  [] "no elements")
matchveqqqPlayground
# see https://janet-lang.org/docs/abstract_machine.html

(def plus10
 (asm 
   '{
     :arity 1
     :bytecode @[(ldi 1 10)    # $1 = 10
                 (add 2 0 1)   # $2 = $0 + $1
                 (ret 2)]}))   # return $2

(plus10 1) # -> 11
asmfelixrPlayground
# absolute value of negatives, ignore positives
(keep |(if (< $ 0) (- $) nil) [-1 2 -3 -4 -5 6]) # -> @[1 3 4 5]

# halves each even number, ignores odds
(keep |(when (even? $) (/ $ 2)) [1 2 8 7 -2])    # -> @[1 4 -1]
keepTechcablePlayground
(os/shell "touch foo")
(os/stat "foo" :modified)  # => 1593836002
(os/touch "foo")
(os/stat "foo" :modified)  # => 1593836013
os/touchcellularmitosisPlayground
(dyn :pretty-format)
# => "%.20Q"
dynsogaiuPlayground
(do
  (def before (dyn :expression))
  (setdyn :expression :smile)
  [before (dyn :expression)])
# => '(nil :smile)
setdynsogaiuPlayground
(var a 0)
(assert (pos? a) "A is definitely not positive")
# error: A is definitely not positive
#  in assert [boot.janet] on line 149, column 11
#  in _thunk [repl] (tailcall) on line 3, column 1
assertpepePlayground
(invert {:a 1 :b 2 :c 3})
# => @{3 :c 1 :a 2 :b}
invertsogaiuPlayground
# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(forv i 0 10
 (print "hello"))

# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => nil
forvjgartePlayground
(map boolean? [true false nil   0     1    ])
# =>         @[true true  false false false]
boolean?cellularmitosisPlayground