以三个为一组获取整数列表,并将它们以 8 个为一组输出

Taking a list of integers in groups of three and outputting them in groups of 8

我正在使用 java 中的 groovy 插件进行一些并行练习,但是我遇到了困难。系统获取包含三个整数的数据对象,但必须输出包含整数的对象中的数据。

到目前为止我得到了什么:

import org.jcsp.lang.*

class GenerateSetsOfThree implements CSProcess {

    def ChannelOutput outChannel

    void run(){
        def threeList = [
                         [1, 2, 3], 
                         [4, 5, 6], 
                         [7, 8, 9], 
                         [10, 11, 12], 
                         [13, 14, 15], 
                         [16, 17, 18],
                         [19, 20, 21], 
                         [22, 23, 24] ]
        for ( i in 0 ..< threeList.size)outChannel.write(threeList[i])
        //write the terminating List as per exercise definition
        outChannel.write ([-1, -1, -1])

一切正常。 那么:

import org.jcsp.lang.*

class ListToStream implements CSProcess{

    def ChannelInput inChannel
    def ChannelOutput outChannel

    void run (){
        def inList = inChannel.read()
        while (inList[0] != -1) {
            // hint: output list elements as single integers
        outChannel.write (inList[0, 1, 2, 3, 4, 5, 6, 7])

        }
        outChannel.write(-1)
    }
}

这就是问题所在,我不知道如何取整数并将它们分组为八

最后:

import org.jcsp.lang.*

class CreateSetsOfEight implements CSProcess{

    def ChannelInput inChannel

    void run(){
        def outList = []
        def v = inChannel.read()
        while (v != -1){
            for ( i in 0 .. 7 ) {
                // put v into outList and read next input
                outList = [(v)]
                v = inChannel.read()    
            }
            println " Eight Object is ${outList}"
        }
        println "Finished"
    }
}

这一切似乎都很好。

如有任何建议,我们将不胜感激。 谢谢。

您不应该已经将 ListToStream 中的它们转换为八元组。首先,他们在那里被转换为单个值。八元组的转换在最后一步完成 CreateSetsOfEight。要传递单个值,您需要自己发出每个值。

例如

while (inList[0] != -1) {
    // hint: output list elements as single integers
    inList.each{ outChannel.write it }
    inList = inChannel.read()
}

然而所有的答案也都在线:https://github.com/GPars/GPars/tree/master/docs/JonKerridgeBook