I need to pipe a list of strings through a function to return a string. Currently, I have this:
@tag_delimiters [" ", "(", ".", "#"]
def tag(string) do
tag(string, " ")
|> tag("(")
|> tag(".")
|> tag("#")
end
defp tag(string, delimiter)
[head | _] = String.split(string, delimiter)
head
end
I want to remove the pipes and use some kind of recursive function to run this based on the @tag_delimiter attribute. What is the correct way to do recursive piping?
Edit for DSL:
tag(string)
When fed "span(class="foo")#bar Hello my name is Dex" or "span Hello my name is Dex" I want the output to be "span". I do this my recursively looking for characters that succeed the first word in a line. However, I do this by splitting the string using dividers.
This is already working, I am just looking for a more elegant solution to my piping problem.
You can use the Enum.reduce function to do this.
Enum.reduce(@tag_delimiters, string, fn(a, b) -> tag(b, a) end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With