JanetDocsPlaygroundI'm feeling luckyGitHub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

(defn get-time-str []
  (let [{:hours h :minutes m :seconds s} (os/date)]
    (string h ":" m ":" s)))

(get-time-str) => "23:18:16"
os/datetupini07Playground
(->> "X"
     (string "a" "b")
     (string "c" "d")
     (string "e" "f"))  # => "efcdabX"
->>uvtcPlayground
(-> "X"
    (string "a" "b")
    (string "c" "d")
    (string "e" "f"))  # => "Xabcdef"
->uvtcPlayground
(defn eval-string
  ``Evaluates a string in the current environment. If more control over the
  environment is needed, use `run-context`.``
  [str]
  (var state (string str))
  (defn chunks [buf _]
    (def ret state)
    (set state nil)
    (when ret
      (buffer/push-string buf str)
      (buffer/push-string buf "\n")))
  (var returnval nil)
  (run-context {:chunks chunks
                :on-compile-error (fn compile-error [msg errf &]
                                    (error (string "compile error: " msg)))
                :on-parse-error (fn parse-error [p x]
                                  (error (string "parse error: " (:error p))))
                :fiber-flags :i
                :on-status (fn on-status [f val]
                             (if-not (= (fiber/status f) :dead)
                               (error val))
                             (set returnval val))
                :source :eval-string})
  returnval)
run-contexttionisPlayground
(find-index |(= $ "b") ["a" "b" "c" "d"]) #=> 1
find-indexAlecTroemelPlayground
(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
(pairs {:a 1 :b 2 :c 3})
# =>
@[(:c 3) (:a 1) (:b 2)]
pairsuvtcPlayground
(group-by odd? [1 2 3 5 6])
# =>
@{false @[2 6] true @[1 3 5]}
group-byuvtcPlayground
(def ds @[@{:a 1 :b 2}
          @{:a 8 :b 9}])

(defn thrice [x] (* x 3))

(update-in ds [0 :b] thrice)

# `ds` is now
@[@{:a 1 :b 6}
  @{:a 8 :b 9}]
update-inuvtcPlayground
(print math/int-min)
-9007199254740992
nil
math/int-minbtbytesPlayground
(file/open "iamfile.txt" :r)
(file/open "iamfile.txt" :w)
(file/open "iamfile.txt" :a)
file/openjgartePlayground
(seq [i :range [0 3]
      j :range [0 3]
      :let [c (string/format "%c" (+ 97 i))]
      :when (and (even? i) (even? j))]
  [(keyword c) j])
# => '@[(:a 0) (:a 2) (:c 0) (:c 2)]
seqsogaiuPlayground
(seq [i :range [0 3]
      j :range [0 3]
      :let [c (string/format "%c" (+ 97 i))]]
  [(keyword c) j])
# => '@[(:a 0) (:a 1) (:a 2) (:b 0) (:b 1) (:b 2) (:c 0) (:c 1) (:c 2)]
seqsogaiuPlayground
(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
(var a nil)

(var b nil)

(varfn a
  [x]
  (if (zero? x)
    0
    (b x)))
    
(varfn b
  [y]
  (if (pos? y)
    1
    (a y)))
    
(a 1)
# => 1

(b 0)
# => 0
varfnsogaiuPlayground
(cmp 0.0 0)
# => 0
cmpsogaiuPlayground
(cmp [1 2] [1 2 3])
# => -1
cmpsogaiuPlayground
(cmp 1.0 2)
# => -1
cmpsogaiuPlayground
(table/clear @{:a 1 :b 2})
# => @{}
table/clearsogaiuPlayground
(->> ["small" "cuddly" "creature"]
     (interpose "-")
     splice
     keyword)
# => :small-cuddly-creature
keywordsogaiuPlayground
(keyword :)
# => :
keywordsogaiuPlayground
(keyword)
# => :
keywordsogaiuPlayground
(keyword "")
# => :
keywordsogaiuPlayground
(let [x false]
  (var y 0)
  (unless x
    (++ y)
    (++ y))
  y)
# => 2
unlesssogaiuPlayground
(-> @{:a 0 :b 0}
    (update :a inc)
    (update :b inc))
# => @{:a 1 :b 1}
updatesogaiuPlayground
(update @{:a 1} :a inc)
# => @{:a 2}
updatesogaiuPlayground
(let [c (ev/chan 1)
      before (ev/count c)]
  (ev/give c :hi)
  [before (ev/count c)])
# => '(0 1)
ev/countsogaiuPlayground
(/ 0)
# => inf
/sogaiuPlayground
(/ 1 2 3)
# => 0.166667
/sogaiuPlayground
(/ 2)
# => 0.5
/sogaiuPlayground