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

(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(var a false)
(defer (set a 42) 
  (set a true)
  (error "Oh no!"))
# error: Oh no!

(pp a) # => prints 42
deferpepePlayground
(defn output [x]
  (case x
    :a "a"
    "b"))

(output :a) # => "a"
(output "anything else") # => "b"
caseswlkrPlayground
(let [x 10]
  (unless (= x 10)
    (print "x is not 10!")
  )
)

# => nil

(let [x 5]
  (unless (= x 10)
    (print "x is not 10!")
  )
)

# => "x is not 10!"
unlessHoangTuan110Playground
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepePlayground
(scan-number "123") # => 123
(scan-number "f") # => nil

(scan-number 123) # =>
# error: bad slot #0, expected string|symbol|keyword|buffer, got 123
#   in scan-number
#   in _thunk [repl] (tailcall) on line 1, column 1
scan-numberswlkrPlayground
(try
  (int/u64 "18446744073709551616")
  ([e] e))
# => "bad u64 initializer: string"
int/u64sogaiuPlayground
# Convert an array of k/v pairs into a table

(def kvp @[[:foo 1] [:bar 2]])
(table ;(mapcat identity kvp))  # => @{:foo 1 :bar 2}
mapcatMikeBellerPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->cellularmitosisPlayground
(->> "X"
     (string "a" "b")
     (string "c" "d")
     (string "e" "f"))  # => "efcdabX"
->>uvtcPlayground
# 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
(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
# janet 1.10.1

(= :a :a)    # => true
(= :a "a")   # => false
(= :a ":a")  # => false

(= "a" "a")    # => true
(= "a" @"a")   # => false
(= @"a" @"a")  # => false

(= [1 2] [1 2])    # => true
(= [1 2] @[1 2])   # => false
(= @[1 2] @[1 2])  # => false

(= {:a 1} {:a 1})    # => true
(= {:a 1} @{:a 1})   # => false
(= @{:a 1} @{:a 1})  # => false

(= (fn []) (fn []))  # => false
=cellularmitosisPlayground
(let [a 1 b 2 c 3] (+ a b c))  # => 6

(let
  [a 1
   b (+ a 1)
   c (+ b 1)]
  (+ a b c))  # => 6
letcellularmitosisPlayground
(math/log (* math/e math/e))  # => 2
(math/log2 256)               # => 8
(math/log10 1000)             # => 3
math/logcellularmitosisPlayground