How to use Java classes, or Clojure libraries

For the darts exercise, I want to use dist =: hypot x y

In Clojure I’d use

  • (Math/hypot x y) for java.lang.Math.hypot, or
  • (require '[clojure.math :as math]) and (math/hypot x y)

How do I do that in yamlscript.

math/hypot(x y) is what I did

Ah.

I see this also works

defn score(x y):
  dist =:
    math/hypot: x y
  # ...
1 Like

Other variants:

dist =: math/hypot(x y)
dist =: x.math/hypot(y)
dist =: y.math/hypot(x _)
dist =:
  y.math/hypot x: y
dist =:
  y.math/hypot _ y: x
dist =:
  ? y.math/hypot
  : x y

none of them are better, but just showing TMTOWTDI YS flexibility…

YS has the most common namespaces available without needing to require them: math, str, fs, io etc.

Also the most common Java classes are imported.

You also could have done Math/hypot(x y) (Math as Java class rather than math as Clojure ns)

1 Like