Is there anybody, who can explain me, how works this piece of code?
class CSVParser {
static def parseCSV(file,closure) {
def lineCount = 0
file.eachLine() { line ->
def field = line.tokenize(",")
lineCount++
closure(lineCount,field)
}
}
}
use(CSVParser.class) {
File file = new File("test.csv")
file.parseCSV { index,field ->
println "row: ${index} | ${field[0]} ${field[1]} ${field[2]}"
}
}
The link: http://groovy-almanac.org/csv-parser-with-groovy-categories/
"parseCSV" looks as a method, but is used on "file" as closure. Closure is one of the "parseCSV" parameters and most confusingly - inside this method there is just closure(lineCount,field)
without any inner functionality.
How does it work exactly with closure on file.parseCSV
and use(CSVParser.class)
?