Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
Example usage
janet:1:> (def buf @"ABCDE" )
@"ABCDE"
janet:2:> (buffer/slice 0 )
error: bad slot #0, expected string|symbol|keyword|buffer, got 0
in buffer/slice
in _thunk [repl ] (tailcall ) on line 2 , column 1
janet:3:> (buffer/slice buf 0 )
@"ABCDE"
janet:4:> (buffer/slice buf 0 1 )
@"A"
janet:5:> (buffer/slice buf 0 -1 )
@"ABCDE"
janet:6:> (buffer/slice buf 1 3 )
@"BC"
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt" ) # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt" )) # => @["hello" "world" ""]
(defn slurp-lines [path ]
(string/split "\n" (slurp path )))
(slurp-lines "foo.txt" ) # => @["hello" "world" ""]
(string/join @["hello" "world" "" ] "\n" ) # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" "" ] "\n" ))
# The contents of foo.txt and foo2.txt are now identical.
(defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))
(spit-lines "foo3.txt" (slurp-lines "foo.txt" ))
# The contents of foo.txt and foo3.txt are now identical.
(array/pop (range 12 )) # => 11 (string/repeat "moshi" 2 ) # => "moshimoshi" (string/reverse "hello" ) # => "olleh" (any? [false false nil ]) => nil
(any? [false false nil 1 ]) => 1
(any? [false false nil true ]) => true (interleave [:a :b :c ]
[1 2 3 ]
["x" "y" "z" ])
# => @[:a 1 "x" :b 2 "y" :c 3 "z"] # janet 1.10.1
(all pos? [1 2 3 ]) # => true
(all pos? [1 2 3 -4 ]) # => false
(all pos? [1 2 3 0 ]) # => false
(all (partial string/has-prefix? "a" ) ["aa" "ab" ]) # => true
(all (partial string/has-prefix? "a" ) ["aa" "ab" "bb" ]) # => false
(all truthy? [1 2 ]) # => true
(all truthy? [1 2 3 ]) # => true
(all truthy? [1 2 nil 3 ]) # => false
(all truthy? [1 false 2 nil 3 ]) # => false
(all (fn [x ] x ) [1 2 ]) # => 2
(all (fn [x ] x ) [1 2 3 ]) # => 3
(all (fn [x ] x ) [1 2 nil 3 ]) # => nil
(all (fn [x ] x ) [1 false 2 nil 3 ]) # => false
(ev/spawn (os/sleep 1 ) (print "Hard work is done!" ))
# prints "Hard work is done!" after one second
# this is the easiest way to put some forms on the event loop
# but do not forget REPL is blocking, for now, so run the example with `janet -e` (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 ] (table ;[:a 1 :b 2 :c 3 ])
# => @{:a 1 :b 2 :c 3}
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt" ) # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt" )) # => @["hello" "world" ""]
(defn slurp-lines [path ]
(string/split "\n" (slurp path )))
(slurp-lines "foo.txt" ) # => @["hello" "world" ""]
(string/join @["hello" "world" "" ] "\n" ) # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" "" ] "\n" ))
# The contents of foo.txt and foo2.txt are now identical.
(defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))
(spit-lines "foo3.txt" (slurp-lines "foo.txt" ))
# The contents of foo.txt and foo3.txt are now identical.
(os/execute
["/usr/bin/bash" "-c" "set" ]
:e
@{"SOME" "value"
"OTHER" "one" })
# => 0
# execute bash and prints environment variables
# which contains SOME=value and Other=one (spit "/tmp/hello.sh" "#!/bin/bash\necho 'Hello from Bash!'\n" )
(os/chmod "/tmp/hello.sh" "rwx------" )
(os/setenv "PATH" (string (os/getenv "PATH" ) ":/tmp" ))
(os/shell "hello.sh" )(scan-number "111" 2 )
# => 7