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

(drop 1 "smile")
# => "mile"
dropsogaiuPlayground
(print
  (prompt :a 
          (for i 0 1000000000000 
            (print (string "i=" i))
            (if (= i 2) 
                (return :a 10)))))
# output:
# i=0
# i=1
# i=2
# 10
returnfelixrPlayground
(in "yo" 0)
# => 121
insogaiuPlayground
(keyword "")
# => :
keywordsogaiuPlayground
(array 1 2.3 :a "foo" true nil [] {} (fn []))
# => @[1 2.3 :a "foo" true nil () {} <function 0x7FB2A3F02170>]
arraycellularmitosisPlayground
(sorted [1 -2 2 3 9 -10])  # @[-10 -2 1 2 3 9]
sortedbtbytesPlayground
(os/clock)  # => 1.59384e+09
os/clockcellularmitosisPlayground
(defn test
  [x]
  (cond
    (> x 10) "Pretty big!"
    (< x 5) "Quite small"
    "Medium size"))

(test 40) # => "Pretty big!"
(test 2) # => "Quite small"
(test 6) # => "Medium size"
condpingiunPlayground
# catch and propagate an error with fiber

(try
  (+ 1 nil)
  ([err fib]
   (propagate err fib)))
tryswlkrPlayground
(array/slice [1 2 3])

#=> @[1 2 3]
# good way to convert tuple to array
array/slicepepePlayground
(def a @[[42 "c"] [68 "b"] [31 "d"] [27 "a"]])
(sort a (fn [a b]
          (< (a 1) (b 1))))
(pp a) # @[(27 "a") (68 "b") (42 "c") (31 "d")]
sortuvtcPlayground
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
(not=        [1 1]   [1 1])  # => false
(not=        [1 1]   [2 3])  # => true
(not=        [1 1]  @[1 1])  # => true
(not=        [1 1]  @[2 3])  # => true
(not=       @[1 1]  @[1 1])  # => true
(not=       @[1 1]  @[2 3])  # => true

(deep-not=   [1 1]   [1 1])  # => nil
(deep-not=   [1 1]   [2 3])  # => true
(deep-not=   [1 1]  @[1 1])  # => true
(deep-not=   [1 1]  @[2 3])  # => true
(deep-not=  @[1 1]  @[1 1])  # => nil
(deep-not=  @[1 1]  @[2 3])  # => true
deep-not=cellularmitosisPlayground
(let [p (parser/new)
      src ``
          (defn x
            [y]
            (+ 3 (* 4
                    (- 2 3)
          ``]
  (parser/consume p src)
  ((parser/state p) :delimiters))
# => "((("
parser/statesogaiuPlayground
# Linux pipes | send data through stdin
# To make linux programs accepting varied input forms:

(defn main [_ & args]
  # If no arguments, read from stdin
  (let [data (if (empty? args) (file/read stdin :all) (string/join args " "))]
    (print (sum (flatten (parse-all data))))))

# This accepts: 1 2 3 or "1" "2" "3" or "1 2 3" or "[1 2 3]" besides piping data in
# janet fib.janet 5 | janet sum.janet

# Allow file inputs also:

(defn main [_ & args]
  (let [data (cond (empty? args) (file/read stdin :all)
                   (os/stat (first args)) (slurp (first args))
                   (string/join args " "))]
    (print (sum (flatten (parse-all data))))))
stdinveqqqPlayground