JanetDocsI'm feeling luckyGitHub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

(def kvpairs [[:x 1] [:y 2]])

(table ;(flatten kvpairs)) => @{:x 1 :y 2}
flattenyumaikas
(def kvpairs [[:x 1] [:y 2]])

(table ;(flatten kvpairs)) => @{:x 1 :y 2}
tableyumaikas
(def f (ev/go (coro "world"))) # coro is great for fast fiber creating
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
(fiber/last-value f) # "world"
ev/gopepe
(peg/find ':d "Battery temperature: 40 °C")
# => 21 index of the first number
peg/findpepe
(os/mkdir "templates")
# => creates ./templates directory
os/mkdirpepe
(var a false)
(defer (set a 42) 
  (set a true)
  (error "Oh no!"))
# error: Oh no!

(pp a) # => prints 42
deferpepe
(def a @[23 42])
(array/clear a)
(pp a)
# => prints @[]
array/clearpepe
(def ast '(print novar))
(def es (compile ast))
# => @{:column 11 :line 12 :error "unknown symbol novar"} returns struct with error on invalid ast
compilepepe
(def cc (compile '(print "Janet"))) # => <function _thunk> on success returns function
(cc) # => prints Janet
compilepepe
(fiber/root) # => <fiber 0x562FF22F10A0> your hex number will differ
fiber/rootpepe
(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
assertpepe
(seq [i :range [0 10] :when (odd? i)] (math/pow 2 i))

# => @[2 8 32 128 512]
# array with 2 to the power of all odd numbers smaller than 10
seqpepe
(def p (os/spawn ["ls"] :p {:in :pipe :out :pipe})) # define core/process with selfpipe
(pp (:read (p :out) :all)) # => prints the ls output
(pp (:wait p)) # => waits for the process to finish, prints 0 if succesful 
os/spawnpepe
# Setup
(spit "poem.txt" "I’ve decided to tackle it\nIt’s going to get done")

# A version of head -n 1, but entirely in Janet
# Note, that because files in Janet support :close as a method
# we don't have to define a dtor. 
# There might be other closing work to do, of course
(with [f (file/open "poem.txt")] (print (:read f :line))) # => "I've decided to tackle it"
withyumaikas
(def chan (ev/chan))
(def f (ev/go (coro (yield "world")) nil chan))
(def [sig fib] (ev/take chan))
(pp sig) # => prints :yield
(pp (fiber/last-value fib)) # => prints world
ev/chanpepe
(defn worker [m]
  (thread/send m "Hello function")
  (:send m "Hello method"))

(thread/new worker)
(pp (thread/receive)) # => prints Hello function
(pp (thread/receive)) # => prints Hello method

thread/receivepepe
(def t @{:a 1 :b 2 :c @[1 2 3]})
(def ct (table/clone t))

(put ct :a 3) # => @{:c @[1 2 3] :a 3 :b 2}
(pp t) # => @{:c @[1 2 3] :a 1 :b 2}

(update ct :c array/concat 4) # => @{:c @[1 2 3 4] :a 3 :b 2}
(pp t) # => @{:c @[1 2 3 4] :a 3 :b 2}
# array under key :c is shared between tables!
table/clonepepe
(protect
  (if (> (math/random) 0.42)
    (error "Good luck")
    "Bad luck"))
# => randomly returns:
# (false "Good luck")
# or
# (true "Bad luck")
protectpepe
(repeat 3 (print "HO"))
# => prints
# HO
# HO
# HO
repeatpepe
(defn worker [m]
  (thread/send m "Hello function")
  (:send m "Hello method"))

(thread/new worker)
(pp (thread/receive)) # => prints Hello function
(pp (thread/receive)) # => prints Hello method
thread/sendpepe
(ev/call print 10)
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
# => prints 10
ev/callpepe
(seq [v :in (coro
                (yield :hi)
                (yield :bye))]
    v)
# => @[:hi :bye]
coropepe
(def chan (ev/chan))
(def f (ev/go (coro (ev/give-supervisor :msg "Hello")) nil chan))
(pp (ev/take chan)) # => (:msg "Hello")
ev/give-supervisorpepe
(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-valuepepe
(def f (fiber/new (fn [] (yield 2) 3)))
(pp (resume f)) # => 2
(resume f)
(pp (fiber/last-value f)) # => 3
fiber/last-valuepepe
(forever
  (print (os/time))
  (ev/sleep 1))

# => epoch clocks, prints epoch timestamp every second
foreverpepe
(freeze @"Ho") #=> "Ho"
(freeze @("Ho")) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepe
(in :yo 0)
# => 121
insogaiu
(in "yo" 0)
# => 121
insogaiu
(invert :yo)
# => @{121 0 111 1}
invertsogaiu