JanetDocsPlaygroundI'm feeling luckyGitHub sign in

varfn



    macro
    boot.janet on line 2137, 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.


2 examplesSign in to add an example
Loading...
(var a nil)

(var b nil)

(varfn a
  [x]
  (if (zero? x)
    0
    (b x)))
    
(varfn b
  [y]
  (if (pos? y)
    1
    (a y)))
    
(a 1)
# => 1

(b 0)
# => 0
sogaiuPlayground
(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