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

(seq [i :range [0 3]
      j :range [0 3]]
  [(keyword (string/format "%c" (+ 97 i)))
   j])
# => '@[(:a 0) (:a 1) (:a 2) (:b 0) (:b 1) (:b 2) (:c 0) (:c 1) (:c 2)]
seqsogaiuPlayground
# 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))
loopstaabPlayground
(find |(string/has-prefix? "a" $) ["be" "cat" "art" "apple"])

# => "art"

findveqqqPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->cellularmitosisPlayground
(defn slurp-lines [path]
  (string/split "\n" (slurp path)))
string/splitcellularmitosisPlayground
(def cc (ev/thread-chan 99))  # Sets up a message channel.
(def ww (filewatch/new cc))   # Creates the watcher.
(filewatch/add ww "ftest.janet" :all)     # Watch for all changes.
(filewatch/add ww "fadd.janet"  :modify)  # Only care if modified.
(filewatch/listen ww)         # Start filewatcher listening for changes.

# Note that we may still get events for files that have been removed
# so we have to make sure we don't remove them twice.

(var careless "Have we already removed %fadd.janet?" false)
(forever
 (let (item (ev/take cc))
   (pp (ev/take cc))
   (when (and (not careless) (= "fadd.janet" (item :wd-path)))
     (print "I don't care about this file anymore.")
     (filewatch/remove ww (item :wd-path))
     (set careless true)
     )))
filewatch/removeoofoePlayground
(do
  (def coll @[])
  (forv i 0 9
    (array/push coll i)
    (+= i 2))
  coll)
# => @[0 3 6]
forvsogaiuPlayground
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
# wrap short-fn / | 
(->> 10
     (|(/ $ 2)))
# =>
5

# also wrap fn
(->> 10
     ((fn [n] (/ n 2))))
# =>
5
->>sogaiuPlayground
(reduce string "ha" ["ha" "ha" "ha" "ha"]) # => "hahahahaha"

(accumulate string "ha" ["ha" "ha" "ha" "ha"]) # => @["haha" "hahaha" "hahahaha" "hahahahaha"]
accumulatejgartePlayground
(->
  {:a [1 2 3] :b [4 5 6]}
  (get :a)
  (sum)
  (string " is the result"))
# -> "6 is the result"

# same as:
(string (sum (get {:a [1 2 3] :b [4 5 6]} :a))" is the result")
->felixrPlayground
(def a @[23 42])
(array/clear a)
(pp a)
# => prints @[]
array/clearpepePlayground
(string/bytes "foo")  # => (102 111 111)
(string/from-bytes 102 111 111)  # => "foo"
(string/from-bytes (splice (string/bytes "foo")))  # => "foo"

(map (fn [x] x)        "foo")  # => @[102 111 111]
(map string/from-bytes "foo")  # => @["f" "o" "o"]

(defn string/explode [s] (map string/from-bytes s))
(string/explode "foo")  # => @["f" "o" "o"]
string/from-bytescellularmitosisPlayground
(table/clear @{:a 1 :b 2})
# => @{}
table/clearsogaiuPlayground
# Convert an array of k/v pairs into a table

(def kvp @[[:foo 1] [:bar 2]])
(table ;(mapcat identity kvp))  # => @{:foo 1 :bar 2}
mapcatMikeBellerPlayground