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

(count even? [1 2 3 4 5])  # => 2

(count  (fn [x] (> x 3))  [1 2 3 4 5])  # => 2
(count         |(> $ 3)   [1 2 3 4 5])  # => 2

(count  (fn [x] (truthy? x))  [nil false true 42 :a "foo"])  # => 4
(count         |(truthy? $)   [nil false true 42 :a "foo"])  # => 4

(var f even?)
(count f [1 2 3 4 5])  # => 2
(set f odd?)
(count f [1 2 3 4 5])  # => 3

(map  (fn [f] (count f [1 2 3 4 5]))  [even? odd?])  # => @[2 3]
(map         |(count $ [1 2 3 4 5])   [even? odd?])  # => @[2 3]
countcellularmitosisPlayground
(table/clear @{:a 1 :b 2})
# => @{}
table/clearsogaiuPlayground
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
(->> (range 10) (map (fn [arg] (* arg arg))))

# => @[0 1 4 9 16 25 36 49 64 81]
mapGeo-7Playground
(defn to-double-digit-string [digit]
  (string/slice (string "0" digit) -3))

(defn get-date-time-string [time]
  (let [date (os/date time)
        year (get date :year)
        month (to-double-digit-string (get date :month))
        day (to-double-digit-string (get date :month-day))
        hours (to-double-digit-string (get date :hours))
        minutes (to-double-digit-string (get date :minutes))
        seconds (to-double-digit-string (get date :seconds))]
    (string year "-" month "-" day "__" hours ":" minutes ":" seconds)))

(defn get-current-date-time-string []
  (get-date-time-string (os/time)))


### USAGE

(get-current-date-time-string)
# => "2020-09-23__17:20:00"
os/dateharryvederciPlayground
(def buf-bytes 12)

(var new-buffer (buffer/new buf-bytes)) #--> @""

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
(math/exp 1)  # => 2.71828
math/e        # => 2.71828
math/expcellularmitosisPlayground
(map int?  [ nil   true  0    1    3.14   (math/trunc 3.14)  (band 1.1 2.2) ])
# =>      @[ false false true true false  true               true           ]
int?cellularmitosisPlayground
# channel that can be used for communication between os threads
(def chan (ev/thread-chan 10))

# one thread for sending a message
(ev/do-thread
  (def msg "hi")
  (print "sending: " msg)
  (ev/give chan msg))

# another thread for receiving a message
(ev/do-thread
  (print "received: " (ev/take chan)))

# expected output
#
# sending: hi
# received: hi
ev/do-threadsogaiuPlayground
(->> ["small" "cuddly" "creature"]
     (interpose "-")
     splice
     keyword)
# => :small-cuddly-creature
keywordsogaiuPlayground
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt")  # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt"))  # => @["hello" "world" ""]

(defn slurp-lines [path]
  (string/split "\n" (slurp path)))

(slurp-lines "foo.txt")  # => @["hello" "world" ""]

(string/join @["hello" "world" ""] "\n")  # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" ""] "\n"))
# The contents of foo.txt and foo2.txt are now identical.

(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))

(spit-lines "foo3.txt" (slurp-lines "foo.txt"))
# The contents of foo.txt and foo3.txt are now identical.
spitcellularmitosisPlayground
(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
((juxt + - * /) ;[1 2 3])
# => '(6 -4 6 0.166667)
juxtsogaiuPlayground
(get default-peg-grammar :a)
# => '(range "az" "AZ")
default-peg-grammarsogaiuPlayground
(defn recent-mods
  "List the files in the current directory which have changed within the last hour."
  []
  (filter
    (fn [fname]
      (<
        (- (os/time) 3600)
        (os/stat fname :modified)))
    (os/dir ".")))
os/statcellularmitosisPlayground