Iterators: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Ring}}: incorrect, or at least, missing the point) |
m (J: slightly clearer presentation of ideas) |
||
Line 114: | Line 114: | ||
└───┴─────────────────────────────────────────┘</lang> |
└───┴─────────────────────────────────────────┘</lang> |
||
Here, |
Here, the implementation's array indexing would see the linked list representation as a two element array. To index arbitrary elements from the linked list, we might map back from the linked list representation to a flat array representation, perhaps using <S:0 (which is a no-op on our array of days of week). |
||
<lang J> echo ;:inv <S:0 dow |
<lang J> echo ;:inv <S:0 dow |
||
Line 131: | Line 131: | ||
purple green yellow</lang> |
purple green yellow</lang> |
||
The downside of this approach is that the programmer must understand the data. |
The downside of this approach is that the programmer must understand the data (to know to map all relevant list structures to the desired form). For example, we might instead say that a linked list is not merely an unbalanced binary tree, but must always have its data elements in the left node. That would give us this implementation for col: |
||
<lang J> |
|||
⚫ | |||
col=: '' ]F..(,<) ;:'red orange yellow green blue purple' |
|||
</lang> |
|||
This creates an issue that a single element nested list looks very much like a flat array. To prevent that from being a problem, we include an empty element at the end of our flat array: |
|||
<lang J> |
|||
dow=: a:,~;:'monday tuesday wednesday thursday friday saturday sunday' |
|||
</lang> |
|||
Now everything is the same as before, except that we need to explicitly ignore the empty trailing element: |
|||
<lang J> echo ;:inv _2 _4 _5 {<S:0 dow |
|||
sunday friday thursday |
|||
echo ;:inv _2 _4 _5 {<S:0 col |
|||
red yellow green</lang> |
|||
That said, we now also have the opportunity to implement a different kind of normalization routine, which takes advantage of the difference in representation (if we had a need for that...). |
|||
⚫ | |||
=={{header|Julia}}== |
=={{header|Julia}}== |