I know I'm late to the party, but I personally feel weird about this choice. Overloading an argument to be either offset or a function is confusing and unnecessarily inconsistent in my opinion. Personally, with this implementation, I would prefer an explicit name, like Enum.map_with_index, since now this feels a lot more like a mapping function than one for adding indices. Secondly, I feel the original intent has gotten lost in a way? If this is being done in the name of efficiency, isn't it a lot worse to be calling a function, rather than just generating tuples? If the expected use-case is to be thrown into Map.new, I feel, again, it might be better to add a specific function like Map.indexed_new or similar.
Just my 2 quarters of a cent. // Valter On Wed, 9 Dec 2020 at 09:16, José Valim <[email protected]> wrote: > Here is the issue description: > https://github.com/elixir-lang/elixir/issues/10547 > > PR is welcome! > > Thanks. > > On Wed, Dec 9, 2020 at 7:07 AM José Valim <[email protected]> wrote: > >> I like Bruce's option a lot and I will proceed with that. Thanks Bruce >> and everyone! >> >> On Wed, Dec 9, 2020 at 1:48 AM Anil Kulkarni <[email protected]> wrote: >> >>> If there was no Enum.with_index/2 function in existence today, how would >>> we implement it? My personal preference is to do {index, value}, as per >>> José’s message. >>> >>> If we are to make a change, I would prefer to create some new API (for >>> back-compat), and then deprecate/remove Enum.with_index(). >>> >>> >>> >>> However, it’s my opinion that this is too much churn for something that >>> is easily accomplished with `Enum.map()`, and I would just leave it as-is, >>> chalked up to legacy decisions. >>> >>> >>> >>> -Anil >>> >>> >>> >>> *From: *<[email protected]> on behalf of Adam Lancaster >>> <[email protected]> >>> *Reply-To: *"[email protected]" < >>> [email protected]> >>> *Date: *Tuesday, December 8, 2020 at 4:29 PM >>> *To: *"[email protected]" < >>> [email protected]> >>> *Subject: *Re: [elixir-core:9842] Enum.index/2 >>> >>> >>> >>> You could also use the new zip_with: >>> >>> >>> >>> Map.new(Enum.zip_with(list, 0..length(list), & {&2, &1})) >>> >>> >>> >>> On Tue, 8 Dec 2020 at 23:51, Christopher Keele <[email protected]> >>> wrote: >>> >>> I think that even if it enabled awesome optimization opportunities, >>> 1. Having both *Enum.index_with* and *Enum.index*/*indexed* will >>> confuse people. >>> >>> 2. Straying from Elixir's consistent API design of "Module X functions >>> take X's as their first argument" will confuse people. >>> >>> a. Especially in the very consistent and large-surface-area *Enum* >>> module. >>> >>> >>> >>> Both together seem like a less-than-ideal combination. These trade-offs >>> may be worth it depending on what is gained, though. >>> >>> >>> >>> I hadn't seen the second point mentioned yet in this thread yet. Which >>> makes me wonder if there is a *different* module it might belong under, >>> instead, resolving both problems while preserving the intention-revealing >>> *index* function name? >>> >>> >>> >>> ie: *Integer.index(start \\ 0 :: non_neg_integer, Enum.t) :: Enum.t* >>> >>> Kind of communicates that we are indexing an enumerable by an integer >>> value; and just appears a little non-standard when we omit the starting >>> point. >>> >>> >>> >>> Creating a new module namespace for such a function to live--ex >>> *Index*--seems >>> a little excessive in this case, a costly increase in the surface of the >>> standard library. (Is *Enum.Index.by/2 <http://Enum.Index.by/2>* >>> unreasonable? >>> Probably.) >>> >>> Worth nothing that these sorts of one-off-function discussions are >>> exactly how some modules we have today came into being, though--*Atom* >>> and *Tuple* come to mind. But "indexes" are hardly first-class language >>> concepts. >>> >>> On Tuesday, December 8, 2020 at 2:02:13 PM UTC-8 [email protected] wrote: >>> >>> There is a bit of a collision with the option for the offset, but you >>> could pattern match that. >>> >>> >>> >>> -bt >>> >>> >>> >>> On Tue, Dec 8, 2020 at 4:57 PM Zach Daniel <[email protected]> >>> wrote: >>> >>> I like that idea a lot Bruce. Let’s you do something like: >>> >>> >>> >>> maps |> Enum.with_index(&Map.put(&1, :index, &2)) >>> >>> >>> >>> On Tue, Dec 8, 2020 at 4:50 PM Bruce Tate <[email protected]> wrote: >>> >>> Maybe a transform function on with_index? >>> >>> >>> >>> Enum.with_index([:a, :b, :c], &{&2, &1}) >>> >>> >>> >>> -bt >>> >>> >>> >>> On Tue, Dec 8, 2020 at 3:05 PM José Valim <[email protected]> wrote: >>> >>> I would like to propose an Enum.index/2 function. It behaves like >>> with_index/2 but the index is the first argument: >>> >>> >>> >>> iex> Enum.index([:a, :b, :c]) >>> [{0, :a}, {1, :b}, {2, :c}] >>> >>> >>> >>> I am hoping it reads "index" this tuple (hence the index as the first >>> element. While with_index means attach an index information to each element >>> (hence the second element). Is this sound? >>> >>> >>> >>> Thank you, >>> >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K6h7c1UbiEfoEsyCXGJRbO8he%3DFJ84yxC%2B3-GOo-2MwQ%40mail.gmail.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K6h7c1UbiEfoEsyCXGJRbO8he%3DFJ84yxC%2B3-GOo-2MwQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> >>> >>> >>> -- >>> >>> >>> >>> Regards, >>> >>> Bruce Tate >>> >>> CEO >>> >>> >>> >>> [image: Image removed by sender.] >>> >>> >>> >>> Groxio, LLC. >>> >>> 512.799.9366 <(512)%20799-9366> >>> >>> [email protected] >>> >>> grox.io >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-4QOLzA4h34KBVSF1OnMFzxJZEwReV334YP3Pwu7cYX1g%40mail.gmail.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-4QOLzA4h34KBVSF1OnMFzxJZEwReV334YP3Pwu7cYX1g%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0AY9GhKiZJ9GS78%2BW3z5m3%3Ds-F%2BNrQiUYje2WOT%3D65ScA%40mail.gmail.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0AY9GhKiZJ9GS78%2BW3z5m3%3Ds-F%2BNrQiUYje2WOT%3D65ScA%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> >>> >>> >>> -- >>> >>> >>> >>> Regards, >>> >>> Bruce Tate >>> >>> CEO >>> >>> >>> >>> [image: Image removed by sender.] >>> >>> >>> >>> Groxio, LLC. >>> >>> 512.799.9366 <(512)%20799-9366> >>> >>> [email protected] >>> >>> grox.io >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/8cfe7aca-a579-405e-89a7-3f81774af255n%40googlegroups.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/8cfe7aca-a579-405e-89a7-3f81774af255n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CADwgC_C0nMQU894350s94OoVfymHFGyJnXYpgwR%3DmUvMDqsmhg%40mail.gmail.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/CADwgC_C0nMQU894350s94OoVfymHFGyJnXYpgwR%3DmUvMDqsmhg%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-core/0100017644f82b24-a6e1b307-78f1-4b83-8d2e-99c40f663533-000000%40email.amazonses.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/0100017644f82b24-a6e1b307-78f1-4b83-8d2e-99c40f663533-000000%40email.amazonses.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4L47EmZx%2BYci5reSdvsCDtxvRDsLrpY2oktQtQO8eRYeQ%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4L47EmZx%2BYci5reSdvsCDtxvRDsLrpY2oktQtQO8eRYeQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAPkwuKnuXKnKG1DqP7%3DCszeCgkoy3pfG2tJCi6sE3-EVE-P13g%40mail.gmail.com.
