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

(math/abs -42)  # => 42
(map math/abs [-1 0 1])  # => @[1 0 1]
math/abscellularmitosisPlayground
(bxor 3 6)  # => 5

#     011  (3)
# xor 110  (6)
# -------
#     101  (5)
bxorcellularmitosisPlayground
(def a @[1 6])                     # => @[1 6]
(array/insert a 1 (splice [2 3]))  # => @[1 2 3 6]
(array/insert a 3 ;[4 5])          # => @[1 2 3 4 5 6]
array/insertcellularmitosisPlayground
(math/log (* math/e math/e))  # => 2
(math/log2 256)               # => 8
(math/log10 1000)             # => 3
math/logcellularmitosisPlayground
(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
(def s (symbol/slice :hello 2 4))

(print (type s)) # symbol
(print s)        # ll
symbol/slicewrqPlayground
# 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
(string/trim " foo ")  # => "foo"
(string/trim "_!_foo_!_" "_!")  # => "foo"
string/trimcellularmitosisPlayground
(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))
string/joincellularmitosisPlayground
(from-pairs [[:hello "world"] [:foo "bar"]]) 

# => @{:foo "bar" :hello "world"}
from-pairsjgartePlayground
trampoline should work at some callback method which will match these code. most provide callback should be like these! 
```c
// liba.so
void call_mul_at_callback(int i, void (*on_complete) (void*,void*), void* user_data) {
  // user_data = ctx
  // work_code
  int v = i * i + i* i * i << 12 - 5 *i;
  printf("from janet ffi\n");
  on_complete(&i, user_data);
  printf("FFI CALLBACK COMPLETE\n");
}
```

```janet
(ffi/context "liba.so")
(ffi/defbind call-mul-at-callback :void (i :int callback :ptr data :ptr))
(def cb (delay (ffi/trampoline :default)))
(call-mul-at-callback 15
                      (cb)
                      (fn[ptr]
                         (let [args (ffi/read (ffi/struct :int) ptr)]
                           (print (string/format "got value %d from ffi"
                                                 (first args))))))
```
ffi/trampolinedG94CgPlayground
(min 1 2 3)             # => 1
(min (splice [1 2 3]))  # => 1
(min ;[1 2 3])          # => 1
(apply min [1 2 3])     # => 1
mincellularmitosisPlayground
# Get a new and different random number each time you run your program.
(math/seedrandom (os/cryptorand 8))
(math/random)
math/randomuvtcPlayground
(math/exp 1)  # => 2.71828
math/e        # => 2.71828
math/expcellularmitosisPlayground
(flatten [1 [2 3 [4]] 5])  # => @[1 2 3 4 5]
flattencellularmitosisPlayground