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

青春壹個敷衍的年華 提交于 2019-12-11 11:56:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!