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

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
(invert :yo)
# => @{111 1 121 0}
invertsogaiuPlayground
(array/slice [1 2 3])

#=> @[1 2 3]
# good way to convert tuple to array
array/slicepepePlayground
(map math/abs   [-2.9 -2.1 2.1 2.9])  # => @[ 2.9  2.1  2.1  2.9 ]
(map math/floor [-2.9 -2.1 2.1 2.9])  # => @[ -3   -3   2    2   ]
(map math/ceil  [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   3    3   ]
(map math/round [-2.9 -2.1 2.1 2.9])  # => @[ -3   -2   2    3   ]
(map math/trunc [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   2    2   ]
math/ceilcellularmitosisPlayground
(map function? [ even?  (fn [])  |($)  file/read  ->   ])
# =>          @[ true   true     true  false      true ]
function?cellularmitosisPlayground
# If your tooling or the compiler doesn't believe a symbol will be bound e.g.:
(def a b) # if b e.g. gets bound by some macro or pushed directly to the env

# You can wrap it with compwhen on a bool:
(var- bla false)
(compwhen bla
          (def a b))

# Whatever defines b should then set bla to true
# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet
compwhenveqqqPlayground
# opens a file named filename for writing, and writes Hello World!
(def f (file/open "filename" :w))
(file/write f "Hello World!")
(file/flush f)
(file/close f)
file/writeterminalcommandPlayground
(update-in @{:a @{:b 1}} [:a :b] (fn [x] (+ 1 x)))
# @{:a @{:b 2}}
update-insbjaverPlayground
# note, if running a server from the repl, you need to (quit) your repl.

# in a terminal:
# $ while true; do date | nc 0.0.0.0 1234 -w 1; sleep 1; done

# in a janet repl:
(net/server "0.0.0.0" 1234
  (fn [conn]
    (prin (net/read conn 4096))
    (net/close conn)))
# output doesn't actually start until you (quit) your repl's fiber:
(quit)
net/servercellularmitosisPlayground
(as-> [1 2 3] _ 
  (map inc _)
  (sum _)
  (- _ 10)
  (< _ 0))
# -> true

# same as
(< (- (sum (map inc [1 2 3])) 10) 0)
as->felixrPlayground
## Quadratic Formula

(defn qform
  "Use the quadratic formula to solve for x. Returns all real solutions."
  [a b c]
  (def det (- (* b b) (* 4 a c)))
  (def factor (/ 0.5 a))
  (cond
    (neg? det) []
    (zero? det) [(* factor (- b))]
    (let [root-det (math/sqrt det)]
        [(* factor (- (- b) root-det)) (* factor (+ (- b) root-det))])))

    (qform 1 4 3) # -> (-3 -1)
defnbakpakinPlayground
(reduce (fn [s1 s2]
          (string "[" s1 "+" s2 "]"))
        "x"
        ["a" "b" "c"])

#=> "[[[x+a]+b]+c]"
reduceuvtcPlayground
(printf "%s %s!" "Hello" "World") # Hello World!
printfaquilaxPlayground
(var a 2)
a        # => 2
(dec a)  # => 1
a        # => 2
(-- a)   # => 1
a        # => 1
--cellularmitosisPlayground
(->
  {:a [1 2 3] :b [4 5 6]}
  (get :a)
  (sum)
  (string " is the result"))
# -> "6 is the result"

# same as:
(string (sum (get {:a [1 2 3] :b [4 5 6]} :a))" is the result")
->felixrPlayground