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

(empty? :)
# => true
empty?sogaiuPlayground
(->> 
  (string/split " " "this  is an  example yes, an example")
  (filter (complement empty?))
  (frequencies))
# -> @{"is" 1 "example" 2 "yes," 1 "an" 2 "this" 1}

# same as:
(frequencies
  (filter
    (complement empty?)
    (string/split " " "this  is an  example yes, an example")))
->>felixrPlayground
(map string/from-bytes "Hello, world!")  # => @["H" "e" "l" "l" "o" "," " " "w" "o" "r" "l" "d" "!"]
mapGrayJackPlayground
#1) start janet normally, work in the repl, encounter something to debug:

(debug)
# debug: 
#   in thunk [repl] (tail call) on line 4, column 1
# nil
# repl:5:> 

#2) because the -d flag was missing on start, debug is skipping back out into the repl. now i really DO want to debug here and now, without restarting the vm:

(dyn *debug*)
# => nil

#3) we can just set the flag ourself and thus activate the debugger:
(setdyn *debug* :anything_truthy)


(debug)

# debug: 
#   in thunk [repl] (tail call) on line 2, column 1
# entering debug[1] - (quit) to exit
# debug[1]:1:>
*debug*kamisoriPlayground
(let [a 1 b 2 c 3] (+ a b c))  # => 6

(let
  [a 1
   b (+ a 1)
   c (+ b 1)]
  (+ a b c))  # => 6
letcellularmitosisPlayground
(map dictionary? [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         true          ]

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

(map table?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     false        true          ]
table?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
(os/dir "./")                   # => @[]
(os/shell "touch foo bar baz")  # => nil
(os/dir "./")                   # => @["foo" "bar" "baz"]
os/dircellularmitosisPlayground
(tuple 1 2.3 :a "foo" true nil [] {} (fn []))
# =>  (1 2.3 :a "foo" true nil () {} <function 0x7FB2A3D030B0>)
tuplecellularmitosisPlayground
(def b @"")
(xprin b "HOHOHO")
# => nil
b
# => "HOHOHO"
xprinpepePlayground
(defn test
  [x]
  (cond
    (> x 10) "Pretty big!"
    (< x 5) "Quite small"
    "Medium size"))

(test 40) # => "Pretty big!"
(test 2) # => "Quite small"
(test 6) # => "Medium size"
condpingiunPlayground
(math/atan 1)    # 0.785398
(math/atan 0.5)  # 0.463648
math/atanbtbytesPlayground
# use (dyn :args) to get the value of dynamic binding *args*
(let [args (dyn :args)]
  (if (= "-h" (get args 1))
    (print "Usage: janet args.janet [-h] ARGS..")
    (printf "command line arguments:\n %q" args)))
*args*AndriamanitraPlayground
(os/shell "echo bar > /tmp/foo")
(with
  [file-handle
   (file/open "/tmp/foo")
   (fn [fd] (file/close fd))]
  (file/read file-handle :all))  # => @"bar\n"
withcellularmitosisPlayground
(let [c0 (ev/chan)
      c1 (ev/chan 1)]
  [(ev/capacity c0) (ev/capacity c1)])
# => '(0 1)
ev/capacitysogaiuPlayground