Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created July 4, 2016 02:53
Show Gist options
  • Save ijunaid8989/893e5f9883eb73d290cfef3ccb80e5e4 to your computer and use it in GitHub Desktop.
Save ijunaid8989/893e5f9883eb73d290cfef3ccb80e5e4 to your computer and use it in GitHub Desktop.
def from_list(list) do
{_, matrix} = do_from_list(list, [], %{})
matrix
end
defp do_from_list(list, indices, matrix) do
Enum.reduce(list, {0, matrix}, fn
sublist, {idx, matrix} when is_list(sublist) ->
{sublist_idx, map} = do_from_list(sublist, [idx | indices], matrix)
{idx + 1, map}
item, {idx, matrix} ->
coordinates =
[idx | indices]
|> Enum.reverse
|> List.to_tuple
{idx + 1, Map.put(matrix, coordinates, item)}
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment