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

(in :yo 0)
# => 121
insogaiuPlayground
(def buf-bytes 12)

(var new-buffer (buffer/new buf-bytes)) #--> @""

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
(reduce2     + [1 2 3])  # -> 6
(accumulate2 + [1 2 3])  # -> @[1 3 6]
accumulate2cellularmitosisPlayground
(try
  (error :fun)
  ([e]
   (= e :fun)))
# => true
errorsogaiuPlayground
(interleave [:a :b :c] 
            [1 2 3] 
            ["x" "y" "z"])
# => @[:a 1 "x" :b 2 "y" :c 3 "z"]
interleavesogaiuPlayground
(- 1)
# => -1
-sogaiuPlayground
(seq [i :range [0 10] :when (odd? i)] (math/pow 2 i))

# => @[2 8 32 128 512]
# array with 2 to the power of all odd numbers smaller than 10
seqpepePlayground
(let [tbl @{:a 1}]
  (merge-into tbl {:b 2})
  tbl)
# => @{:a 1 :b 2}

# real-world example: https://git.sr.ht/~subsetpark/bagatto/tree/19aea03fe23fe5486890912df7dc4a936ce617a3/item/main.janet#L23
merge-intosogaiuPlayground
# Walk from the API is defined using a case 

(defn walk
  `Iterate over the values in ast and apply f
  to them. Collect the results in a data structure. If ast is not a
  table, struct, array, or tuple,
  returns form.`
  [f form]
  (case (type form)
    :table (walk-dict f form)
    :struct (table/to-struct (walk-dict f form))
    :array (walk-ind f form)
    :tuple (let [x (walk-ind f form)]
             (if (= :parens (tuple/type form))
               (tuple/slice x)
               (tuple/brackets ;x)))
    form))
casepingiunPlayground
(peg/find-all ~(capture :d)
              "hi 0 bye 1")
# => @[3 9]
peg/find-allsogaiuPlayground
(def ast '(print novar))
(def es (compile ast))
# => @{:column 11 :line 12 :error "unknown symbol novar"} returns struct with error on invalid ast
compilepepePlayground
(def a @[1 2 3])
(def b (array/slice a))

#=> good way to clone array, a and b are different arrays
array/slicepepePlayground
(map |(+ $0 $1) [1 2 3] [4 5 6]) # @[5 7 9] - uses the fn shorthand
mapahungryPlayground
(string/repeat "moshi" 2)  # => "moshimoshi"
string/repeatcellularmitosisPlayground
(def chan (ev/chan))
(def f (ev/go (coro (ev/give-supervisor :msg "Hello")) nil chan))
(pp (ev/take chan)) # => (:msg "Hello")
ev/give-supervisorpepePlayground