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

# `parse` can decode arbitrary JDN (Janet Data Notation) encoded by `string/format`

(def encoded (string/format "%j" @{:a 123 :b "foo" :c @{1 [1.2 2.3 3.4]}}))
# => "@{:a 123 :b \"foo\" :c @{1 @[1.2 2.2999999999999998 3.3999999999999999]}}"

(parse encoded)
# => @{:a 123 :b "foo" :c @{1 @[1.2 2.3 3.4]}}
parseCFiggersPlayground
(def f (generate [i :range [0 5]] (+ i i)))

(print (fiber/status f))
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (fiber/status f)) # -> :pending
(print (resume f)) 
(print (fiber/status f)) # -> :dead


# :new
# 0
# 2
# 4
# 6
# 8
# :pending
#
# :dead
generateleobmPlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(map truthy? [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ false false true  true  true  true  true  true  true  true  true    ]

# note that 'not' works as an implementation of 'falsey?'
(map not     [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ true  true  false false false false false false false false false   ]
truthy?cellularmitosisPlayground
(os/mkdir "templates")
# => creates ./templates directory
os/mkdirpepePlayground
(sort (keys default-peg-grammar))
# => @[:A :D :H :S :W :a :a* :a+ :d :d* :d+ :h :h* :h+ :s :s* :s+ :w :w* :w+]
default-peg-grammarsogaiuPlayground
(protect
  (if (> (math/random) 0.42)
    (error "Good luck")
    "Bad luck"))
# => randomly returns:
# (false "Good luck")
# or
# (true "Bad luck")
protectpepePlayground
# demo of (@ <sym>) feature
(let [content "# => label"]
  (match [:comment @{} "# => label"]
    [:comment _ (@ content)]
    :found
    nil))
# => :found
matchsogaiuPlayground
(try
  (error :fun)
  ([e]
   (= e :fun)))
# => true
errorsogaiuPlayground
(string/replace "+" "-" "ctrl+c")
# => "ctrl-c"
string/replacesogaiuPlayground
(do
  (def before (dyn :expression))
  (setdyn :expression :smile)
  [before (dyn :expression)])
# => '(nil :smile)
setdynsogaiuPlayground
(def name "Joe")
(when (= name "Joe") "Hello Joe")
# "Hello Joe"


(when (not= 1 2) "not-equal")
# "not-equal"
whenleobmPlayground
(os/shell "echo bar > /tmp/foo")
(with
  [file-handle
   (file/open "/tmp/foo")
   (fn [fd] (file/close fd))]
  (file/read file-handle :all))  # => @"bar\n"
withcellularmitosisPlayground
(def a @[1 2 3])
(def b (array/slice a))

#=> good way to clone array, a and b are different arrays
array/slicepepePlayground
(buffer/bit @"0" 4)
# => true
buffer/bitsogaiuPlayground