Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
# janet 1.10.1
(= [1 1 ] [1 1 ]) # => true
(= [1 1 ] [2 3 ]) # => false
(= [1 1 ] @[1 1 ]) # => false
(= [1 1 ] @[2 3 ]) # => false
(= @[1 1 ] @[1 1 ]) # => false
(= @[1 1 ] @[2 3 ]) # => false
(deep= [1 1 ] [1 1 ]) # => true
(deep= [1 1 ] [2 3 ]) # => false
(deep= [1 1 ] @[1 1 ]) # => false
(deep= [1 1 ] @[2 3 ]) # => false
(deep= @[1 1 ] @[1 1 ]) # => true
(deep= @[1 1 ] @[2 3 ]) # => false
(os/dir "./" ) # => @[]
(os/shell "touch foo bar baz" ) # => nil
(os/dir "./" ) # => @["foo" "bar" "baz"]
(def a @[23 42 ])
(array/clear a )
(pp a )
# => prints @[] (def buf-bytes 12 )
(var new-buffer (buffer/new buf-bytes )) #--> @""
(buffer/push new-buffer "hello, world" ) #--> @"hello, world" # slice with strings
(slice "Hello" 1 ) # => "ello"
(slice "Playing" 0 -4 ) # => "Play"
(slice "Playing" -4 ) # => "ing"
# slice with keyword
(slice :hello 1 ) # => "ello" (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 ] (describe @[:a :b ]) # => "<array 0x55EC375CF440>" (string/find "a" "abcdefa" 1 )
# not yet documented start ^ position
# => 6 ### read a file line by line ###
(let [f (file/open "filename.txt" )] # bind an open file handle to "f"
(while true # loop until "break" is called
(let [l (file/read f :line )] # bind a line of the file to "l"
(if l
(print l ) # if l is truthy print l
(break )))) # if l is not break from loop
(file/close f )) # close the file handle
# same as above but using "with"
# this means there's no need to
# call file/close, also replace
# let with var
(with [f (file/open "filename.txt" )]
(while true
(var l (file/read f :line ))
(if l
(print l )
(break ))))# 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" (string/format "%f" (os/clock )) # => "1627746483.991000" (def f (ev/go (coro "world" ))) # coro is great for fast fiber creating
(ev/sleep 0.0001 ) # give ev a chance in the REPL, remove in file
(fiber/last-value f ) # "world"
(neg? -42 ) # => true
(map neg? [-1 0 1 ]) # => @[true false false] ## Quadratic Formula
(defn qform
"Use the quadratic formula to solve for x. Returns all real solutions."
[a b c ]
(def det (- (* b b ) (* 4 a c )))
(def factor (/ 0.5 a ))
(cond
(neg? det ) []
(zero? det ) [(* factor (- b ))]
(let [root-det (math/sqrt det )]
[(* factor (- (- b ) root-det )) (* factor (+ (- b ) root-det ))])))
(qform 1 4 3 ) # -> (-3 -1)
(def b @"" )
(def v (* 1000 (math/random )))
# => 912.753 can differ for you
(xprintf b "Value reached level %f" v )
# => nil
b
# => @"Value reached level 912.752790\n"