[go: up one dir, main page]

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/floor 1.1)  # => 1
(map math/floor [1.1 1.2 1.3])  # => @[1 1 1]
math/floorcellularmitosisPlayground
(math/rng-uniform (math/rng 0))
# => 0.487181
math/rng-uniformsogaiuPlayground
(varglobal "smile" false)
# => nil

smile
# => false

(set smile true)
# => smile

smile
# => true

(dyn 'smile)
# => @{:ref @[true]}
varglobalsogaiuPlayground
(ev/spawn (os/sleep 1) (print "Hard work is done!"))

# prints "Hard work is done!" after one second
# this is the easiest way to put some forms on the event loop
# but do not forget REPL is blocking, for now, so run the example with `janet -e`
ev/spawnpepePlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
labelsogaiuPlayground
(seq [i :range [0 3]
      j :range [0 3]
      :let [c (string/format "%c" (+ 97 i))]]
  [(keyword c) j])
# => '@[(:a 0) (:a 1) (:a 2) (:b 0) (:b 1) (:b 2) (:c 0) (:c 1) (:c 2)]
seqsogaiuPlayground
(doc string)


# =>    cfunction
# =>    ../src/core/corelib.c on line 349, column 1
# =>
# =>    (string & xs)
# =>
# =>    Creates a string by concatenating the elements of xs together. If
# =>    an element is not a byte sequence, it is converted to bytes via
# =>    describe. Returns the new string.


# nil
docjgartePlayground
(let [a 1 b 2 c 3] (+ a b c))  # => 6

(let
  [a 1
   b (+ a 1)
   c (+ b 1)]
  (+ a b c))  # => 6
letcellularmitosisPlayground
# Interactively read a line from STDIN
(file/read stdin :line)

# Type abcd and then press ENTER
abcd

# Returns:
@"abcd\n"
stdinsemperosPlayground
(type print)
# =>
:cfunction
typesogaiuPlayground
# Suppose you have a fiber that yields chunks of paginated api results:
(def api-results (fiber/new (fn [] (yield [1 2 3]) (yield [4 5 6]))))

# Using :iterate, the right side of the binding is evaluated each time the loop is run,
# which allows for running a side-effecting expression that may be different each time.
(loop [_ :iterate (fiber/can-resume? api-results)] (pp (resume api-results)))

# This example can be simplified using :generate
(loop [chunk :generate api-results] (pp chunk))
loopstaabPlayground
# with by function 

(sorted [1 -2 2 3 9 -10] >)  #@[9 3 2 1 -2 -10]
sortedleobmPlayground
(dyn 'defn)
# => @{:source-map ("boot.janet" 12 1) :value <function defn> :doc "(defn name & more)\n\nDefine a function. Equivalent to (def name (fn name [args] ...))." :macro true}
dynsogaiuPlayground
# absolute value of negatives, ignore positives
(keep |(if (< $ 0) (- $) nil) [-1 2 -3 -4 -5 6]) # -> @[1 3 4 5]

# halves each even number, ignores odds
(keep |(when (even? $) (/ $ 2)) [1 2 8 7 -2])    # -> @[1 4 -1]
keepTechcablePlayground
(defn output [x]
  (case x
    :a "a"
    "b"))

(output :a) # => "a"
(output "anything else") # => "b"
caseswlkrPlayground