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

# many terminals will truncate long data, use this if you need to see/copypaste it:
(defn full-print `Print a large DS without truncation` [ds] (print (string/format "%m" ds)))
ppveqqqPlayground
(math/exp 1)  # => 2.71828
math/e        # => 2.71828
math/expcellularmitosisPlayground
(def p1 (os/spawn ["echo" "hello"] :p {:out :pipe}))
(def p2 (os/spawn ["grep" "hello"] :p {:in (p1 :out)}))

(:wait p2)
# Creates a pipeline (e.g. echo hello | grep hello)
os/spawnbakpakinPlayground
janet:2:> (all true? [true true false])
false
janet:3:> (all even? [2 4 6 7])
false
janet:5:> (all even? [2 4 6])
true
allswlkrPlayground
# see https://janet-lang.org/docs/abstract_machine.html

(def plus10
 (asm 
   '{
     :arity 1
     :bytecode @[(ldi 1 10)    # $1 = 10
                 (add 2 0 1)   # $2 = $0 + $1
                 (ret 2)]}))   # return $2

(plus10 1) # -> 11
asmfelixrPlayground
(keyword? :a)   # => true
(keyword? "a")  # => false
keyword?cellularmitosisPlayground
(get (os/environ) "HOME")  # => "/Users/cell"
(os/getenv "HOME")  # => "/Users/cell"
os/environcellularmitosisPlayground
(each item [1 2 3 4] (print item))
# prints 1
# prints 2
# prints 3
# prints 4
# => nil
eachpepePlayground
(partition-by even? [1 1 2 3 5 8 13 21])
# => @[@[1 1] @[2] @[3 5] @[8] @[13 21]]
partition-bysogaiuPlayground
(group-by odd? [1 2 3 5 6])
# =>
@{false @[2 6] true @[1 3 5]}
group-byuvtcPlayground
(get default-peg-grammar :a)
# => '(range "az" "AZ")
default-peg-grammarsogaiuPlayground
(math/rng-uniform (math/rng 0))
# => 0.487181
math/rng-uniformsogaiuPlayground
(bnot 255)  # => -256
bnotcellularmitosisPlayground
(map dictionary? [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         true          ]

(map struct?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         false         ]

(map table?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     false        true          ]
table?cellularmitosisPlayground
(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground