# An second example of using pipes in Elixir. # # The major difference here is the use of named functions # inside a module and replacing most of the Enum functions # with recursion # # author gtowell # Created: July 19, 2022 defmodule UpTest do @doc """ a function to capitalize the first letter of a string This takes a stirng as an arguement and returns a string with the firstletter capitalized """ def up_first (str) do String.upcase(String.slice(str, 0..0)) <> String.slice(str, 1..-1) end @doc """ Replace the Enum with my own recursion. Usually this is unnecessary and silly; better to use Enum.reduce. But as an example, OK. """ # if the list is empty def cap_list ([]) do [] end # if the list of not empty def cap_list([head | rest]) do [up_first(head) | cap_list(rest)] end @doc """ Joint together the strings in the given list separating them with the string given in the insert param """ # Handle the case where the list is empty def mjoin([], _insert) do "" end # Handle the case where the list has exactly one item # Jusr ensures that the resulting string does not have a # trailing insert def mjoin([h], _insert) do h end # Handle the general case def mjoin([head | rest], insert) do head <> insert <> mjoin(rest, insert) end end defmodule Main do def main do # example IO.puts UpTest.up_first("this") # examples IO.inspect UpTest.cap_list(["a", "test"]) IO.inspect UpTest.cap_list(~w[aaa bbb ccc ddd]) IO.inspect UpTest.cap_list(String.split("this is a test")) IO.puts UpTest.mjoin(~w[aaa bbb ccc ddd], "<>") IO.inspect String.split("this is a test") |> UpTest.cap_list |> UpTest.mjoin(" ") # Putting everything together without using pipes #IO.inspect Enum.join(cap_list.(String.split("this is a test")), "_") end end Main.main