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

(loop [x :range [1 10]
       :let [square (* x x)]
       :until (> square 9)
       :before (print "before")
       :after (print "after")
       :repeat 2]
  (print (string "square: " square)))

# before
# square: 1
# square: 1
# after
# before
# square: 4
# square: 4
# after
# before
# square: 9
# square: 9
# after

loopstaabPlayground
(def a @[1 2])
(def b @[1 2])
(= a b)  # => false

(def a @[1 2])
(def b (array/concat a 3))
a        # => @[1 2 3]
b        # => @[1 2 3]
(= a b)  # => true
array/concatcellularmitosisPlayground
(repeat 3 (print "HO"))
# => prints
# HO
# HO
# HO
repeatpepePlayground
# removes a file in the current directory
(os/rm "hello.txt")
os/rmswlkrPlayground
(forever 
 (print "and then?") 
 (ev/sleep 1) 
 (print "no and then!")) 

# => and then? 
# sleeps for one second
# => no and then!
# => and then?
# sleeps for one second
# => no and then!
# ...
foreverjgartePlayground
(let [buf @"hello"]
  (buffer/blit buf " there" -1))
# =>
@"hello there"
buffer/blitsogaiuPlayground
# convert a string to an integer
(peg/match '(number :d+) "123")
#=> @[123]

(first (peg/match '(number :d+) "123"))
#=> 123
peg/matcherichaneyPlayground
(apply * [1 2 3])     # -> 6
(* (splice [1 2 3]))  # -> 6
(* ;[1 2 3])          # -> 6
(* 1 2 3)             # -> 6
applycellularmitosisPlayground
(mean [1 10 100])  # => 37
(mean [])          # => nan
meancellularmitosisPlayground
(buffer ;(interpose " " [:ant 'bee "cat" ``dog`` @`ewe`]))
# => @"ant bee cat dog ewe"
buffersogaiuPlayground
(or true)        # => true
(or true true)   # => true
(or true false)  # => true

(or false 1 2)  # => 1
(or false 2 1)  # => 2

(or false nil)  # => nil
(or nil false)  # => false

# note that `or` does not behave as you might expect
# when used with `apply` and `splice`:
(or 1 2 3)             # => 1
(or (splice [1 2 3]))  # => (1 2 3)
(apply or [1 2 3])     # => (if 1 1 (if 2 2 3))
orcellularmitosisPlayground
(last 'hello)
# => 111
lastsogaiuPlayground
(string? "hello")   # => true
(string? @"hello")  # => false
(string? :hello)    # => false
string?cellularmitosisPlayground
(def [pipe-r pipe-w] (os/pipe))

(ev/spawn
  # write to the pipe in a separate fiber
  (for i 0 32000
    (def str (string "Hello Janet " i "\n"))
    (:write pipe-w str))
  (:close pipe-w))

(forever
  (def text (:read pipe-r 4096))
  (when (nil? text) (break))
  (pp text))

# read a series of strings from the pipe in parallel
# to writing to the other side, to avoid the program
# from hanging if the pipe is "full"
#
# see https://github.com/janet-lang/janet/issues/1265
os/pipeYohananDiamondPlayground
(os/which)  # => :macos
os/whichcellularmitosisPlayground