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

(defn dataframe 
  ``Create an empty dataframe with the given columns``
  [& columns]
  (tabseq [c :in columns] c @[]))
tabseqveqqqPlayground
(var a false)
(defer (set a 42) 
  (set a true)
  (error "Oh no!"))
# error: Oh no!

(pp a) # => prints 42
deferpepePlayground
(update @{:data @[:cherry :orange]} :data (comp sorted |(array/push $ :apple)))
# => {:data @[:apple :cherry :orange]}


# note that all data structures must be mutable with @
(def c @{:size :small :roast :dark :add-ins @[:soy :almond :soy]})
(defn add! [coffee extra]
  (update coffee :extras (comp sort |(array/push $ extra))))
(add! c :chocolate)
updateveqqqPlayground
# use (dyn :args) to get the value of dynamic binding *args*
(let [args (dyn :args)]
  (if (= "-h" (get args 1))
    (print "Usage: janet args.janet [-h] ARGS..")
    (printf "command line arguments:\n %q" args)))
*args*AndriamanitraPlayground
(defmacro timeit [& body]
    # generate unique symbols to use in the macro so they can't conflict with anything used in `body`
    (with-syms [$t0 $t1]
        ~(do
            (def $t0 (os/clock :monotonic :double))
            (do ,;body)
            (def $t1 (os/clock :monotonic :double))
            (- $t1 $t0))))

(def time-taken (timeit (os/sleep 0.5)))
(printf "Took %.3f seconds" time-taken)
with-symsAndriamanitraPlayground
(string/slice "hello")    # => "hello"

(string/slice "hello" 0)  # => "hello"
(string/slice "hello" 1)  # => "ello"
(string/slice "hello" 4)  # => "o"
(string/slice "hello" 5)  # => ""

(string/slice "hello" -1)  # => ""
(string/slice "hello" -2)  # => "o"
(string/slice "hello" -5)  # => "ello"
(string/slice "hello" -6)  # => "hello"

(string/slice "hello" 1 1)  # => ""
(string/slice "hello" 1 2)  # => "e"
(string/slice "hello" 1 5)  # => "ello"

(string/slice "hello" -5 -4)  # => "e"
(string/slice "hello" -6 -4)  # => "he"

(string/slice "hello"  2  1)  # => ""
(string/slice "hello" -1 -2)  # => ""
string/slicecellularmitosisPlayground
(try
  (int/u64 "18446744073709551616")
  ([e] e))
# => "bad u64 initializer: string"
int/u64sogaiuPlayground
(last "hello")
# => 111
lastsogaiuPlayground
(get  [10 11 12 13]  0  )  # => 10
(get  {:a 10 :b 20}  :a )  # => 10

(get  [10 11 12]  99  )  # => nil
(get  [10 11 12]  -1  )  # => nil
(get  [10 11 12]  -2  )  # => nil
(get  {:a 1}      :z  )  # => nil

(map  (fn [x] (get x 0))   [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  ])
# =>                      @[  97  97  97   97    97     97      97       ]
getcellularmitosisPlayground
(os/lstat "t.janet")
# @{:size 249 :permissions "rw-r--r--" :nlink 1 :blocks 8 :dev 16777221 :accessed 1606236760 :modified 1606236759 :uid 501 :mode :file :blocksize 4096 :changed 1606236759 :inode 14801850 :rdev 0 :int-permissions 420 :gid 501}

(get (os/lstat "t.janet") :size)  # 249
os/lstatbtbytesPlayground
(def f (ev/spawn 4))
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
(fiber/last-value f) # => 4
fiber/last-valuepepePlayground
(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)  # File to watch.
(filewatch/listen ww)         # Start filewatcher listening for changes.

(ev/spawn-thread              # Spawn thread to catch change events.
 (print "Waiting...")
 (forever (pp (ev/take cc))))
filewatch/newoofoePlayground
math/e 
# => 2.71828
math/eleobmPlayground
(var a 0)
(assert (pos? a) "A is definitely not positive")
# error: A is definitely not positive
#  in assert [boot.janet] on line 149, column 11
#  in _thunk [repl] (tailcall) on line 3, column 1
assertpepePlayground
(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
not=cellularmitosisPlayground