JanetDocsPlaygroundI'm feeling luckyGitHub sign in

varfn



    macro
    boot.janet on line 1957, column 1

    (varfn name & body)

    Create a function that can be rebound. varfn has the same signature 
    as defn, but defines functions in the environment as vars. If a var 
    'name' already exists in the environment, it is rebound to the new 
    function. Returns a function.


1 exampleSign in to add an example
Loading...
(defn greet-me [] (print "hey programmer!"))
(defn greet-stranger [] (print "hey stranger!"))

(varfn greet [] (greet-me))
(greet) # prints "hey programmer!"
(varfn greet [] (greet-stranger))
(greet) # prints "hey stranger!"

# kind of analogous to (def greet (fn [] (some-thunk))), but with built-in
# sugar to automatically update 'greet' if varfn is called with a name
# already bound
wrqPlayground