Iterators: Difference between revisions

Content added Content deleted
(Iterators en BASIC256)
m (J: try to fit in with the crowd a bit better)
Line 277: Line 277:


However, given the intrinsic value of comprehension when programming, a requirement for comprehension (and, thus, documentation) is frequently a worthwhile price. (There's also an execution cost issue, which can be especially significant in contexts where comprehension is difficult.)
However, given the intrinsic value of comprehension when programming, a requirement for comprehension (and, thus, documentation) is frequently a worthwhile price. (There's also an execution cost issue, which can be especially significant in contexts where comprehension is difficult.)

That said, we could implement other approaches which are more similar to some of the other approaches here. These tend to be bulky and inefficient, but some people like that kind of thing. (Some insist on that kind of thing.)

For example:

<lang J>nextItem=: {{ (x+1) -.#y }}
nextLink=: {{ (1;x) #~ (L.y) > #x }}

makeIterator=: {{ x;y;0 }}

iterate=: {{'`next val more'=. ops['ops list position'=. y
(}:y),<position next list
}}

value=: {{'`next val more'=. ops['ops list position'=. y
position val list
}}

more=: {{'`next val more'=. ops['ops list position'=. y
more position
}}</lang>

With this approach, one of the task examples could look like this:

<lang J>
printAll=: {{
while. more y do.
echo value y
y=. iterate y
end.EMPTY
}}
DOW=: nextItem`{::`# makeIterator ;:'monday tuesday wednesday thursday friday saturday sunday'
COL=: nextLink`{::`# makeIterator (,<)/;:'red orange yellow green blue purple'

printAll DOW
monday
tuesday
wednesday
thursday
friday
saturday
sunday
printAll COL
red
orange
yellow
green
blue
purple
value iterate^:0 DOW
monday
value iterate^:3 DOW
thursday</lang> etc.


=={{header|Julia}}==
=={{header|Julia}}==