Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(var a 0 )
(assert (pos? a ) "A is definitely not positive" )
# error: A is definitely not positive
# in assert [boot.janet] on line 149, column 11
# in _thunk [repl] (tailcall) on line 3, column 1 (map boolean? [true false nil 0 1 ])
# => @[true true false false false]
# To make "stub functions" in an image, solving c interop problems like this:
# I hoped that I would be able to unmarshal the runtime.jimage, load my C functions,
# and Janet would use my app's functions instead of the stubs. However, during
# compilation, Janet seems to precompile all the top-level function calls. This
# means hotswapping the functions does nothing, and the Janet code continues to
# use the stubs instead.
#
# I was hoping that there would be some way of telling Janet "don't precompile
# this please", either a (defstubn ...) or the ability to do
# (defn internal/redraw-ui [opts] (dyn)), where (dyn) is some abstract type that
# Janet can't precompute directly.
# https://github.com/janet-lang/janet/issues/1642
# pretend this is the stub function we want to replace
(defn greet [] (print "hello world" ))
# make-image-dict determines which values will be filled in at
# "deserialization" time
(put make-image-dict greet 'greet )
(def pretend-env @{
'some-function (fn [] (greet ) (greet ))})
(def compiled (make-image pretend-env ))
# notice: the string "hello world" does not appear in the compiled result
(pp compiled )
# if we just try to (load-image compiled) at this point, it will raise,
# because we haven't specified what to do about 'greet
# we have to make an entry in the load-image-dict. note that
# it does not have to be the same value!
(put load-image-dict 'greet (fn [] (print "something else!" )))
# now we can call load-image
(def pretend-env-reparsed (load-image compiled ))
# and when we run this, we will see the replacement that we gave
((pretend-env-reparsed 'some-function ))(def cc (ev/thread-chan 99 )) # Sets up a message channel.
(def ww (filewatch/new cc )) # Creates the watcher.
(filewatch/add ww "ftest.janet" :all ) # File to watch.
(filewatch/listen ww ) # Start filewatcher listening for changes.
(ev/spawn-thread # Spawn thread to catch change events.
(print "Waiting..." )
(forever (pp (ev/take cc ))))(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 (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 ] (math/hypot 1 1 ) # => 1.41421
(math/sqrt 2 ) # => 1.41421
(range 12 ) # => @[0 1 2 3 4 5 6 7 8 9 10 11]
(range 0 12 ) # => @[0 1 2 3 4 5 6 7 8 9 10 11]
(range 0 12 1 ) # => @[0 1 2 3 4 5 6 7 8 9 10 11]
(range 0 12 2 ) # => @[0 2 4 6 8 10]
(range 0 12 3 ) # => @[0 3 6 9]
(range 0 12 4 ) # => @[0 4 8]
(range 0 12 6 ) # => @[0 6]
(buffer/bit (buffer/new-filled 1 48 ) 4 )
# => true
(empty? {})
# => true
(defn inc [x ] (+ x 1 ))
(defn inv [x ] (* x -1 ))
(defn sq [x ] (* x x ))
(-> 2 inc ) # -> 3
(-> 2 inc inc ) # -> 4
(-> 2 inc inc inv ) # -> -4
(-> 2 inc inc inv sq ) # -> 16
(var x 1 )
(+= x 2 3 4 )
# x = 10 # Contrived example returning the variadic arguments passed in.
(defmacro example-macro [& args ] ~(tuple ,;args ))
(example-macro 1 2 3 ) # => (1 2 3)
(def args [1 2 3 ])
# `apply` is for functions, but there's always `eval`.
(assert (= (example-macro 1 2 3 )
(eval ~(example-macro ,;args ))))(var a 1 )
a # => 1
(inc a ) # => 2
a # => 1
(++ a ) # => 2
a # => 2
(def b @"" )
(xprint b "HOHOHO" )
# => nil
b
# => @"HOHOHO\n" # From: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/main.janet#L30
# where in .env you have:
# DATABASE_URL=examples-db.sqlite3
# PORT=9025
(defn main [& args ]
(joy/db/connect (joy/env :database-url ))
(server app (joy/env :port ) "0.0.0.0" )
(joy/db/disconnect ))(dyn :pretty-format )
# => "%.20Q" math/e
# => 2.71828 (string/check-set "0123456789abcdef" "deadbeef" ) # => true (describe @[:a :b ]) # => "<array 0x55EC375CF440>"