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

(min 1 2 3)             # => 1
(min (splice [1 2 3]))  # => 1
(min ;[1 2 3])          # => 1
(apply min [1 2 3])     # => 1
mincellularmitosisPlayground
(eachk k {:a "a val" :b "b val" :c "c val"} (print k))
# prints c
# prints a
# prints b
eachkpepePlayground
(defn- parse-timestamp
  ``Parse a date or datetime string into a unix epoch int``
  [s]
  (if (string/find " " s)
    # datetime: "yyyy-MM-dd HH:mm"
    (let [[date-part time-part] (string/split " " s)
          [y m d]               (string/split "-" date-part)
          [hh mm]               (string/split ":" time-part)]
      (os/mktime {:year         (scan-number y)
                  :month        (scan-number m)
                  :month-day    (scan-number d)
                  :hours        (scan-number hh)
                  :minutes      (scan-number mm)
                  :seconds      0}))
    # date: "yyyy-MM-dd"
    (let [[y m d] (string/split "-" s)]
      (os/mktime {:year      (scan-number y)
                  :month     (scan-number m)
                  :month-day (scan-number d)
                  :hours 0 :minutes 0 :seconds 0}))))
os/mktimeveqqqPlayground
(group-by 
  (fn [i] (i :label)) 
  [{:label 'A :value 4} {:label 'A :value 3} {:label 'B :value 5}])
# @{A @[{:label A :value 4} {:label A :value 3}] B @[{:label B :value 5}]}
group-byaquilaxPlayground
(def [pipe-r pipe-w] (os/pipe))
(:write pipe-w "hello")
(:read pipe-r 5)
# => @"hello"

# send the string "hello" into the writable stream
# part and read it back from the readable one
os/pipeYohananDiamondPlayground
(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
(dyn :pretty-format)
# => "%.20Q"
dynsogaiuPlayground
# Run this in a file.
# Notice how each thread gets its own copy of the environment,
# including the global 'counter' variable.

(var counter 0)
(defn start-thread [name sleep]
  (def chan (ev/thread-chan))
  (ev/spawn-thread
    (repeat 10
      (ev/sleep sleep)
      (++ counter)
      (print name " counter is " counter))
    (print name " has finished")
    (ev/give chan "done"))
  chan)

# Spawn two threads that increment counter
(def chan-a (start-thread "Slow thread" 0.8))
(def chan-b (start-thread "Fast thread" 0.45))

# Wait for both threads to finish
(ev/take chan-a)
(ev/take chan-b)
(print "Global counter is still " counter)
ev/spawn-threadfuxoftPlayground
(next  [4 5 6]    )  # => 0
(next  [4 5 6]  0 )  # => 1
(next  [4 5 6]  1 )  # => 2
(next  [4 5 6]  2 )  # => nil

# note that dictionary keys are not necessarily in the same order
# as the corresponding literal.
(next  {:a 5 :b 6 :c 7}     )  # => :a
(next  {:a 5 :b 6 :c 7}  :a )  # => :c
(next  {:a 5 :b 6 :c 7}  :c )  # => :b
(next  {:a 5 :b 6 :c 7}  :b )  # => nil
nextcellularmitosisPlayground
[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
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
(distinct [1 1 2 3])  # => @[1 2 3]

(distinct "hello")  # => @[104 101 108 111]
(string/from-bytes (splice (distinct "hello")))  # => "helo"
distinctcellularmitosisPlayground
(- 1)
# => -1
-sogaiuPlayground
# Map over multiple structures -- Stops after the shortest is exhausted.
(map (fn [& args] ;args) [1] [1 2] [1 2 3])

# => @[(1 1 1)]
mapGrazfatherPlayground
(invert [:ant :bee :elephant :fox :penguin])
# => @{:bee 1 :fox 3 :elephant 2 :ant 0 :penguin 4}
invertsogaiuPlayground