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

# Gathers all of the calls of "declare-" into a table from the project.janet file at path
(defn capture-declares [path] 
    (def *capture* @{})
    (defn append-capture [name & arg]
      (update *capture* name (fn [val] (array/push (or val @[]) ;arg))))
    (defn only-captures [form] 
      (when (string/has-prefix? "declare-" (string (form 0)))
        (append-capture (form 0) (slice form 1))
        form)
      nil)
    (dofile path :expander only-captures)
    *capture*)
dofileyumaikasPlayground
(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 nil? [nil  0     false []   ])
# =>     @[true false false false]
nil?cellularmitosisPlayground
# 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
# access tuple values

(def t [:a :b :c]) # test tuple

(first t)
# => :a

(last t)
# => :c

(slice t 0 2)
# => (:a :b)

# Index as function
(0 t)
# => :a

# Tuple as function
(t 0)
# => :a

# With destructuring
(let [[_ b _] t] 
    b)
# => :b

# With pattern matching
(match t
    [:a _ :d] (print "does not match")
    [:a b :c] (print b)
    _         (print "anything"))

# => :b
tupleleobmPlayground
# janet 1.10.1

# non-numeric values appear to always be greater than numeric values
(< math/inf nil)      # -> true
(< math/inf true)     # -> true
(< math/inf false)    # -> true
(< math/inf "hello")  # -> true
(< math/inf :heyo)    # -> true
(< math/inf (fn []))  # -> true
(< math/inf {:a 1})   # -> true

# non-numeric values also follow an ordering.
# rearranging any of these values turns the result false:
(< nil false true "a" "b" :a :b [] [1] [1 1] [2] {} {:a 1} (fn []))  # -> true
<cellularmitosisPlayground
(->
  {:a [1 2 3] :b [4 5 6]}
  (get :a)
  (sum)
  (string " is the result"))
# -> "6 is the result"

# same as:
(string (sum (get {:a [1 2 3] :b [4 5 6]} :a))" is the result")
->felixrPlayground
(array 1 2.3 :a "foo" true nil [] {} (fn []))
# => @[1 2.3 :a "foo" true nil () {} <function 0x7FB2A3F02170>]
arraycellularmitosisPlayground
(cmp 0.0 0)
# => 0
cmpsogaiuPlayground
(array/slice [1 2 3])

#=> @[1 2 3]
# good way to convert tuple to array
array/slicepepePlayground
(print :hi ` ` "there" @`` `` @"mate")
# => nil
printsogaiuPlayground
(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
(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
(def f (file/temp))
(file/write f "ok stuff")
(file/seek f :set 0)
(file/read f :all)

# N.b. file/temp's file is not accessible outside of the process
file/tempveqqqPlayground
(do
  (def coll @[])
  (forv i 0 9
    (array/push coll i)
    (+= i 2))
  coll)
# => @[0 3 6]
forvsogaiuPlayground