Still a newb so looking for feedback on if this would work:
defmodule ExampleModule do
@doc """
Takes a path and returns a list of tuples in the form of:
[{"segment", "/path/to/segment"}]
## Examples:
iex> ExampleModule.segment_path("/path/to/segment")
iex> [{"path", "/path"}, {"to", "/path/to"}, {"segment", "/path/to/segment"}]
"""
@spec segment_path(binary()) :: [{String.t(), String.t()}]
def segment_path(path) when is_binary(path) do
path
|> String.split("/", trim: true)
|> Enum.scan({nil, ""}, fn
el, {_, path} when is_binary(path) ->
{el, "#{path}/#{el}"}
_, _ ->
:noop
end)
|> Enum.map(fn
{seg, path} when is_binary(seg) and is_binary(path) ->
{seg, path |> String.split("/") |> Enum.reverse() |> Enum.join("/")}
_, _ ->
:noop
end)
|> Enum.reject(&(&1 == :noop))
end
end






















