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

(forever
  (print (os/time))
  (ev/sleep 1))

# => epoch clocks, prints epoch timestamp every second
foreverpepePlayground
(def a @[])
(array/push a 1)  # => @[1]
a                 # => @[1]
(array/push a 2)  # => @[1 2]
a                 # => @[1 2]
array/pushcellularmitosisPlayground
(print
  (prompt :a 
          (for i 0 1000000000000 
            (print (string "i=" i))
            (if (= i 2) 
                (return :a 10)))))
# output:
# i=0
# i=1
# i=2
# 10
returnfelixrPlayground
(any? "")
# => nil

(any? "Hello")
# => 72

(any? @"Hello")
# => 72

(any? @[])
# => nil

(any? [])
# => nil

(any? {})
# => nil

(any? @{})
# => nil

(any? @[1 2 3 4])
# => 1 

(any? @{:a 2 :b 3})
# => 2
any?leobmPlayground
(apply * [1 2 3])     # -> 6
(* (splice [1 2 3]))  # -> 6
(* ;[1 2 3])          # -> 6
(* 1 2 3)             # -> 6
applycellularmitosisPlayground
(map nil? [nil  0     false []   ])
# =>     @[true false false false]
nil?cellularmitosisPlayground
(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
(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
  (->> (all-bindings)
       (keep (fn [sym]
               (when (string/has-prefix? "peg/" (string sym))
                 (string sym))))
       sort)

  # =>
  @["peg/compile"
    "peg/find"
    "peg/find-all"
    "peg/match"
    "peg/replace"
    "peg/replace-all"]

# from https://github.com/sogaiu/margaret/blob/master/tutorials/tutorial.janet
all-bindingsveqqqPlayground
# Note: the numbers may appear the same, but this is only due to printing truncation.
1                             # => 1
(math/next 1 math/inf)        # => 1
(< 1 1)                       # => false
(< 1 (math/next 1 math/inf))  # => true
math/nextcellularmitosisPlayground
# channels that can be used for communication between os threads
(def chan-a (ev/thread-chan 10))
(def chan-b (ev/thread-chan 10))

# one thread
(ev/thread
  (fiber/new
    (fn []
      (def msg "hi")
      (print "thread 1 sending: " msg)
      (ev/give chan-a msg)
      #
      (print "thread 1 received: " (ev/take chan-b))))
  :1
  :n)

# another thread
(ev/thread
  (fiber/new
    (fn []
      (print "thread 2 received: " (ev/take chan-a))
      #
      (def msg "peace")
      (print "thread 2 sending: " msg)
      (ev/give chan-b msg)))
  :2
  :n)

# expected output
#
# thread 1 sending: hi
# thread 2 received: hi
# thread 2 sending: peace
# thread 1 received: peace
ev/threadsogaiuPlayground
# To make a package conditionally export from another:

# A package can see whether another module was already imported,
# and conditionally act and e.g. import it and rebind/export some symbols:
(var- bla false)
(compwhen  (not (some |(string/has-suffix? "dataframes.janet" $) (keys module/cache)))
           (import declarative-dsls/dataframes :as "no" :export false)
           (set bla true))
(compwhen bla
          (def print-as-table no/print-as-table)
          (def select no/select)
          (def populate-df! no/populate-df!)
          (def rows->dataframe no/rows->dataframe)
          (def dataframe->rows no/dataframe->rows))

# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet
importveqqqPlayground
(nan? 1)  # => false
(nan? (/ 0 0))  # => true
(nan? (sqrt -1))  # => true
nan?cellularmitosisPlayground
(pp (all-bindings))
# => prints @[% %= * ... yield zero? zipcoll]

(def a "A")
(pp (all-bindings (curenv) true))
# => prints @[_ a] - only local bindings are listed
all-bindingspepePlayground
(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