I am making a image filter, I already have this done, but I've read that for large collections it would be better to use a sequence for iteration, and since is a 8k image I suppose that I would gain some performance because of its lazy initialization with a Sequence<IntArray>
instead of Array<IntArray>
, or even Sequence<Sequence<Int>>
. I don't know if this is possible yet, I'm quite confused and trying to learn this new paradigm for me, and I'm having difficult to find more easier material to understand the usage syntax of this concept.
This is what a tried, but is a mess, I don't have much idea of how to proceed this, or even if I should use the "newImage" as Sequence also.
val myPredicate = { array : IntArray -> !array.first() /*???*/ && !array.last() }
image.asSequence().forEach { array ->
array.filter(myPredicate ) // ???
}
This is the functional code to be transformed:
fun chunker(image : Array<IntArray>) : Array<IntArray> {
val arrayRowSize = image.size
val arrayColSize = image[0].size
val newImage : Array<IntArray> by lazy {
Array(arrayRowSize) { IntArray(arrayColSize) }
}
var kernel = IntArray(9)
// to translate to a Sequence those two for loops
for (row in 1 .. arrayRowSize - 2) {
for (col in 1 .. arrayColSize - 2) {
kernel = changer(row, col, kernel, image)
newImage[row][col] = kernel[4]
}
}
return newImage
}