|
| 1 | +# A built-in for getting index-and-value pairs from a list |
| 2 | + |
| 3 | +## Preamble |
| 4 | + |
| 5 | + Author: Ricardo Signes <[email protected]> |
| 6 | + Sponsor: |
| 7 | + ID: 0016 |
| 8 | + Status: Draft |
| 9 | + |
| 10 | +## Abstract |
| 11 | + |
| 12 | +This RFC proposes `indexed`, a new builtin for interleaving a list of values |
| 13 | +with their index in that list. Among other things, this makes key/value |
| 14 | +iteration on arrays easy. |
| 15 | + |
| 16 | +## Motivation |
| 17 | + |
| 18 | +With v5.36.0 poised to add n-at-a-time foreach, easily getting a list of |
| 19 | +index/value pairs from an array makes iteration over the pairs also becomes |
| 20 | +easy. |
| 21 | + |
| 22 | +## Rationale |
| 23 | + |
| 24 | +If we start with the specific case of iterating over the indexes and values of |
| 25 | +an array using two-target foreach, we might write this: |
| 26 | + |
| 27 | +```perl |
| 28 | +for my ($i, $value) (%array[ keys @array ]) { |
| 29 | + say "$i == $value"; |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +This is tolerable, but a bit verbose. If we bury our target array deep in a |
| 34 | +structure, we get this: |
| 35 | + |
| 36 | +```perl |
| 37 | +for my ($i, $value) ($alpha->{beta}->[0]->%[ keys $alpha->{beta}->[0]->@* ]) { |
| 38 | + say "$i == $value"; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +This is pretty bad. |
| 43 | + |
| 44 | +With `indexed`, we write this: |
| 45 | + |
| 46 | +```perl |
| 47 | +for my ($i, $value) (indexed $alpha->{beta}->[0]->@*) { |
| 48 | + say "$i == $value"; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +This is probably about as simple as this can get without some significant new |
| 53 | +addition to the language. |
| 54 | + |
| 55 | +## Specification |
| 56 | + |
| 57 | + indexed LIST |
| 58 | + |
| 59 | +`indexed` takes a list of arguments. |
| 60 | + |
| 61 | +In scalar context, `indexed` evalutes to the number of entries in its argument, |
| 62 | +just like `keys` or `values`. This is useless, and issues a warning in the new |
| 63 | +"scalar" category: |
| 64 | + |
| 65 | + Useless use of indexed in scalar context |
| 66 | + |
| 67 | +In void context, the `Useless use of %s in void context` warning is issued. |
| 68 | + |
| 69 | +In list context, `indexed LIST` evalutes to a list twice the size of the array, |
| 70 | +meshing the values with a list of integers starting from zero. All values are |
| 71 | +copies, unlike `values ARRAY`. (If your LIST was actually an array, you can |
| 72 | +use the index to modify the array that way!) |
| 73 | + |
| 74 | +## Backwards Compatibility |
| 75 | + |
| 76 | +There should be no significant backwards compatibility concerns. `indexed` |
| 77 | +will be imported only when requested. Static analysis tools may need to be |
| 78 | +updated. |
| 79 | + |
| 80 | +A polyfill for indexed can be provided for older perls, but may not be as |
| 81 | +optimizable. |
| 82 | + |
| 83 | +## Security Implications |
| 84 | + |
| 85 | +Nothing specific predicted. |
| 86 | + |
| 87 | +## Examples |
| 88 | + |
| 89 | +(See the examples under **Rationale**.) |
| 90 | + |
| 91 | +I expect that docs for `keys` and `values` will be updated to reference |
| 92 | +`indexed` as well, and we'll add a note about it to the documentation on `for` |
| 93 | +and possibly pair slices. |
| 94 | + |
| 95 | +When n-at-a-time foreach is no longer experimental, we should refer to the |
| 96 | +combination of `for my (...) (...)` with `indexed` as forming an alternative to |
| 97 | +`each` in the documentation for `each`. |
| 98 | + |
| 99 | +## Prototype Implementation |
| 100 | + |
| 101 | +None. |
| 102 | + |
| 103 | +## Future Scope |
| 104 | + |
| 105 | +I believe this will be complete as is. |
| 106 | + |
| 107 | +## Rejected Ideas |
| 108 | + |
| 109 | +This proposal replaces one for `kv` which could be called on hash or array |
| 110 | +literals to act like a combination of `keys` and `values`. |
| 111 | + |
| 112 | +That proposal replaced one for a slice syntax that evaluated to a slice that |
| 113 | +omitted nothing. |
| 114 | + |
| 115 | +## Open Issues |
| 116 | + |
| 117 | +None? |
| 118 | + |
| 119 | +## Copyright |
| 120 | + |
| 121 | +Copyright (C) 2021, Ricardo Signes. |
| 122 | + |
| 123 | +This document and code and documentation within it may be used, redistributed |
| 124 | +and/or modified under the same terms as Perl itself. |
0 commit comments