Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
# Note that the earlier sort func is executed later
(update @{:data @[:cherry :orange ]} :data (comp sort |(array/push $ :apple )))
# => @{:data @[:apple :cherry :orange]} (math/sqrt 9 ) # => 3 (import joy )
# Given a handler
(defn handle-func [request ]
(joy/text/plain (string "Hi " ((request :params ) :name ))))
# joy/route will attach it to a route
(joy/route :get "/hi/:name" :handle-func )
# without needing to pass the route(s) to joy/app
(def app (joy/app {}))
(joy/server app 9002 )
# visit at localhost:9002/hi/bob # From the REPL, always nil
repl> (dyn *current-file* )
nil
# As an expression to the CLI, always nil
$ janet -e '(pp (dyn *current-file* ))'
nil
# Given the file /tmp/janet/test.janet with the following contents:
(pp (dyn *current-file* ))
# Note how the value reflects the given file path
~ $ janet /tmp/janet/test.janet
"/tmp/janet/test.janet"
/tmp/janet $ janet ./test.janet
"./test.janet"
/tmp/janet $ janet test.janet
"test.janet"
/tmp/janet $ janet ../janet/test.janet
"../janet/test.janet"
# The following are equivalent - the first is preferred.
# The key insight is that dynamic bindings are just keyword/value
# pairs in the current fiber's environment table.
(dyn *current-file* )
(dyn :current-file )
((curenv ) *current-file* )
((curenv ) :current-file )# Expanding the example from https://janetdocs.org/core-api/stdin
# We use argparse to provide automatically add flags
# --help comes for free, try it!
(import spork/argparse )
(defn main [& ]
(let [opts (argparse/argparse "Sum numbers from files, stdin or data."
"multiply" {:kind :option :short "m" :default "1" :map scan-number :help "Multiply the result" }
"verbose" {:kind :flag :help "Print the input first" }
:default {:kind :accumulate })
_ (unless opts (os/exit 0 )) # lest it crash on help etc.
args (or (opts :default ) []) # give empty? empty array of :default is nil
data (cond (empty? args ) (file/read stdin :all )
(os/stat (first args )) (slurp (first args ))
(string/join args " " ))
total (* (opts "multiply" ) (sum (flatten (parse-all data ))))]
(if (opts "verbose" ) (print "Data: " data ))
(print total )))(net/address "0.0.0.0" 80 ) # => <core/socket-address 0x55CABA438E90>
(net/address "0.0.0.0" 8989 ) # => <core/socket-address 0x55CABA439980>
(update @{:data @[:cherry :orange ]} :data (comp sorted |(array/push $ :apple )))
# => {:data @[:apple :cherry :orange]}
# note that all data structures must be mutable with @
(def c @{:size :small :roast :dark :add-ins @[:soy :almond :soy ]})
(defn add! [coffee extra ]
(update coffee :extras (comp sort |(array/push $ extra ))))
(add! c :chocolate )(reduce2 + [1 2 3 ]) # -> 6
(accumulate2 + [1 2 3 ]) # -> @[1 3 6] #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:> (cmp [1 2 ] [1 2 3 ])
# => -1
(in "yo" 0 )
# => 121
(os/perm-string 420 ) # => "rw-r--r--"
(os/perm-string 8r644 ) # => "rw-r--r--"
(os/perm-string 493 ) # => "rwxr-xr-x"
(os/perm-string 8r755 ) # => "rwxr-xr-x"
(distinct [1 1 2 3 ]) # => @[1 2 3]
(distinct "hello" ) # => @[104 101 108 111]
(string/from-bytes (splice (distinct "hello" ))) # => "helo"
(map tuple ["a" "b" "c" "d" ] [1 2 3 4 ] "abcd" )
# => @[("a" 1 97) ("b" 2 98) ("c" 3 99) ("d" 4 100)] (even? 1 ) # => false
(map even? [1 2 3 4 ]) # => @[false true false true]