(this question is written using javascript for examples)
Say I have a bunch of files that I need to access sequentially:
1.json
2.json
3.json
4.json
...
Say I want to transform the data in all of these files and store the transformed data in new files.
This loop goes through a list of IDs, reads a file, transforms the data, and then writes the data to a new file.
import { readFileSync, writeFileSync } from 'fs'
const ids = [1,2,3,4]
for (const id of ids) {
const datum = JSON.parse(readFileSync(`${id}.json`))
const transformedDatum = ....
writeFileSync(`${id}-transformed.json`, JSON.stringify(transformedDatum)
}
In reality, the logic for all of this is many more lines of code, and I want to use functional programming techniques to make it more readable and make pure functions that don’t have side effects.
So I start to go down this road.
import { readFileSync, writeFileSync } from 'fs'
function transformDatum(id) {
const datum = JSON.parse(readFileSync(`${id}.json`))
const transformedDatum = ....
writeFileSync(`${id}-transformed.json`, JSON.stringify(transformedDatum)
}
const ids = [1,2,3,4]
const transformedData = ids.map(transformDatum)
This now encapsulates the list of IDs passed into the business logic, and the business logic and IDs are now no longer existing in the same scope.
But the function is still interacting with the file system. I want to isolate the I/O so that the function receives the file data and returns the transformed data, making it pure.
import { readFileSync, writeFileSync } from 'fs'
function transformDatum(rawDatum) {
const transformedDatum = ....
return transformedDatum
}
const ids = [1,2,3,4]
for (const id of ids) {
const rawDatum = JSON.parse(readFileSync(`${id}.json`))
const transformedDatum = transformDatum(rawDatum)
writeFileSync(`${id}-transformed.json`, JSON.stringify(transformedDatum)
}
Now the transformDatum
function is pure, but I’ve sort of walked back to having an outer for
loop which goes through the IDs.
Is this more or less a “healthy” tradeoff to make in functional programming? Is there some better way of handling this?
rpivovar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.