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

(sort (keys default-peg-grammar))
# => @[:A :D :H :S :W :a :a* :a+ :d :d* :d+ :h :h* :h+ :s :s* :s+ :w :w* :w+]
default-peg-grammarsogaiuPlayground
# Demonstrate file/flush -- tail %flushtest.csv to see new
# entries appended as the program runs. Otherwise, entries
# wouldn't be visible until file was closed. @oofoe

(def fp (file/open "flushtest.csv" :wb))

(file/write fp "Timestamp,Fiducial\n")
(for i 0 5
  (print "-- Writing " i)
  (file/flush (file/write fp (string (os/time) "," i "\n")))
  (os/sleep (* 5 (math/random))))

(file/close fp)
file/flushoofoePlayground
(not=        [1 1]   [1 1])  # => false
(not=        [1 1]   [2 3])  # => true
(not=        [1 1]  @[1 1])  # => true
(not=        [1 1]  @[2 3])  # => true
(not=       @[1 1]  @[1 1])  # => true
(not=       @[1 1]  @[2 3])  # => true

(deep-not=   [1 1]   [1 1])  # => nil
(deep-not=   [1 1]   [2 3])  # => true
(deep-not=   [1 1]  @[1 1])  # => true
(deep-not=   [1 1]  @[2 3])  # => true
(deep-not=  @[1 1]  @[1 1])  # => nil
(deep-not=  @[1 1]  @[2 3])  # => true
deep-not=cellularmitosisPlayground
(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
(table ;(kvs (struct :a 1 :b 2)))
# => @{:a 1 :b 2}
tablesogaiuPlayground
(def a @[[42 "c"] [68 "b"] [31 "d"] [27 "a"]])
(sort a (fn [a b]
          (< (a 1) (b 1))))
(pp a) # @[(27 "a") (68 "b") (42 "c") (31 "d")]
sortuvtcPlayground
(doc-of file/read) # => file/read docs are shown

(def tmpfile (file/temp))

(doc-of (tmpfile :read)) # => also shows the file/read docs. 
# Note that we didn't have to use the file/read symbol here to get docs.
doc-ofyumaikasPlayground
(defn square [x] (* x x))
(defn square-then-dec [x] ((comp dec square) x))
(defn dec-then-square [x] ((comp square dec) x))
(square-then-dec 3)  # => 8
(dec-then-square 3)  # => 4
compcellularmitosisPlayground
# Contrived examples returning the variadic arguments passed in.
(defn example-function [& args] args)
(defmacro example-macro [& args] ~(tuple ,;args))

(macex '(example-macro 1 2 3))

(assert (= (example-function 1 2 3)
           (example-macro 1 2 3)))

(def args [1 2 3])

# `apply` is for functions, but there's always `eval`.
(assert (= (apply example-function args)
           (eval ~(example-macro ,;args))))

# Same return for both.
# => (1 2 3)
apply4kbytePlayground
# removes a file in the current directory
(os/rm "hello.txt")
os/rmswlkrPlayground
(ev/call print 10)
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
# => prints 10
ev/callpepePlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(reduce string "ha" ["ha" "ha" "ha" "ha"]) # => "hahahahaha"

(accumulate string "ha" ["ha" "ha" "ha" "ha"]) # => @["haha" "hahaha" "hahahaha" "hahahahaha"]
accumulatejgartePlayground
(defn capitalize [str]
  (string (string/ascii-upper (string/slice str 0 1)) (string/slice str 1)))
string/ascii-upperveqqqPlayground
(any? "")
# => nil

(any? "Hello")
# => 72

(any? @"Hello")
# => 72

(any? @[])
# => nil

(any? [])
# => nil

(any? {})
# => nil

(any? @{})
# => nil

(any? @[1 2 3 4])
# => 1 

(any? @{:a 2 :b 3})
# => 2
any?leobmPlayground