I am trying to improve the code for my Elixir library which looks like this:
def dirs(path, regex_dir \\ ".+") when (is_bitstring(path) or is_list(path)) do
file_path = case String.valid? path do
true -> [path]
false -> path
end
do_dirs(file_path, [], regex_dir)
end
defp do_dirs([], result, regex_dir) do
result
end
defp do_dirs(paths ,result, regex_dir) do
[h | t] = paths
do_dirs(t, result ++ dirs_list(h, regex_dir), regex_dir)
end
defp dirs_list(path, regex_dir) when is_bitstring(path) do
Finder.new()
|> Finder.with_directory_regex(Regex.compile!(regex_dir))
|> Finder.only_directories()
|> Finder.find(Path.expand(path))
|> Enum.to_list
|> Enum.sort
end
I especially hate the part where I check if file_path is a valid string but I can't figure out a way to beautify it or make it more idiomatic Elixir. I want dirs
to be able to accept both bitstring
and list
arguments.