This may be a basic question, but it’s not always clear to me when or how one should leverage Elixir’s concurrency support.
Consider a simple example where we want to replace every vowel in a sentence with its upper-case equivalent. The replacements
list contains tuples indicating the character to find and its replacement:
original = "this is a sentence originally all lower-case"
replacements = [
{"a", "A"},
{"e", "E"},
{"i", "I"},
{"o", "O"},
{"u", "U"},
]
output = Enum.reduce(replacements, original, fn {find, replacement}, acc ->
String.replace(acc, find, replacement) # <-- works
end)
This works, but it does not leverage concurrency. I tried to use spawn(fn -> String.replace(acc, find, replacement) end)
inside the reduce
function, but of course that doesn’t work because spawn()
returns a PID.
This problem is probably a poor candidate for concurrency because each replacement relies on the output of the previous operation. Is there a way to structure this problem in a way that might take advantage of concurrent processes?
Thanks for any pointers!