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

(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/roundcellularmitosisPlayground
(as-> [1 2 3] _ 
  (map inc _)
  (sum _)
  (- _ 10)
  (< _ 0))
# -> true

# same as
(< (- (sum (map inc [1 2 3])) 10) 0)
as->felixrPlayground
(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
(peg/find-all ~(capture (range "09"))
              "hi 0 bye 1")
# => @[3 9]
peg/find-allsogaiuPlayground
# To resume an image's environment:

# With this example image:
# echo "(def a 1)" > test.janet
# janet -c test.janet test.jimage
# Now open the REPL:

# To enter an image, for continued development or exploration. N.b. this does not work as expected:
# See: https://janet.zulipchat.com/#narrow/channel/409517-help/topic/Image.20Based.20Development.3F/with/529177765

(defn restore-image [image]
  (loop [[k v] :pairs image]
    (put (curenv) k v)))

(restore-image (load-image (slurp "test.jimage")))
load-imageveqqqPlayground
# note that os/touch does not create a file if it does not yet exist.
(os/touch "foo")
error: No such file or directory
  in os/touch
  in _thunk [repl] (tailcall) on line 2, column 1
os/touchcellularmitosisPlayground
(->  1 (< 2))   # -> true
(->> 1 (< 2))   # -> false
->cellularmitosisPlayground
(/ 2)
# => 0.5
/sogaiuPlayground
(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)]      ]
keyscellularmitosisPlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
(def conn-chan (ev/thread-chan 1000))

(defn producer [no]
  (forever
    (ev/sleep 5)
    (print "Adding data from producer num:" no)
    (ev/give conn-chan (math/random))))

(defn consumer [no]
  (forever
    (ev/sleep 0.5)
    (def num (ev/take conn-chan))
    (print num ": Printing from consumer:" no)))

(defn main [& args]
  (ev/spawn-thread (producer 1))
  (ev/spawn-thread (consumer 1))
  (ev/spawn-thread (consumer 2))
  (ev/sleep 20)
  (print "exiting"))
ev/thread-chanGeo-7Playground
(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))
string/joincellularmitosisPlayground
(-> "X"
    (string "a" "b")
    (string "c" "d")
    (string "e" "f"))  # => "Xabcdef"
->uvtcPlayground
(def chan (ev/chan))
(def f (ev/go (coro (ev/give-supervisor :msg "Hello")) nil chan))
(pp (ev/take chan)) # => (:msg "Hello")
ev/give-supervisorpepePlayground
(import joy)

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

# joy/routes will make handlers
(def r (joy/routes [:get "/hi/:name" :handle-func]))
# which you can pass to joy/app
(joy/server (joy/app {:routes r}) 9002)
# visit at localhost:9002/hi/bob
joy/routesveqqqPlayground