Another way:
def segment_path(path) do
path_levels =
path
|> String.split("/", trim: true)
|> length()
path
|> breadcrumb()
|> Stream.iterate(fn
{segment, segment_path} ->
parent_path = String.replace_trailing(segment_path, "/#{segment}", "")
breadcrumb(parent_path)
end)
|> Enum.take(path_levels)
|> Enum.reverse()
end
def breadcrumb(path) do
segment = path
|> String.split("/")
|> List.last()
{segment, path}
end
The solution with this code unfolds like this:
path = "/some/path/to/nowhere"
1. {"nowhere", "/some/path/to/nowhere"}
2. {"to", "/some/path/to"}
3. {"path", "/some/path"}
4. {"some", "/some"}
Then it’s reversed.
I won’t add reasoning on why (or whether) this is better, but just another implementation. ![]()






















