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

(defn f1 [s] (string "-" s "-"))
(defn f2 [s] (string "=" s "="))
(defn f3 [s] (string "#" s "#"))

(def f (juxt f1 f2 f3))

(f "x") # => ("-x-" "=x=" "#x#")
juxtuvtcPlayground
# slice with strings
(slice "Hello" 1)        # => "ello"
(slice "Playing" 0 -4)   # => "Play"
(slice "Playing" -4)     # => "ing"

# slice with keyword
(slice :hello 1)         # => "ello"
sliceleobmPlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
returnsogaiuPlayground
# Expanding the example from https://janetdocs.org/core-api/stdin
# We use argparse to provide automatically add flags
# --help comes for free, try it!

(import spork/argparse)

(defn main [&]
  (let [opts (argparse/argparse "Sum numbers from files, stdin or data."
              "multiply" {:kind :option :short "m" :default "1" :map scan-number :help "Multiply the result"}
              "verbose"  {:kind :flag :help "Print the input first"}
              :default   {:kind :accumulate})

        _ (unless opts (os/exit 0)) # lest it crash on help etc.
        args (or (opts :default) []) # give empty? empty array of :default is nil
        data (cond (empty? args) (file/read stdin :all)
                   (os/stat (first args)) (slurp (first args))
                   (string/join args " "))
        total (* (opts "multiply") (sum (flatten (parse-all data))))]

    (if (opts "verbose") (print "Data: " data))
    (print total)))
argparse/argparseveqqqPlayground
(math/pow 10 3)   # => 1000
(math/cbrt 1000)  # => 10
math/cbrtcellularmitosisPlayground
# nested iteration
(loop [a :in [100 200 300]
       b :in [1 2 3]]
   (print (+ a b)))

# 101
# 102
# 103
# 201
# 202
# 203
# 301
# 302
# 303
looperichaneyPlayground
(map length  [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  ])
# =>        @[  1   1   1    1     1      1       1        ]
lengthcellularmitosisPlayground
(defmacro inner [x] ~(do [,x (* ,x ,x)]))
(defmacro outer [n] (map |~(inner ,$0) (range n)))

# Hints:
#
# * Quote the argument.
# * Because it's quoted, the argument can have undefined symbols.
# * Compare the result of `macex` with `macex1`.
# * If needed, print the result with `pp`.
#
(macex '(outer 10))
macex4kbytePlayground
(def kvpairs [[:x 1] [:y 2]])

(table ;(flatten kvpairs)) => @{:x 1 :y 2}
tableyumaikasPlayground
(math/hypot 1 1)  # => 1.41421
(math/sqrt 2)     # => 1.41421
math/hypotcellularmitosisPlayground
(def f (generate [i :range [0 5]] (+ i i)))

(print (fiber/status f))
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (fiber/status f)) # -> :pending
(print (resume f)) 
(print (fiber/status f)) # -> :dead


# :new
# 0
# 2
# 4
# 6
# 8
# :pending
#
# :dead
generateleobmPlayground
(net/address "0.0.0.0" 80) # => <core/socket-address 0x55CABA438E90>

(net/address "0.0.0.0" 8989) # => <core/socket-address 0x55CABA439980>

net/addressjgartePlayground
(def new-bytes 512)

(var new-buffer (buffer/new new-bytes))

# --> @""
buffer/newMorganPetersonPlayground
(defn knorkulate [a b]
  (tracev (+ a (* b b ))))
# <function knorkulate>

(knorkulate 2 34)
# trace on line 2, column 1: (+ a (* b b)) is 1158
# 1158
tracevkamisoriPlayground
(not=        [1 1]   [1 1])  # => false
(not=        [1 1]   [2 3])  # => true
(not=        [1 1]  @[1 1])  # => true
(not=        [1 1]  @[2 3])  # => true
(not=       @[1 1]  @[1 1])  # => true
(not=       @[1 1]  @[2 3])  # => true

(deep-not=   [1 1]   [1 1])  # => nil
(deep-not=   [1 1]   [2 3])  # => true
(deep-not=   [1 1]  @[1 1])  # => true
(deep-not=   [1 1]  @[2 3])  # => true
(deep-not=  @[1 1]  @[1 1])  # => nil
(deep-not=  @[1 1]  @[2 3])  # => true
not=cellularmitosisPlayground