Success! I've included the final solution here for completeness and
community contribution.
1. Thanks, Didier-- your last bit got me on the right path.
2. Thanks, Gregg-- it turns out gen-class does recognize that syntax, if
quoted, like so:
#^{:static true} [score ["[Ljava.lang.Number;" "[Ljava.lang.Object;"]
"[[Ljava.lang.Number;"]
I preferred to put the data manipulation into the clojure layer to ensure
that it's acting as consistent resource provider, allowing a "natural"
interpretation in the host language (in this case, JavaScript). Note that
in addition to interpreting the input into a Clojurey frame, it also
reframed the output back into a JavaScripty frame.
Here's the final solution:
*Clojure*
(ns score
"docs omitted"
(:gen-class
:name score
:prefix "javascript-compat-"
:methods [
#^{:static true} [
score
["[Ljava.lang.Double;" "[Ljava.lang.Object;"]
clojure.lang.PersistentObject]]))
(defn score
"do mathy stuff"
[subject context]
(do-something-fancy subject context))
(defn javascript-compat-score
[subject context]
(into []
(score (into [] subject)
(mapv #(into [] %) context))))
*JavaScript*
const java = require('java');
function genFixture () {
return new Array(12).fill(parseFloat(Math.random().toFixed(2)));
}
console.log("Generating fixtures...");
var data = genFixture();
var contex = new Array(10).fill(1);
context.forEach((d,i) => { context[i] = genFixture(); });
// This produces, for example, the following fixtures:
// data: [ 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89,
0.89, 0.89 ]
// context: [
// [ 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35 ],
// [ 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
0.85 ],
// ...
// [ 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31,
0.31 ]]
java.classpath.push("./java/score-0.2.1-standalone.jar");
console.log("Importing class ...");
var score = java.import('score');
console.log("Attempting to run score.scoreSync(data, context)");
try {
var s = score.scoreSync(data, context);
console.log("S is", s.tail);
} catch (e) {
console.log("Got an error:", e);
}
Output
Generating fixtures...
Importing class ...
Attempting to run score.scoreSync(data, context)
S is [ 0.1945, 0.2495 ]
Thanks again!
-Sir
On Monday, August 7, 2017 at 2:29:52 PM UTC-4, Didier wrote:
>
> Arrays in Clojure are different to Lists, and its probably why when you
> pass arrays to the score fn, that things don't work 100%.
>
> If you can coerce those arrays to a java list it would probably work. You
> can do that in js, but if that doesn't work or seem easy, you could have
> your -score wrapper do it too.
>
> To convert the 2d array do:
>
> (mapv #(into [] %) the-array)
>
> To convert the 1d array do:
>
> (into [] the-array)
>
> And to convert the return of score from a list back to an array do:
>
> (to-array the-list) or (to-array-2d the-list-of-list)
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.