Search code examples
clojureread-eval-print-loop

Clojure - Evaluating returned function from higher order function


If I run a higher order function in the repl, or something that returns a function, like below, is there any way to later retrieve the returned function (the value that is returned by the repl) and evaluate it?

user> #(% 5 5)
#function[user/eval13160/fn--13161]

To explain the reason for the question, I'm playing around with http-kit, and ran the function run-server. It was only after execution that I realized that the function returns a function that is required to stop the server, and so I've been trying to figure how to use the function that was returned.


Solution

  • Yep, you can access previous REPL values with *1:

    user=> #(% 5 5)
    #object[user$eval3$fn__4 0x487db668 "user$eval3$fn__4@487db668"]
    user=> (*1 +)
    10
    

    There's also *2 and *3 for trailing values from previous evaluations, and *e for prior exceptions.

    You could also def the result:

    (def my-fn #(% 5 5))
    (def my-fn *1) ;; or do it later