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

(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/trunccellularmitosisPlayground
(buffer/bit @"0" 4)
# => true
buffer/bitsogaiuPlayground
(var a 1)
a        # => 1
(inc a)  # => 2
a        # => 1
(++ a)   # => 2
a        # => 2
++cellularmitosisPlayground
# create a channel without buffer, default is 0
(def channel (ev/chan))

(ev/spawn
  (ev/sleep 5)
  (ev/give channel "Hard work is done!"))

(print "do anything")
(for i 0 5
  (print i)
  (ev/sleep 0.5))

(print (ev/take channel)) # blocks here, until there is a result in the channel
(print "done")

ev/chanleobmPlayground
(* 2 3)      # -> 6
(* 2 3.3)    # -> 6.6
(* 2.2 3.3)  # -> 7.26

(def pi 3.14159)
(* 2 pi)  # -> 6.28318

(* 2)  # -> 2
(*)    # -> 1

(* 2 3 4)             # -> 24
(apply * [2 3 4])     # -> 24
(* (splice [2 3 4]))  # -> 24
(* ;[2 3 4])          # -> 24
(def a [2 3 4])
(* ;a)                # -> 24
*cellularmitosisPlayground
(string/has-prefix? "project." "project.janet") # => true
string/has-prefix?sogaiuPlayground
(map truthy? [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ false false true  true  true  true  true  true  true  true  true    ]

# note that 'not' works as an implementation of 'falsey?'
(map not     [ nil   false true  0     1     'a    :a    "a"   []    {}    (fn []) ])
# =>        @[ true  true  false false false false false false false false false   ]
truthy?cellularmitosisPlayground
(os/shell "seq 12 | head -n 1") # => 1
os/shelljgartePlayground
(partition-by even? [1 1 2 3 5 8 13 21])
# => @[@[1 1] @[2] @[3 5] @[8] @[13 21]]
partition-bysogaiuPlayground
(type (fiber/new (fn [])))
# =>
:fiber
typesogaiuPlayground
(-> "X"
    (string "a" "b")
    (string "c" "d")
    (string "e" "f"))  # => "Xabcdef"
->uvtcPlayground
# Contrived examples returning the variadic arguments passed in.
(defn example-function [& args] args)
(defmacro example-macro [& args] ~(tuple ,;args))

(macex '(example-macro 1 2 3))

(assert (= (example-function 1 2 3)
           (example-macro 1 2 3)))

(def args [1 2 3])

# `apply` is for functions, but there's always `eval`.
(assert (= (apply example-function args)
           (eval ~(example-macro ,;args))))

# Same return for both.
# => (1 2 3)
apply4kbytePlayground
(peg/replace-all '(set "aiou") "e" "The quick brown fox jumps over the lazy dog")
# returns The qeeck brewn fex jemps ever the lezy deg
peg/replace-allpepePlayground
(def p (os/spawn ["ls"] :p {:in :pipe :out :pipe})) # define core/process with selfpipe
(pp (:read (p :out) :all)) # => prints the ls output
(pp (:wait p)) # => waits for the process to finish, prints 0 if succesful 
os/spawnpepePlayground
(take-while number? @[1 2 3 "4" 5 6 7 8 9 "10" 11 "12"]) # => (1 2 3)
take-whilejgartePlayground