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

(varglobal "smile" false)
# => nil

smile
# => false

(set smile true)
# => smile

smile
# => true

(dyn 'smile)
# => @{:ref @[true]}
varglobalsogaiuPlayground
(defn walker
  `Simple walker function, that prints non-sequential 
   members of the form or prints "Sequence" and walks 
   recursively sequential members of the tree.` 
  [form] 
  (if (or (indexed? form) (dictionary? form))
    (do (print "Sequence")
        (walk walker form))
    (print form)))

(walk walker [[[[0 1 3]] 16 7 [3 [3 5]] 3 4] 1 [3 4]])

# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
walkpepePlayground
(def a @[1 2])
(array/pop a)  # => 2
a              # => @[1]
(array/pop a)  # => 1
a              # => @[]
(array/pop a)  # => nil
a              # => @[]
array/popcellularmitosisPlayground
  (->> (all-bindings)
       (keep (fn [sym]
               (when (string/has-prefix? "peg/" (string sym))
                 (string sym))))
       sort)

  # =>
  @["peg/compile"
    "peg/find"
    "peg/find-all"
    "peg/match"
    "peg/replace"
    "peg/replace-all"]

# from https://github.com/sogaiu/margaret/blob/master/tutorials/tutorial.janet
all-bindingsveqqqPlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(get (os/environ) "HOME")  # => "/Users/cell"
(os/getenv "HOME")  # => "/Users/cell"
os/environcellularmitosisPlayground
(var x 12) # => 12

(/= x 2) # => 6
/=jgartePlayground
[1 2 3]               # => (1 2 3)
(tuple/type [1 2 3])  # => :parens

(tuple/brackets 1 2 3)               # => [1 2 3]
(tuple/type (tuple/brackets 1 2 3))  # => :brackets
tuple/typecellularmitosisPlayground
# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(protect
  (if (> (math/random) 0.42)
    (error "Good luck")
    "Bad luck"))
# => randomly returns:
# (false "Good luck")
# or
# (true "Bad luck")
protectpepePlayground
(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))
string/joincellularmitosisPlayground
# 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
(peg/replace ~(sequence :s (thru "t")) 
             " duck"
             "smiling cat sleeps")
# => @"smiling duck sleeps"
peg/replacesogaiuPlayground
(pp (all-bindings))
# => prints @[% %= * ... yield zero? zipcoll]

(def a "A")
(pp (all-bindings (curenv) true))
# => prints @[_ a] - only local bindings are listed
all-bindingspepePlayground
(defn greet-me [] (print "hey programmer!"))
(defn greet-stranger [] (print "hey stranger!"))

(varfn greet [] (greet-me))
(greet) # prints "hey programmer!"
(varfn greet [] (greet-stranger))
(greet) # prints "hey stranger!"

# kind of analogous to (def greet (fn [] (some-thunk))), but with built-in
# sugar to automatically update 'greet' if varfn is called with a name
# already bound
varfnwrqPlayground