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

(def name "Joe")
(when (= name "Joe") "Hello Joe")
# "Hello Joe"


(when (not= 1 2) "not-equal")
# "not-equal"
whenleobmPlayground
(defn capitalize [str]
  (string (string/ascii-upper (string/slice str 0 1)) (string/slice str 1)))
string/ascii-upperveqqqPlayground
(any? [false false nil]) => nil
(any? [false false nil 1]) => 1
(any? [false false nil true]) => true
any?swlkrPlayground
(disasm +)
# {:max-arity 2147483647
#  :constants @[]
#  :name "+"
#  :defs @[]
#  :vararg true
#  :environments @[]
#  :arity 0
#  :slotcount 6

#  :bytecode
#  @[(len 1 0)
#    (eqim 2 1 0)
#    (jmpno 2 3)
#    (ldi 3 0)
#    (ret 3)
#    (eqim 2 1 1)
#    (jmpno 2 5)
#    (ldi 3 0)
#    (geti 4 0 0)
#    (add 3 3 4)
#    (ret 3)
#    (geti 3 0 0)
#    (ldi 5 1)
#    (in 4 0 5)
#    (add 3 3 4)
#    (addim 5 5 1)
#    (eq 2 5 1)
#    (jmpno 2 -4)
#    (ret 3)]
#  :min-arity 0}
disasmskuzzymigletPlayground
(map true? [ true nil   false 0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>      @[ true false false false false false false false false false false   false   ]
true?cellularmitosisPlayground
(math/atan2 0 0)
# => 0
math/atan2sogaiuPlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(defmacro- =dude
 "sets any symbolic expression to the atom :what?"
 [a]
 ~(var a :what?)) # => <function =dude>

(=dude 2) # => :what?

(=dude :2) # => :what?

(=dude 2) # => :what?

(=dude "2") # => :what?

(=dude @"2") # => :what?

(=dude @[2]) # => :what?

(=dude (2)) # => :what?

(=dude [2]) # => :what?

(=dude @{2 3}) # => :what?


(doc =dude)


# =>    macro
# =>    repl on line 59, column 1
# =>
# =>    (=dude a)


# => nil


# Macro expanding the =dude macro:

(macex (=dude 2)) # => :what?

(macex1 (=dude 2)) # => :what?

# Macros have to be quoted in order to expand

(macex ~(=dude 2)) # => (var a :what?)

(macex1 ~(=dude 2)) # => (var a :what?)

(macex ~(=dude @{2 3})) # => (var a :what?)
defmacro-jgartePlayground
(<)        # -> true
(< 1)      # -> true
(< 1 2)    # -> true
(< 2 1)    # -> false
(< 1 2 3)  # -> true
(< 1 3 2)  # -> false
<cellularmitosisPlayground
(peg/find-all ~(capture (range "09"))
              "hi 0 bye 1")
# => @[3 9]
peg/find-allsogaiuPlayground
(defn knorkulate [a b]
  (tracev (+ a (* b b ))))
# <function knorkulate>

(knorkulate 2 34)
# trace on line 2, column 1: (+ a (* b b)) is 1158
# 1158
tracevkamisoriPlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
returnsogaiuPlayground
(doc-of file/read) # => file/read docs are shown

(def tmpfile (file/temp))

(doc-of (tmpfile :read)) # => also shows the file/read docs. 
# Note that we didn't have to use the file/read symbol here to get docs.
doc-ofyumaikasPlayground
(defn read-from-file [file-path]
  (let [f (file/open file-path :r)
        content  (file/read f :all)]
    (file/close f)
    content))

### USAGE 

(read-from-file "/path/to/file-read-example.janet")
# => @"(defn read-from-file [file-path]\n  (let [f (file/open file-path :r)\n        content  (file/read f :all)]\n    (file/close f)\n    content))\n"
file/readharryvederciPlayground
(unmarshal @"hello world!") # => 104
unmarshaljgartePlayground