I only need i for use in an algorithm. I feel like importing clojure.math is overkill for such a task.
Why?
I have no need for complex results, infact there is no need for a real part with the imaginary part. In my implementation, I only use one value at a time and never combine the two, except for multiplication. The output ends up with no reference to i, the imaginary part is only needed to see the changes in sign in the computation.
Simply put, it would be nice if there was a way to define i as:
(def i (....) )
such that (* i i) equals -1.
If you want
(* i i)to evaluate to-1we’ll need to prepare a macro.The macro expands to an ordinary multiplication and it shouldn’t interfere with multiplication of real numbers.
Is a macro necessary? Without a macro
is present in(* i i) would get evaluated. Since they weren’t defined it would cause a compile-time error. As suggested in the question, we could defineias a value which*knows how to handle. Despite that being possible, it would still be evaluated at runtime. A clear advantage of a macro is the fact that it’s evaluated during compilation and replaced with an ordinary call toclojure.core/*as shown in examples above. Simply put, it’s fast.