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

(def kvpairs [[:x 1] [:y 2]])

(table ;(flatten kvpairs)) # => @{:x 1 :y 2}
flattenyumaikasPlayground
(array/new-filled 3)     # => @[nil nil nil]
(array/new-filled 3 :a)  # => @[:a :a :a]
array/new-filledcellularmitosisPlayground
(def buf-bytes 12)

(var new-buffer (buffer/new buf-bytes)) #--> @""

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
(os/cwd)  # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(get (os/environ) "PWD")  # => "/Users/cell/tmp"
os/cwdcellularmitosisPlayground
(string/bytes "foo")  # => (102 111 111)
(string/from-bytes 102 111 111)  # => "foo"
(string/from-bytes (splice (string/bytes "foo")))  # => "foo"

(map (fn [x] x)        "foo")  # => @[102 111 111]
(map string/from-bytes "foo")  # => @["f" "o" "o"]

(defn string/explode [s] (map string/from-bytes s))
(string/explode "foo")  # => @["f" "o" "o"]
string/from-bytescellularmitosisPlayground
(math/exp2 8)  # => 256
math/exp2cellularmitosisPlayground
(peg/replace ~(sequence :s (thru "t")) 
             " duck"
             "smiling cat sleeps")
# => @"smiling duck sleeps"
peg/replacesogaiuPlayground
(type print)
# =>
:cfunction
typesogaiuPlayground
# Note that the earlier sort func is executed later

(update @{:data @[:cherry :orange]} :data (comp sort |(array/push $ :apple)))
# => @{:data @[:apple :cherry :orange]}
compveqqqPlayground
(keyword)
# => :
keywordsogaiuPlayground
$ # 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
(reduce (fn [s1 s2]
          (string "[" s1 "+" s2 "]"))
        "x"
        ["a" "b" "c"])

#=> "[[[x+a]+b]+c]"
reduceuvtcPlayground
(math/round 1.1)  # => 1
(map math/round [1.49 1.50 1.51])  # => @[1 2 2]
math/roundcellularmitosisPlayground
(parse "(+ 4 5)") # => (+ 4 5)
(type (parse "(+ 4 5)")) # => :tuple
parseskuzzymigletPlayground
(defn walker
  `Simple walker function, that prints non-sequential 
   members of the form or prints "Sequence" and walks 
   recursively sequential members of the tree.` 
  [form] 
  (if (or (indexed? form) (dictionary? form))
    (do (print "Sequence")
        (walk walker form))
    (print form)))

(walk walker [[[[0 1 3]] 16 7 [3 [3 5]] 3 4] 1 [3 4]])

# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
walkpepePlayground