Chaining function calls using the result of the previous function

I found myself wanting to apply a set of functions which would return a modified version of the passed in string, to which I wanted to apply the result to the next function. This led to me writing code like so:

val modifiedContent1 = repeatedHeaderRegex.replaceAllIn(content, “”)
val modifiedContent2 = repeatedMultipleWordsWithDateHeaderRegex.replaceAllIn(modifiedContent1, “”)
repeatedSeparatedByBulletPointHeaderRegex.replaceAllIn(modifiedContent2, “”)

Which is pretty ugly and would mean needing to add more code for any additional regexes.

After digging around for some kind of function that would make this less ugly\verbose, I came across the foldLeft function.
This allowed me to pass in the original content string and then apply the current regex item’s findAllIn function to the result of the previous iteration. This does exactly the same as the above code, but looks like so:

Seq(repeatedHeaderRegex, repeatedMultipleWordsWithDateHeaderRegex, repeatedSeparatedByBulletPointHeaderRegex)
.foldLeft(content)((text, regex) => regex.replaceAllIn(text, “”))

This is good to keep in mind in the future as I do need to do something like this fairly often, but I would just do separate functions for each case instead of putting them all in one like I did in the first code block.
I looked for an equivalent in C# and came across the Aggregate function, so I’m glad I can do this there too.