JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!

Loading...

Random Examples

# many terminals will truncate long data, use this if you need to see/copypaste it:
(defn full-print `Print a large DS without truncation` [ds] (print (string/format "%m" ds)))
ppveqqqPlayground
(string/find-all "duck" "duck duck duck goose!") # => @[0 5 10]
string/find-allsogaiuPlayground
(scan-number "111" 2)
# => 7
scan-numbersogaiuPlayground
(def a @[[42 "c"] [68 "b"] [31 "d"] [27 "a"]])
(sort a (fn [a b]
          (< (a 1) (b 1))))
(pp a) # @[(27 "a") (68 "b") (42 "c") (31 "d")]
sortuvtcPlayground
(let [sweet "what does mine say?"]
 (let [dude sweet]
  (let [what-does-mine-say? dude]
   what-does-mine-say?))) # => "what does mine say?"
letjgartePlayground
# 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
(string/join @["alice" "bob" "eve"] "\t")
# => "alice\tbob\teve"
string/joinsogaiuPlayground
(var a 1)
a        # => 1
(inc a)  # => 2
a        # => 1
(++ a)   # => 2
a        # => 2
++cellularmitosisPlayground
(find |(= 2 $) {:a 1 :b 2 :c 3})
# => 2

(get @"Hello" 1)
# => 101

(find |(= 101 $)  @"Hello")
# => 101

(find |(> $ 3)  {:a 1 :b 2 :c 3})
# => nil
findleobmPlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(varglobal "smile" false)
# => nil

smile
# => false

(set smile true)
# => smile

smile
# => true

(dyn 'smile)
# => @{:ref @[true]}
varglobalsogaiuPlayground
(def buf-bytes 12)

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

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
# note that os/touch does not create a file if it does not yet exist.
(os/touch "foo")
error: No such file or directory
  in os/touch
  in _thunk [repl] (tailcall) on line 2, column 1
os/touchcellularmitosisPlayground
(range 10000) # @[0 1 2 ... 9997 9998 9999]

# By default, pp truncates. You can change its behavior:
(setdyn :pretty-format "%j") # jdn format
(setdyn :pretty-format "%m") # full printing without truncation
# For a list of all print formats: https://janet-lang.org/capi/writing-c-functions.html#Panicking

(range 10000) # Prints all of them according to your dyn
ppveqqqPlayground
(when-with [f (file/open "nofile.exists")] 
  (print "Realy?"))
# it just returns nil 
when-withpepePlayground
# use (dyn :args) to get the value of dynamic binding *args*
(let [args (dyn :args)]
  (if (= "-h" (get args 1))
    (print "Usage: janet args.janet [-h] ARGS..")
    (printf "command line arguments:\n %q" args)))
*args*AndriamanitraPlayground
(tabseq [i :in (range 97 100)]
  i (string/from-bytes i))
# =>
@{97 "a"
  98 "b"
  99 "c"}
tabseqsogaiuPlayground
(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))
string/joincellularmitosisPlayground
(def b @"")
(def v (* 1000 (math/random)))
# => 487.181 your number can differ
(xprinf b "Value reached level %f" v)
# => nil
b
# => @"Value reached level 487.181188"
xprinfpepePlayground
[1 2 3]               # => (1 2 3)
(tuple/type [1 2 3])  # => :parens

(tuple/brackets 1 2 3)               # => [1 2 3]
(tuple/type (tuple/brackets 1 2 3))  # => :brackets
tuple/typecellularmitosisPlayground