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

(def a @[])
(array/push a 1)  # => @[1]
a                 # => @[1]
(array/push a 2)  # => @[1 2]
a                 # => @[1 2]
array/pushcellularmitosisPlayground
(scan-number "1_000_000")
# => 1000000
scan-numbersogaiuPlayground
(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
(var a 1)
a        # => 1
(inc a)  # => 2
a        # => 1
(++ a)   # => 2
a        # => 2
++cellularmitosisPlayground
(math/pow 2 8)  # => 256
math/powcellularmitosisPlayground
(os/which)  # => :macos
os/whichcellularmitosisPlayground
(ev/call print 10)
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
# => prints 10
ev/callpepePlayground
(def error-levels {:red 3 :orange 2 :yellow 1})

# Create a new prototype object called ErrorProto with one method, :compare
(def ErrorProto
  @{:level nil

    # Returns -1, 0, 1 for x < y, x = y, x > y respectively.
    :compare (fn [self other]
               (let [lx (error-levels (self :level))
                     ly (error-levels (other :level))]
                 (cond
                   (< lx ly) -1
                   (> lx ly) 1
                   :else 0)))})

# Factory function to create new Error objects 
(defn make-error
  [level]
  (table/setproto @{:level level} ErrorProto))


(def err-red (make-error :red))
(def err-yell (make-error :yellow))
(def err-orange (make-error :orange))

# calls of the polymorphic compare function
(compare err-red err-orange) # 1
(compare err-orange err-red) # -1
(compare err-red err-red)    # 0

# These following functions call internally 
# the polymorphic compare function, but return a boolean value
(compare> err-red err-orange)  # true
(compare> err-yell err-orange) # false
(compare= err-yell err-yell)   # true

#-------------------------------------------------------------------------------
# sort the objects with compare> and compare<

(def errors-unsorted @[err-red err-yell err-orange])

# ascending order
(sort errors-unsorted compare<) 
# => @[@{:level :yellow} @{:level :orange} @{:level :red}]

# descending order
(sort errors-unsorted compare>) 
# => @[@{:level :red} @{:level :orange} @{:level :yellow}]

# without compare
(sort-by |(error-levels ($ :level)) errors-unsorted)
# => @[@{:level :yellow} @{:level :orange} @{:level :red}]

# Note!!!, the following does not work as expected. 
# sort alone does not automatically use the compare function (the comparator)
(sort errors-unsorted) # result is not sorted!
compareleobmPlayground
# To resume an image's environment:

# With this example image:
# echo "(def a 1)" > test.janet
# janet -c test.janet test.jimage
# Now open the REPL:

# To enter an image, for continued development or exploration. N.b. this does not work as expected:
# See: https://janet.zulipchat.com/#narrow/channel/409517-help/topic/Image.20Based.20Development.3F/with/529177765

(defn restore-image [image]
  (loop [[k v] :pairs image]
    (put (curenv) k v)))

(restore-image (load-image (slurp "test.jimage")))
load-imageveqqqPlayground
(table/clear @{:a 1 :b 2})
# => @{}
table/clearsogaiuPlayground
(let [sweet "what does mine say?"]
 (let [dude sweet]
  (let [what-does-mine-say? dude]
   what-does-mine-say?))) # => "what does mine say?"
letjgartePlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
(table/new 2)
# => @{}
table/newsogaiuPlayground
path/delim # => ":" on Unix and Linux, ";" on Windows
spork/path/delimclementiPlayground
(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