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

(forever
  (print (os/time))
  (ev/sleep 1))

# => epoch clocks, prints epoch timestamp every second
foreverpepePlayground
(filter identity [1 true 2 3 nil 4 false])
# => @[1 true 2 3 4]
identityleobmPlayground
# Gathers all of the calls of "declare-" into a table from the project.janet file at path
(defn capture-declares [path] 
    (def *capture* @{})
    (defn append-capture [name & arg]
      (update *capture* name (fn [val] (array/push (or val @[]) ;arg))))
    (defn only-captures [form] 
      (when (string/has-prefix? "declare-" (string (form 0)))
        (append-capture (form 0) (slice form 1))
        form)
      nil)
    (dofile path :expander only-captures)
    *capture*)
dofileyumaikasPlayground
(find |(string/has-prefix? "a" $) ["be" "cat" "art" "apple"])

# => "art"

findveqqqPlayground
(def conn-chan (ev/thread-chan 1000))

(defn producer [no]
  (forever
    (ev/sleep 5)
    (print "Adding data from producer num:" no)
    (ev/give conn-chan (math/random))))

(defn consumer [no]
  (forever
    (ev/sleep 0.5)
    (def num (ev/take conn-chan))
    (print num ": Printing from consumer:" no)))

(defn main [& args]
  (ev/spawn-thread (producer 1))
  (ev/spawn-thread (consumer 1))
  (ev/spawn-thread (consumer 2))
  (ev/sleep 20)
  (print "exiting"))
ev/thread-chanGeo-7Playground
(os/shell "echo bar > /tmp/foo")
(with
  [file-handle
   (file/open "/tmp/foo")
   (fn [fd] (file/close fd))]
  (file/read file-handle :all))  # => @"bar\n"
withcellularmitosisPlayground
(let [x false]
  (var y 0)
  (unless x
    (++ y)
    (++ y))
  y)
# => 2
unlesssogaiuPlayground
(->> "X"
     (string "a" "b")
     (string "c" "d")
     (string "e" "f"))  # => "efcdabX"
->>uvtcPlayground
(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/executegoldenHairDafoPlayground
# Map over multiple structures -- Stops after the shortest is exhausted.
(map (fn [& args] ;args) [1] [1 2] [1 2 3])

# => @[(1 1 1)]
mapGrazfatherPlayground
(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground
(map  |($ {:a 7 :b 8} )   [  keys      values    kvs             pairs                 ])
# =>                     @[  @[:a :b]  @[7 8]    @[:a 7 :b 8]    @[(:a 7) (:b 8)]      ]

(map  |($ [4 5 6] )       [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1 2]  @[4 5 6]  @[0 4 1 5 2 6]  @[(0 4) (1 5) (2 6)]  ]

(map  |($ 'ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ :ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ "ab" )          [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]
kvscellularmitosisPlayground
(- 1)
# => -1
-sogaiuPlayground
(ev/spawn (os/sleep 1) (print "Hard work is done!"))

# prints "Hard work is done!" after one second
# this is the easiest way to put some forms on the event loop
# but do not forget REPL is blocking, for now, so run the example with `janet -e`
ev/spawnpepePlayground
(def chan (ev/chan))
(def f (ev/go (coro (ev/give-supervisor :msg "Hello")) nil chan))
(pp (ev/take chan)) # => (:msg "Hello")
ev/give-supervisorpepePlayground