问题
Assume I have a Frege module
module Util where
total :: [Int] -> Int
total xs = fold (+) 0 xs
If "total" was written in Java, I could call it via
Util.total(Arrays.asList(1,2,3));
What is the best way to call the Frege implementation from Java?
回答1:
You could use a good old int [] array, the corresponding frege type would be JArray Int. Because arrays can be made from and into lists in both Java and frege, they are good for such tasks.
Please use the repl to get an idea how to convert the array into a list so that you can pass it to your function.
If there are concerns rgd. heap space, there is also a so called ArrayIterator in Data.Iterators, that is an instance of the ListView type class. So another option would be to write your frege so as to take a ListView
total xs = fold (+) 0 xs.toList
and in java call the equivalent of
ArrayIterator.from (... code to create array here ...)
And if you can't change the frege code, or don't want to, you can make a lazy list with
(ArrayIterator.from (... code to create array here ...)).toList
Last but not least, you can fold over the array without converting it before with foldArray
.
来源:https://stackoverflow.com/questions/27349444/what-is-the-easiest-way-to-pass-a-list-of-integers-from-java-to-a-frege-function