Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive piping to a function using a list

Tags:

elixir

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.

like image 855
dimiguel Avatar asked Feb 15 '26 19:02

dimiguel


1 Answers

You can use the Enum.reduce function to do this.

Enum.reduce(@tag_delimiters, string, fn(a, b) -> tag(b, a) end)    
like image 139
dimiguel Avatar answered Feb 21 '26 00:02

dimiguel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!