简体   繁体   中英

what is the easiest way to pass a list of integers from java to a frege function?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM