Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform-independent math functions in Clojure(script)

How to access things like sin, cos or PI in a pure Clojure way?

For example, if I want to write a pure library, I mustn’t use anything like (.PI Math) (Java) or (.‑PI js/Math) (JS).

like image 233
Profpatsch Avatar asked Jan 16 '14 11:01

Profpatsch


1 Answers

The easiest way is to use Cljx: https://github.com/lynaghk/cljx

With it you can write something like:

(* 5 #+clj (.PI Math) #+cljs (.‑PI js/Math))

and have this code compiled properly to Clojure and ClojureScript.

As far as I know there’s no better way to write one code to be runned as Clojure/ClojureScript.

There are some plans to include platform detection in Clojure itself but I think it’s not ready yet.

like image 198
yonki Avatar answered Nov 03 '22 09:11

yonki