Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List comprehension is... useful #67

Open
ndrean opened this issue Nov 6, 2023 · 2 comments
Open

List comprehension is... useful #67

ndrean opened this issue Nov 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@ndrean
Copy link
Contributor

ndrean commented Nov 6, 2023

I had this pretty (useful) complicated function:

def list_tuples_to_unique_keys(parts) do
  key = parts |> hd() |> elem(0)
  new_keys = Enum.map(1..length(parts), &(key <> "-#{&1}"))

  Enum.zip_reduce([parts, new_keys], [], fn [elt, new_key], acc ->
    [
      elt |> Tuple.delete_at(0) |> Tuple.insert_at(0, new_key)
      | acc
    ]
  end)
  |> Enum.sort()
end

Instead, using a comprehension list does the same job and is easy to understand....Never too late to improve!

def  list_tuples_to_unique_keys_from_comprehension(parts) do
  for {{f, l, m}, i}<- Enum.with_index(parts) do
    {f<>"-"<>Integer.to_string(i+1), l,m} 
  end
end
parts = [
    {
       "files",
       [
         {"content-type", "image/png"},
         {"content-disposition", "form-data; name=\"files\"; filename=\"first.png\""}
       ], 
       %{path: "..", content_type: "image/png", filename: "first.png"}
    },
    {
       "files",
       [
         {"content-type", "image/webp"},
         {"content-disposition", "form-data; name=\"files\"; filename=\"second.webp\""}
       ], 
       %{path: "...", content_type: "image/webp", filename: "second.webp"}
    }
]

list_tuples_to_unique_keys_from_comprehension(parts)
[
  {
     "files-1",
       ^^^
     [
       {"content-type", "image/png"},
       {"content-disposition", "form-data; name=\"files\"; filename=\"first.png\""}
     ], 
     %{filename: "first.png", path: "..", content_type: "image/png"}
  },
  {
     "files-2",
      ^^^
     [
       {"content-type", "image/png"},
       {"content-disposition", "form-data; name=\"files\"; filename=\"first.png\""}
     ], 
     %{filename: "first.png", path: "..", content_type: "image/png"}
  }
]
@ndrean
Copy link
Contributor Author

ndrean commented Nov 6, 2023

https://www.mitchellhanberg.com/the-comprehensive-guide-to-elixirs-for-comprehension/

Screenshot 2023-11-06 at 09 22 31

@ndrean ndrean changed the title List comprehension are... useful List comprehension is... useful Nov 6, 2023
@nelsonic nelsonic added the enhancement New feature or request label Nov 6, 2023
@ndrean
Copy link
Contributor Author

ndrean commented Nov 10, 2023

Just to add that the "comprehension" version works when you know exactly what you are receiving, whilst the first version works whatever you receive, as long as the "key" is the first element of the tuple of course.

The learning is more about the power of "comprehensions".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants