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

(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
# Find all bindings (in the current environment) with "push" in the name.
(doc "push")

#    Bindings:
#
#    array/push buffer/push buffer/push-byte buffer/push-string
#    buffer/push-word
#
#    Dynamics:
#
#
#
#    Use (doc sym) for more information on a binding.
docuvtcPlayground
(defn fizzbuzz [n]
  (cond 
    (and
      (= 0 (% n 3))
      (= 0 (% n 5))) "fizzbuzz"
    (= 0 (% n 3)) "fizz"
    (= 0 (% n 5)) "buzz"
    :else n))

(fizzbuzz 1)   # 1
(fizzbuzz 3)   # "fizz"
(fizzbuzz 5)   # "buzz"
(fizzbuzz 15)  # "fizzbuzz"
%cellularmitosisPlayground
# in the file ex.janet

(main [_ & args]
      (print "write something and I'll echo it")
      (def x (getline))
      (print x))

# in the terminal:
# $ janet ex.janet
# write something and I'll echo it
# bla <- you wrote this
# bla
getlineveqqqPlayground
(disasm (fn []) :bytecode)
# => @[(retn)]
disasmsogaiuPlayground
(merge {:a 1 :b 2})

#=> @{:a 1 :b 2} 
# good way to convert struct to table
mergepepePlayground
(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
(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
(def channel (ev/chan))

(ev/spawn
 (do
   (for i 0 10
     (ev/give channel (math/random))
     (ev/sleep 0.25))
   (ev/chan-close channel)))

(defn consumer [name delay]
  (loop [item :iterate (ev/take channel)]
    (match item
      [:close _] nil
      v (print name " got " v))))

(ev/call consumer "bob" 1)
(ev/call consumer "alice" 3)
ev/chan-closetxgruppiPlayground
(filter  even?                     [1 2 3 4 5])  # => @[2 4]
(filter  odd?                      [1 2 3 4 5])  # => @[1 3 5]
(filter  (fn [x] (not (even? x)))  [1 2 3 4 5])  # => @[1 3 5]
(filter  (complement even?)        [1 2 3 4 5])  # => @[1 3 5]

(def fns [even? odd?])
(map  (fn [f] (filter f [-2 -1 0 1 2]))  fns)  # => @[ @[-2 0 2] @[-1 1]   ]
(def fns (map complement fns))
(map  (fn [f] (filter f [-2 -1 0 1 2]))  fns)  # => @[ @[-1 1]   @[-2 0 2] ]
complementcellularmitosisPlayground
(count even? [1 2 3 4 5])  # => 2

(count  (fn [x] (> x 3))  [1 2 3 4 5])  # => 2
(count         |(> $ 3)   [1 2 3 4 5])  # => 2

(count  (fn [x] (truthy? x))  [nil false true 42 :a "foo"])  # => 4
(count         |(truthy? $)   [nil false true 42 :a "foo"])  # => 4

(var f even?)
(count f [1 2 3 4 5])  # => 2
(set f odd?)
(count f [1 2 3 4 5])  # => 3

(map  (fn [f] (count f [1 2 3 4 5]))  [even? odd?])  # => @[2 3]
(map         |(count $ [1 2 3 4 5])   [even? odd?])  # => @[2 3]
countcellularmitosisPlayground
(filter (fn [x] (> x 2)) [1 2 3 4 5])  # @[3 4 5]
filterbtbytesPlayground
(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
(invert [:ant :bee :elephant :fox :penguin])
# => @{:bee 1 :fox 3 :elephant 2 :ant 0 :penguin 4}
invertsogaiuPlayground
(first [4 5 6])  # => 4
firstcellularmitosisPlayground