JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!

Loading...

Random Examples

# 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.
spitcellularmitosisPlayground
(let [p (parser/new)
      src ``
          (defn x
            [y]
            (+ 3 (* 4
                    (- 2 3)
          ``]
  (parser/consume p src)
  ((parser/state p) :delimiters))
# => "((("
parser/statesogaiuPlayground
(import spork/charts :as c)
(import spork/gfx2d :as g)
(g/save "bla.png" (c/plot-line-graph
                   :canvas (g/blank 100 100)
                   :data {:timestamp [1 2 3 40 5 60]
                          :temperature-1 [75.1 75.2 75.4 75.5 75.5 75.4]
                          :temperature-2 [55.1 55.4 55.7 60.0 60.4 60.9]}
                   :x-column :timestamp
                   :y-column [:temperature-1 :temperature-2]))

# then you can look at bla.png
spork/charts/plot-line-graphveqqqPlayground
(reduce2     + [1 2 3])  # -> 6
(accumulate2 + [1 2 3])  # -> @[1 3 6]
accumulate2cellularmitosisPlayground
(map bytes?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ true  true  true   true   false    false     false        false         ]

(map symbol?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ true  false false  false  false    false     false        false         ]

(map keyword?    [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false true  false  false  false    false     false        false         ]

(map string?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false true   false  false    false     false        false         ]

(map buffer?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  true   false    false     false        false         ]
symbol?cellularmitosisPlayground
(map int?  [ nil   true  0    1    3.14   (math/trunc 3.14)  (band 1.1 2.2) ])
# =>      @[ false false true true false  true               true           ]
int?cellularmitosisPlayground
In order to use :fresh, write:
(import path :fresh true)
importSaikyunPlayground
(filter (fn [x] (> x 2)) [1 2 3 4 5])  # @[3 4 5]
filterbtbytesPlayground
(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
# Get all matches, similar to a global regular expression

(peg/match '(any (+ (<- :a+) 1)) "Hello, world!")
# => @["Hello" "world"]

(peg/match '(any (+ (number :d) 1)) "1A2B3C4D5")
# => @[1 2 3 4 5]

(peg/match '(any (+ (<- :a+) 1)) "12345")
# => @[]
peg/matchTheLastZombiePlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
returnsogaiuPlayground
(neg? -42)  # => true
(map neg? [-1 0 1])  # => @[true false false]
neg?cellularmitosisPlayground
(find-index |(= $ "b") ["a" "b" "c" "d"]) #=> 1
find-indexAlecTroemelPlayground
(import joy)

# given a handler
(defn handle-func [request]
  (joy/text/plain (string "Hi " ((request :params) :name))))

# joy/defroutes will prepare routes
(joy/defroutes r [:get "/hi/:name" :handle-func])
# which you can pass to joy/app
(joy/server (joy/app {:routes r}) 9002)
# visit localhost:9002/hi/bob
joy/defroutesveqqqPlayground
(def server # name it, to close it after
  (with-dyns [:foo "foo"]
    (def env (curenv)) # must make a closure, otherwise it will use the fiber's env
    (net/server "0.0.0.0" 9092 # starts a fiber
                (fn [conn]
                  # pass the saved env:
                  (fiber/setenv (fiber/current) (table/setproto @{} env))
                  (:write conn (dyn :foo)) # n.b. stringify if using other :foo value
                  (:close conn))
                :stream)))


(:read (net/connect "127.0.0.1" 9092) 1024)
(:close server)
fiber/setenvveqqqPlayground
(os/time)  # => 1593837535
os/timecellularmitosisPlayground
(when-with [f (file/open "nofile.exists")] 
  (print "Realy?"))
# it just returns nil 
when-withpepePlayground
(def arr (array :a [:b :c] :d))
# => @[:a (:b :c) :d]
(0 arr)
# => :a
(1 arr)
# => (:b :c)

# out-of-bounds access causes an error
(try
  (3 arr)
  ([err] err))
# => "expected integer key for array in range [0, 3), got 3"

# you may use `get` to avoid the error
(get arr 3)
# => nil
arrayAndriamanitraPlayground
# wrap short-fn / | 
(->> 10
     (|(/ $ 2)))
# =>
5

# also wrap fn
(->> 10
     ((fn [n] (/ n 2))))
# =>
5
->>sogaiuPlayground
(slice [:a :b :c :d])  # => (:a :b :c :d)

(slice [:a :b :c :d] 0)  # => (:a :b :c :d)
(slice [:a :b :c :d] 1)  # => (:b :c :d)
(slice [:a :b :c :d] 3)  # => (:d)
(slice [:a :b :c :d] 4)  # => ()
(slice [:a :b :c :d] 5)  # error: index out of range

(slice [:a :b :c :d] -1)  # => ()
(slice [:a :b :c :d] -2)  # => (:d)
(slice [:a :b :c :d] -4)  # => (:b :c :d)
(slice [:a :b :c :d] -5)  # => (:a :b :c :d)
(slice [:a :b :c :d] -6)  # error: index out of range

(slice [:a :b :c :d] 0 0)  # => ()
(slice [:a :b :c :d] 0 1)  # => (:a)
(slice [:a :b :c :d] 0 4)  # => (:a :b :c :d)

(slice [:a :b :c :d] -1 -1)  # => ()
(slice [:a :b :c :d] -2 -1)  # => (:d)
(slice [:a :b :c :d] -5 -1)  # => (:a :b :c :d)

(slice [:a :b :c :d] 1 0)  # => ()
(slice [:a :b :c :d] 4 0)  # => ()
(slice [:a :b :c :d] -1 -2)  # => ()
(slice [:a :b :c :d] -1 -5)  # => ()
slicecellularmitosisPlayground