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

(peg/find-all ~(capture :d)
              "hi 0 bye 1")
# => @[3 9]
peg/find-allsogaiuPlayground
(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
(table ;(kvs (struct :a 1 :b 2)))
# => @{:a 1 :b 2}
tablesogaiuPlayground
(map math/abs   [-2.9 -2.1 2.1 2.9])  # => @[ 2.9  2.1  2.1  2.9 ]
(map math/floor [-2.9 -2.1 2.1 2.9])  # => @[ -3   -3   2    2   ]
(map math/ceil  [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   3    3   ]
(map math/round [-2.9 -2.1 2.1 2.9])  # => @[ -3   -2   2    3   ]
(map math/trunc [-2.9 -2.1 2.1 2.9])  # => @[ -2   -2   2    2   ]
math/trunccellularmitosisPlayground
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt")  # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt"))  # => @["hello" "world" ""]

(defn slurp-lines [path]
  (string/split "\n" (slurp path)))

(slurp-lines "foo.txt")  # => @["hello" "world" ""]

(string/join @["hello" "world" ""] "\n")  # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" ""] "\n"))
# The contents of foo.txt and foo2.txt are now identical.

(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))

(spit-lines "foo3.txt" (slurp-lines "foo.txt"))
# The contents of foo.txt and foo3.txt are now identical.
slurpcellularmitosisPlayground
# Flatten single level of nested collections

(def foo [ [[1 2] [3 4]]   
           [[5 6] [7 8]] ])

(apply array/concat @[] foo) # => @[(1 2) (3 4) (5 6) (7 8)]
array/concatmaradPlayground
(def add17 (partial + 17))
(add17 3)            # -> 20
(add17 3 4)          # -> 24
(map add17 [1 2 3])  # -> @[18 19 20]
partialcellularmitosisPlayground
(math/ceil 1.1)  # => 2
(map math/ceil [1.1 1.2 1.3])  # => @[2 2 2]
math/ceilcellularmitosisPlayground
(defn ls-sockets
  "List the sockets in the current directory."
  []
  (filter
    (fn [fname] (= :socket (os/stat fname :mode)))
    (os/dir ".")))
os/statcellularmitosisPlayground
(string "hello " "world") # => "hello world"
stringswlkrPlayground
(os/shell "uptime > /tmp/uptime.txt")  # => 0
(slurp "/tmp/uptime.txt")
# => @"22:33  up 5 days,  9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt")  # => nil
os/shellcellularmitosisPlayground
(def a @[])
(array/insert a  1 :a)  # error: index out of range
(array/insert a -2 :a)  # error: index out of range
array/insertcellularmitosisPlayground
(table/to-struct @{:a 1}) # => {:a 1}
table/to-structswlkrPlayground
(def b @"")
(xprint b "HOHOHO")
# => nil
b
# => @"HOHOHO\n"
xprintpepePlayground
(os/execute
  ["/usr/bin/bash" "-c" "set"]
  :e
  @{"SOME" "value"
    "OTHER" "one"})
# => 0

# execute bash and prints environment variables
# which contains SOME=value and Other=one
os/executegoldenHairDafoPlayground