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

# wrap short-fn / | 
(->> 10
     (|(/ $ 2)))
# =>
5

# also wrap fn
(->> 10
     ((fn [n] (/ n 2))))
# =>
5
->>sogaiuPlayground
(blshift 8 2)  # => 32
blshiftcellularmitosisPlayground
(from-pairs [[:a 1] [:b 2]])
# => @{:a 1 :b 2}
from-pairsswlkrPlayground
(do
  (def coll @[])
  (forv i 0 9
    (array/push coll i)
    (+= i 2))
  coll)
# => @[0 3 6]
forvsogaiuPlayground
(-)
# => 0
-sogaiuPlayground
# 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
Example usage


janet:1:> (def buf @"ABCDE")
@"ABCDE"
janet:2:> (buffer/slice 0)
error: bad slot #0, expected string|symbol|keyword|buffer, got 0
  in buffer/slice
  in _thunk [repl] (tailcall) on line 2, column 1
janet:3:> (buffer/slice buf 0)
@"ABCDE"
janet:4:> (buffer/slice buf 0 1)
@"A"
janet:5:> (buffer/slice buf 0 -1)
@"ABCDE"
janet:6:> (buffer/slice buf 1 3)
@"BC"
buffer/sliceroobiePlayground
# Get a new and different random number each time you run your program.
(math/seedrandom (os/cryptorand 8))
(math/random)
math/randomuvtcPlayground
(empty? [])
# => true
empty?sogaiuPlayground
(let [tbl @{:a 1}]
  (merge-into tbl {:b 2})
  tbl)
# => @{:a 1 :b 2}

# real-world example: https://git.sr.ht/~subsetpark/bagatto/tree/19aea03fe23fe5486890912df7dc4a936ce617a3/item/main.janet#L23
merge-intosogaiuPlayground
# janet 1.10.1

# non-numeric values appear to always be greater than numeric values
(< math/inf nil)      # -> true
(< math/inf true)     # -> true
(< math/inf false)    # -> true
(< math/inf "hello")  # -> true
(< math/inf :heyo)    # -> true
(< math/inf (fn []))  # -> true
(< math/inf {:a 1})   # -> true

# non-numeric values also follow an ordering.
# rearranging any of these values turns the result false:
(< nil false true "a" "b" :a :b [] [1] [1 1] [2] {} {:a 1} (fn []))  # -> true
<cellularmitosisPlayground
(file/open "iamfile.txt" :r)
(file/open "iamfile.txt" :w)
(file/open "iamfile.txt" :a)
file/openjgartePlayground
# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(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
(defn fizzbuzz [n]
  (cond 
    (and
      (= 0 (% n 3))
      (= 0 (% n 5))) "fizzbuzz"
    (= 0 (% n 3)) "fizz"
    (= 0 (% n 5)) "buzz"
    :else n))

(fizzbuzz 1)   # 1
(fizzbuzz 3)   # "fizz"
(fizzbuzz 5)   # "buzz"
(fizzbuzz 15)  # "fizzbuzz"
%cellularmitosisPlayground