# An example of using pipes in Elixir. # The pipes do a fairly trivial task, namely capitalizing # the first letter of every word in a string. # # The pipes only appear at the end of this file # Prior to that is a couple of functions and examples of # their use. # # This version uses only anonymous functions and several # functions in the Enum library # # author gtowell # Created: July 19, 2022 # First a function to capitalize the first letter of a string # This takes a string as an argument and returns a string # with the first letter capitalized up_first = fn (str) -> String.upcase(String.slice(str, 0..0))<> String.slice(str, 1..-1) end # example IO.puts up_first.("this") # Capitalize the each word in a list of words cap_list = fn (wlist) -> Enum.map(wlist, fn (w) -> up_first.(w) end) end # examples IO.inspect cap_list.(["a", "test"]) IO.inspect cap_list.(String.split("this is a test")) # Putting everything together without using pipes IO.inspect Enum.join(cap_list.(String.split("this is a test")), "_") # Now do it using pipes # The results is alost always far easier to read. IO.inspect String.split("this is a test") |> cap_list.() |> Enum.join("_")