将整数列表从Java传递到frege函数的最简单方法是什么? [英] what is the easiest way to pass a list of integers from java to a frege function?

查看:83
本文介绍了将整数列表从Java传递到frege函数的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Frege模块

Assume I have a Frege module

module Util where

total :: [Int] -> Int
total xs = fold (+) 0 xs

如果"total"是用Java编写的,我可以通过以下方式调用它

If "total" was written in Java, I could call it via

Util.total(Arrays.asList(1,2,3));

从Java调用Frege实现的最佳方法是什么?

What is the best way to call the Frege implementation from Java?

推荐答案

您可以使用旧的int []数组,对应的frege类型为JArray Int.由于数组可以在Java和frege中的列表中来回建立,因此对于此类任务非常有用.

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.

请使用repl了解如何将数组转换为列表,以便可以将其传递给函数.

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.

如果担心rgd.在堆空间中,Data.Iterators中还有一个所谓的ArrayIterator,它是ListView类型类的实例.因此,另一种选择是编写您的frege,以便采用ListView

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

,并且在Java中调用等价

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

最后但并非最不重要的一点是,您可以使用foldArray折叠数组而不用转换它.

Last but not least, you can fold over the array without converting it before with foldArray.

这篇关于将整数列表从Java传递到frege函数的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆