4 ms·
> Clojure doesn't provide built-in function for exponential operation It does actually: (Math/pow 2 3) ;;=> 8
by didibus 5y ago
> Clojure doesn't provide built-in function for exponential operation
It does actually:
(Math/pow 2 3)
;;=> 8
- mapgrep 5y ago“Math” here is short for “java.lang.Math” though. Technically that’s interop rather than Clojure per se. /pedantic
- Fellshard 5y agoIf you remove the Java SDK from Clojure's foundation, you're losing a lot of its power. The interop is part of that foundation.
- mapgrep 5y agoFor sure, but “Clojure doesn't provide built-in function for exponential operation” is still an objectively true assertion. If your point is that it’s wrong in spirit, I totally agree, and I think this is yet another example of the limits of this “cheat sheet” approach. If you’re coming from another language, the concept of easy, well integrated interop with a host language platform would be fairly foreign, so the importance of an available Java method, and the fact that such methods are frequently used as core parts of the language, would be totally lost on you.
- Fellshard 5y agoThink it might just be quibbling on the nature of 'built-in' at that point.
- mapgrep 5y agoWell yes hence the “/pedantic” tag. I was quibbling with someone who went out of their way to “correct” a true fact. Quibbling with quibbling if you will. Sort of like you’re doing. But hey they started it ;)
- didibus 5y agoI think for Math/ it's a bit more fuzzy, because it doesn't need to be aliased explicitly, it's always available to use as it is automatically imported as Math. And this is then consistent in ClojureScript as well.
- jffry 5y agoThe very next sentence of the article points out that you can leverage Clojure's interop to invoke Math.pow(), but that's still limited to casting its arguments down to java.lang.Double and suffering the limits therein If you instead used the library org.clojure/math.numeric-tower [1] you can get much more precise answers, for example # start a repl with an extra dep $ clj -Sdeps '{:deps {org.clojure/math.numeric-tower {:mvn/version "0.0.4"}}}' user=> (require 'clojure.math.numeric-tower) nil user=> (Math/pow 33 33) 1.2911004008776103E50 user=> (type *1) java.lang.Double user=> (clojure.math.numeric-tower/expt 33 33) 129110040087761027839616029934664535539337183380513N user=> (type *1) clojure.lang.BigInt [1] https://github.com/clojure/math.numeric-tower https://github.com/clojure/math.numeric-tower
- didibus 5y ago> The very next sentence of the article points out that you can leverage Clojure's interop to invoke Math.pow() Hum, ok, to my defense it's not the very next sentence, it's much later at the very end when talking about Method invocation, so I hadn't read that far. +1 for linking to the numeric tower though
- jffry 5y agoMy apologies. I searched for Math/pow, saw that the sentence there started with "Clojure doesn't", and missed that we were looking at two different parts of the document :)
- dig1 5y agoYou need to be familiar with Java types and differences between them (Integer, BigInteger) and how Math.pow() behaves with different types. This is more up to Java, rather than Clojure. No need to pull the library for this, Clojure already has (biginteger) for that: user=> (.pow (biginteger 33) 33) 129110040087761027839616029934664535539337183380513
- kazinator 5y agoDidn't Clojure use the N suffix for big integers at one point? I don't see one on that output.
- Oreb 5y ago`Math/pow` is not a function. If it were, you would be able to do things like this: (map (partial Math/pow 2) [0 1 2 3])
- dig1 5y ago"Math/pow" is actually a function on JVM level. Clojure functions are objects with special callable method. This will work: (map (partial #(Math/pow %1 %2) 2) [0 1 2 3])