Re: Passing Arguments From Java to Clojure

2010-06-16 Thread rzeze...@gmail.com
-- CallClojure.java --
import clojure.lang.RT;
import clojure.lang.Var;
import clojure.lang.PersistentVector;

public class CallClojure {

static PersistentVector toVec(int[][] arr) {
PersistentVector pv = PersistentVector.EMPTY;
for (int[] a : arr) {
PersistentVector temp = PersistentVector.EMPTY;
for (int n : a) {
temp = temp.cons(n);
}
pv = pv.cons(temp);
}
return pv;
}

public static void main(String[] args) throws Exception {
RT.loadResourceScript("foo.clj");
Var gimmie_vec = RT.var("foo", "gimmie-vec");
int[][] ar = {{1, 2}, {3, 4}, {5, 6}};
Object result = gimmie_vec.invoke(toVec(ar));
System.out.println(result);
}
}

-- foo.clj --
(ns foo)

(defn gimmie-vec [v]
  (println "class:" (class v))
  (println "v:" v))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Passing Arguments From Java to Clojure

2010-06-16 Thread Stuart Halloway
Do you really need a Clojure vector-of-vectors, or do you just want an indexed 
collection of indexed collections?  If the latter, you can simply use Java 
arrays, or ArrayMaps.

; build a collection a Java programmer might have made
; (I am not really going to go into Java just for an example... :-)
(def x (into-array (map into-array [[1 2] [3 4]])))
=> #'user/x

; function that destructures positional args
(defn foo [[[a b] [c d]]] [a b c d])
=> #'user/foo

; ta da
(foo x)
=> [1 2 3 4]


> There's a canonical intro on how to call or embed Clojure into Java:
> http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#Invoking_Clojure_from_Java
> 
> While this is a great introduction, the only thing Java passes in to
> Clojure here is a string. I've tried methods where Java passes in
> ints, and those work fine too.
> 
> What I'm primarily concerned with is if there is some way to pass in a
> list/vector/etc, if there's a particular way to format the argument on
> the Java side so it can be easily converted on the Clojure side
> without much hassle (via vector or something similar), or if I need to
> just suck it up, pass in a string of numbers, and write a function on
> the Clojure side that handles all that.
> 
> To be specific, I would like to make a vector that would end up in
> pairs: [[1 2] [3 4] [5 6]]. For those of you familiar with Incanter,
> this will then become a dataset.
> 
> Thanks in advance.
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Passing Arguments From Java to Clojure

2010-06-16 Thread Daniel
Actually, just look at the main method (for testing) which has been
commented out at the bottom - that will show you a better way to
create and use it.

On Jun 16, 9:37 am, allie  wrote:
> There's a canonical intro on how to call or embed Clojure into 
> Java:http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#I...
>
> While this is a great introduction, the only thing Java passes in to
> Clojure here is a string. I've tried methods where Java passes in
> ints, and those work fine too.
>
> What I'm primarily concerned with is if there is some way to pass in a
> list/vector/etc, if there's a particular way to format the argument on
> the Java side so it can be easily converted on the Clojure side
> without much hassle (via vector or something similar), or if I need to
> just suck it up, pass in a string of numbers, and write a function on
> the Clojure side that handles all that.
>
> To be specific, I would like to make a vector that would end up in
> pairs: [[1 2] [3 4] [5 6]]. For those of you familiar with Incanter,
> this will then become a dataset.
>
> Thanks in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Passing Arguments From Java to Clojure

2010-06-16 Thread Daniel
You need to pass in a PersistentVector of PersistentVector's.  It's in
clojure.lang and there are a number of static creation methods.

On Jun 16, 9:37 am, allie  wrote:
> There's a canonical intro on how to call or embed Clojure into 
> Java:http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#I...
>
> While this is a great introduction, the only thing Java passes in to
> Clojure here is a string. I've tried methods where Java passes in
> ints, and those work fine too.
>
> What I'm primarily concerned with is if there is some way to pass in a
> list/vector/etc, if there's a particular way to format the argument on
> the Java side so it can be easily converted on the Clojure side
> without much hassle (via vector or something similar), or if I need to
> just suck it up, pass in a string of numbers, and write a function on
> the Clojure side that handles all that.
>
> To be specific, I would like to make a vector that would end up in
> pairs: [[1 2] [3 4] [5 6]]. For those of you familiar with Incanter,
> this will then become a dataset.
>
> Thanks in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en