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 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         ]
buffer?cellularmitosisPlayground
(string/join @["alice" "bob" "eve"] "\t")
# => "alice\tbob\teve"
string/joinsogaiuPlayground
# Upgrade mutates, but I wasn't sure whether it'd matter using the pure sorted or mutative sort. So I
# did a simple test.

(import spork)
# create data before test
(def d @{:data (range 10000000 0 -1)})
(spork/test/timeit (update d :data sort)) # 4.32930135726929 seconds and 87.4 MB

(def d @{:data (range 10000000 0 -1)}) # 87.4 MB
(spork/test/timeit (update d :data sorted)) # 4.49482655525208 seconds and 167 MB

# Where did those memory numbers come from? With only the data, the Janet process 
# uses 87.4 MB. Timewise, they take the same amount of time but on starting, sorted
# prepares memory equivalent to its input. To check ram within Janet:
(def pid (os/getpid)) # => "/proc/367537/statm"
(def statm-path (string "/proc/" pid "/statm")) # => 367537
(slurp statm-path) # => @"40444 40038 710 82 0 39426 0\n"

# Collecting garbage:
(gccollect)
(slurp statm-path) # => @"20912 20503 695 82 0 19894 0\n"
spork/test/timeitveqqqPlayground
(defn get-time-str []
  (let [{:hours h :minutes m :seconds s} (os/date)]
    (string h ":" m ":" s)))

(get-time-str) => "23:18:16"
os/datetupini07Playground
# nested iteration
(loop [a :in [100 200 300]
       b :in [1 2 3]]
   (print (+ a b)))

# 101
# 102
# 103
# 201
# 202
# 203
# 301
# 302
# 303
looperichaneyPlayground
(var a 2)
a        # => 2
(dec a)  # => 1
a        # => 2
(-- a)   # => 1
a        # => 1
--cellularmitosisPlayground
(* 2 3)      # -> 6
(* 2 3.3)    # -> 6.6
(* 2.2 3.3)  # -> 7.26

(def pi 3.14159)
(* 2 pi)  # -> 6.28318

(* 2)  # -> 2
(*)    # -> 1

(* 2 3 4)             # -> 24
(apply * [2 3 4])     # -> 24
(* (splice [2 3 4]))  # -> 24
(* ;[2 3 4])          # -> 24
(def a [2 3 4])
(* ;a)                # -> 24
*cellularmitosisPlayground
# janet 1.10.1

# note that os/cd does not appear to update PWD in the shell's environment.
(os/cwd)           # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(os/cd "/tmp")     # => nil
(os/cwd)           # => "/private/tmp"  (on Apple, /tmp is actually /private/tmp)
(os/getenv "PWD")  # => "/Users/cell/tmp"
os/cdcellularmitosisPlayground
# Get a new and different random number each time you run your program.
(math/seedrandom (os/cryptorand 8))
(math/random)
math/randomuvtcPlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
# uses the fn shorthand
(take-until |(< 3 $) [0 1 2 3 4 5 6 7 8])
# (0 1 2 3)

# uses partial
(take-until (partial < 3) [0 1 2 3 4 5 6 7 8])
# (0 1 2 3)
take-untilleobmPlayground
(frequencies ["a" "a" "b" "c" "d" "d"])
# => @{"c" 1 "d" 2 "a" 2 "b" 1}


(frequencies {:a "Bob"
              :b "Joe"
              :c "Mat"
              :d "Bob"})
# => @{"Bob" 2 "Joe" 1 "Mat" 1}

(frequencies "Hallo World")
# => @{108 3 114 1 100 1 87 1 97 1 32 1 111 2 72 1}
frequenciesleobmPlayground
(each item [1 2 3 4] (print item))
# prints 1
# prints 2
# prints 3
# prints 4
# => nil
eachpepePlayground
(doc string)


# =>    cfunction
# =>    ../src/core/corelib.c on line 349, column 1
# =>
# =>    (string & xs)
# =>
# =>    Creates a string by concatenating the elements of xs together. If
# =>    an element is not a byte sequence, it is converted to bytes via
# =>    describe. Returns the new string.


# nil
docjgartePlayground