Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
# 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 (def [pipe-r pipe-w ] (os/pipe ))
(:write pipe-w "hello" )
(:read pipe-r 5 )
# => @"hello"
# send the string "hello" into the writable stream
# part and read it back from the readable one
(var x 5 )
(%= x 3 )
(pp x ) # 2 (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` (string/find "needle"
"hay hay hay needle hay" )
# => 12 (defn dataframe
``Create an empty dataframe with the given columns``
[& columns ]
(tabseq [c :in columns ] c @[]))(string/repeat "moshi" 2 ) # => "moshimoshi" (disasm (fn []) :bytecode )
# => @[(retn)]
(os/shell "uptime > /tmp/uptime.txt" ) # => 0
(slurp "/tmp/uptime.txt" )
# => @"22:33 up 5 days, 9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt" ) # => nil # sh/$ can't expand "~" so you must build it:
# (def key "D8E4DB18BF87FLEW7402BBE3AA91B16F4A65C4C9") # use your gpg key ID
(defn copy-and-encrypt-password-store [key-id ]
(with [out (file/open "pass-backup.tar.gz.gpg" :w )]
(sh/$ tar -czf - ,(string (os/getenv "HOME" ) "/.password-store" )
| gpg --encrypt --recipient ,key-id > ,out )))
# tar -cz ~/.password-store/ | gpg --encrypt --recipient YOUR_KEY_ID > pass-backup.tar.gz.gpg (fiber/root ) # => <fiber 0x562FF22F10A0> your hex number will differ (file/open "iamfile.txt" :r )
(file/open "iamfile.txt" :w )
(file/open "iamfile.txt" :a )(filter (partial string/has-prefix? "z" ) (all-bindings )) # => @[zero? zipcoll] (defn walker
`Simple walker function, that prints non-sequential
members of the form or prints "Sequence" and walks
recursively sequential members of the tree.`
[form ]
(if (or (indexed? form ) (dictionary? form ))
(do (print "Sequence" )
(walk walker form ))
(print form )))
(walk walker [[[[0 1 3 ]] 16 7 [3 [3 5 ]] 3 4 ] 1 [3 4 ]])
# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
$ janet -e '(os/exit 42 )' ; echo $?
42