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

$ # trivial server which echo's to the console.
$ cat > srv.janet << EOF
#!/usr/bin/env janet

(defn handle-conn [conn]
  (print "new connection")
  (while true
    (def data (net/read conn 4096))
    (if (not data) (break))
    (prin data))
  (net/close conn)
  (print "connection closed"))

(print "starting server on 0.0.0.0:1234")
(net/server "0.0.0.0" 1234 handle-conn)
EOF
$ chmod +x srv.janet
$ ./srv.janet

----

$ # in another terminal:
$ echo hello | nc 0.0.0.0 1234
net/servercellularmitosisPlayground
(math/exp2 8)  # => 256
math/exp2cellularmitosisPlayground
(apply * [1 2 3])     # -> 6
(* (splice [1 2 3]))  # -> 6
(* ;[1 2 3])          # -> 6
(* 1 2 3)             # -> 6
applycellularmitosisPlayground
(forv i 0 10
 (print "hello"))

# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => nil
forvjgartePlayground
(def f (ev/spawn 4))
(ev/sleep 0.0001) # give ev a chance in the REPL, remove in file
(fiber/last-value f) # => 4
fiber/last-valuepepePlayground
(def ds @[@{:a 1 :b 2}
          @{:a 8 :b 9}])

(defn thrice [x] (* x 3))

(update-in ds [0 :b] thrice)

# `ds` is now
@[@{:a 1 :b 6}
  @{:a 8 :b 9}]
update-inuvtcPlayground
(var enabled false)
(toggle enabled)
(print enabled)
# => true
toggleFuriouZzPlayground
(as?-> [1 2 3] _ 
  (sum _)
  (when (> 6 _) _))
# => nil

(as?-> [1 2 3] _ 
  (sum _)
  (when (>= 6 _) _))

# => 6
as?->leobmPlayground
(unmarshal @"hello world!") # => 104
unmarshaljgartePlayground
(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
# Map over multiple structures -- Stops after the shortest is exhausted.
(map (fn [& args] ;args) [1] [1 2] [1 2 3])

# => @[(1 1 1)]
mapGrazfatherPlayground
(<)        # -> true
(< 1)      # -> true
(< 1 2)    # -> true
(< 2 1)    # -> false
(< 1 2 3)  # -> true
(< 1 3 2)  # -> false
<cellularmitosisPlayground
(defn capitalize [str]
  (string (string/ascii-upper (string/slice str 0 1)) (string/slice str 1)))
string/ascii-upperveqqqPlayground
(buffer/bit @"1" 0)
# => true
buffer/bitsogaiuPlayground
(map type [nil true 42 [] @[] {} @{} "a" @"b" 'c :d identity (fn [])])
# => @[:nil :boolean :number :tuple :array :struct :table :string :buffer :symbol :keyword :function :function]
typecellularmitosisPlayground