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

(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground
(keyword :)
# => :
keywordsogaiuPlayground
(try
  (int/u64 "18446744073709551616")
  ([e] e))
# => "bad u64 initializer: string"
int/u64sogaiuPlayground
(map  |($ {:a 7 :b 8} )   [  keys      values    kvs             pairs                 ])
# =>                     @[  @[:a :b]  @[7 8]    @[:a 7 :b 8]    @[(:a 7) (:b 8)]      ]

(map  |($ [4 5 6] )       [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1 2]  @[4 5 6]  @[0 4 1 5 2 6]  @[(0 4) (1 5) (2 6)]  ]

(map  |($ 'ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ :ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ "ab" )          [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]
valuescellularmitosisPlayground
(spit "/tmp/hello.sh" "#!/bin/bash\necho 'Hello from Bash!'\n")
(os/chmod "/tmp/hello.sh" "rwx------")
(os/setenv "PATH" (string (os/getenv "PATH") ":/tmp"))
(os/shell "hello.sh")
os/chmodcellularmitosisPlayground
(drop-while |(> 3 $) [0 1 2 3 4 5 6 7 8 ])
# => (3 4 5 6 7 8)
drop-whileleobmPlayground
(map number? [nil   true  42   :a    "a"   []    {}    (fn []) ])
# =>        @[false false true false false false false false   ]
number?cellularmitosisPlayground
(defn inc [x] (+ x 1))
(defn inv [x] (* x -1))
(defn sq  [x] (* x x))

(-> 2 inc)             # -> 3
(-> 2 inc inc)         # -> 4
(-> 2 inc inc inv)     # -> -4
(-> 2 inc inc inv sq)  # -> 16
->cellularmitosisPlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
returnsogaiuPlayground
# This turns the current REPL session (environment) with all bindings, including synced closures,
# into a loadable or compilable image.
# It is the same as e.g.: `janet -c test.janet test.jimage` after (spit "test.janet" (currenv))

 (spit "test.jimage" (make-image (curenv)))
make-imageveqqqPlayground
(* 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
# complex matching, when you need to use predicates
# to determine a match
(def test {:a {:b 1} :c 2})
(match test
  ({:a a} (dictionary? a)) (print "is a dictionary"))
# --> "is a dictionary"

(match test
  ({:a a} (boolean? a)) (print "is a dictionary"))
# --> nil
matchllmIIPlayground
# From: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/helpers.janet#L52

(defn current-account [request]
  (def login (get-in request [:session :login] ""))
  (joy/db/find-by :account :where {:login login}))
joy/db/find-byveqqqPlayground
(string "hello" nil true 42 :bar)  # => "helloniltrue42bar"
(string :a :b :c)  # => "abc"
stringcellularmitosisPlayground
(map true? [ true nil   false 0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>      @[ true false false false false false false false false false false   false   ]
true?cellularmitosisPlayground