From the clojure docs, the function *'
:
Returns the product of nums. (*') returns 1. Supports arbitrary precision. See also: *
I understand the use cases for arbitrary precision as explained in the example provided:
;; great so it gives the same results as *.
;; not quite check this out
(* 1234567890 9876543210)
;; ArithmeticException integer overflow
(*' 1234567890 9876543210)
;;=> 12193263111263526900N
However, the (*') returns 1
does not seem to have any use at all since you can just specify the explicit value. In the same example provided:
;; there is an implicit 1
(*')
;;=> 1
;; the implicit 1 comes into play
(*' 6)
;;=> 6
I had thought that perhaps it would be useful if the second argument is not defined, perhaps nil
but:
(*' 6 nil)
Throws a NullPointerException
.
Why would you use (*' 6)
over 6
and (*')
over 1
?
*
and *'
are equivalent in this regard:
first of all - how would you otherwise handle the []
and [x]
cases in *
?
The source as it is written makes the most sense and AFAIK is mathematicly correct (identity value and such).
user=> (source *)
(defn *
"Returns the product of nums. (*) returns 1. Does not auto-promote
longs, will throw on overflow. See also: *'"
{:inline (nary-inline 'multiply 'unchecked_multiply)
:inline-arities >1?
:added "1.2"}
([] 1)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (multiply x y)))
([x y & more]
(reduce1 * (* x y) more)))
second, it makes it much more robust in cases other then manually writing (* 2 4)
. I wouldn't write (*)
to mean 1
.
Like the following - see the single element and emtpy vector on pos 3 and 4.
user=> (map (partial apply *) [[2 2] [2 3 2] [6] []])
(4 12 6 1)