This question is not meant to be a critique of functional programming, but more hoping for resources and opinions.
I am refactoring some historically messy code so that it follows functional programming a bit more, and I've arrived at something like this.
readJson('./data/member.json')
.map(unmarshallData)
.map(getCachedData(newBioIdToUrlNameMap))
.map(getTransformedFields)
.map(getVoteData(changedFiles))
I've abstracted away business logic and encapsulated portions into functions with appropriate names. I aimed for pure functions, and any parts that are impure I've contained in their own functions (I'm probably not fully living up to the tenets of functional programming here, but maybe another refactor can take it to the next level). Now that I've gotten to this more readable and understandable place, I feel very warm and fuzzy inside.
I am looking at my old code. It was one giant for loop:
const rawData = readJson('./data/member.json')
for (const rawDatum of rawData) {
...
...
}
This loop had multiple responsibilities and performed many side effects. It felt very messy and increasingly unmaintainable -- but it was doing everything in a single loop.
"Pipelines" in functional programming are mn
time complexity, where m
is the length of segments in a pipeline. In my particular case, my "imperative" code was 1n
while my fp refactor is 4n
.
I've read somewhere that the coefficient before n
is not really important in time complexity, at least not nearly as important as going from O(n)
to O(n log n)
or O(n^2)
. But still -- wouldn't there be some real world scenarios where a team might want to take the 1n
solution over the 4n
solution?
My questions about this are:
How important is the coefficient before the
n
in Big O notation? Is there some consensus in the Big O community about its level of importance?Are there any languages or tools that "optimize" fp code like this, taking multiple loops and then compiling them down to less loops and adding in side effects in the process? This kind of code is undesirable as a human, but the computer doesn't care. Maybe this is ludicrous, but I'm just wondering if this exists.