Fork me on GitHub
#beginners
<
2016-10-26
>
pawel.kapala10:10:30

Hey. Is there core function that returns nil for any number of args, ie something like: (defn nil-foo [&args]) Or maybe I’m doing it wrong, as I need this for ((or (resolve 'function-that-might-not-exist) nil-foo) "args" "to" "resolved" "function”)

agile_geek11:10:41

Any reason you can't just wrap the fn call in a when?

agile_geek11:10:29

(when function-that-might-not-exist
   (function-that-might-not-exist "args" "to" "function"))

rauh11:10:29

@pawel.kapala Yeah when-let should be your friend

pawel.kapala11:10:14

that’s good, but when function-that-might-not-exist does not exist I get RuntimeException that function-that-might-not-exist does not exist 😆 and I want nil to be returned in that case, I wouldn’t (try) (catch) would I? :X

agile_geek11:10:14

I think I'd need to see more of your code. The when tests for if the symbol (`function-that-might-not-exist` in this case) is nil or not and only invokes the fn if it's not nil otherwise a when returns nil

agile_geek11:10:01

e.g.

(defn invoke-fn-that-may-not-exist
    [fn]
    (when fn
      (fn "output this")))

  (defn echo
    [x]
    (str x))

  (invoke-fn-that-may-not-exist echo) ;; => "output this"

   (invoke-fn-that-may-not-exist dummy) ;; => nil


pawel.kapala11:10:37

but in this case all the symbols you’re using are defined, maybe I should’ve wrote function-that-might-not-be-defined instead of function-that-might-not-exist 😉

pawel.kapala11:10:01

see this (`foo-bar` is not defined above):

(when foo-bar
                    (foo-bar "args" "to" "resolved" "function"))

CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo-bar in this context, compiling:(/private/var/folders/pq/jrskhrwn5jl_yknn7jc5jkt00000gn/T/form-init8702595223277930847.clj:1:1) 

agile_geek11:10:11

dummy is not defined in the edited example

pawel.kapala11:10:00

ok I get you now 🙂 dummy is passed as fn param and is bound inside, that’s why it works 😆

agile_geek11:10:04

if you don't have a symbol defined you can't reference it although you could use your resolve in the when or even better a when-let

pawel.kapala11:10:22

thanks @agile_geek! I’m gonna go with when as it’s simple and clear.