Iterators: Difference between revisions
julia example
Thundergnat (talk | contribs) m (→{{header|Raku}}: sigh, more typos) |
(julia example) |
||
Line 81:
Purple, Yellow, Orange
</pre>
=={{header|Julia}}==
{{trans|C++}}
Julia has a iteration interface. Any Julia type which implements the `iterate` function is
considered an iterable. In base Julia, this includes any array type or collection, including
any subclass of AbstractRange, UnitRange, Tuple, Number, AbstractArray, BitSet, IdDict, Dict,
WeakKeyDict, EachLine, AbstractString, Set, Pair, and NamedTuple. In particular, Julia has an
extensive set of functions which act of lists and vectors. Julia's Iteration utilities can
implement the C++ example:
<lang julia>function PrintContainer(iterator)
iter = Iterators.Stateful(iterator)
foreach(x -> print(x, ", "), Iterators.take(iter, length(iter) -1))
foreach(println, Iterators.take(iter, 1))
end
function FirstFourthFifth(iterator)
iter = Iterators.Stateful(iterator)
foreach(x -> print(x, ", "), Iterators.take(iter, 1))
popfirst!(iter); popfirst!(iter)
foreach(x -> print(x, ", "), Iterators.take(iter, 1))
foreach(println, Iterators.take(iter, 1))
end
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
const colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]
println("All elements:")
PrintContainer(days)
PrintContainer(colors)
println("\nFirst, fourth, and fifth elements:")
FirstFourthFifth(days)
FirstFourthFifth(colors)
println("\nReverse first, fourth, and fifth elements:")
FirstFourthFifth(Iterators.reverse(days))
FirstFourthFifth(Iterators.reverse(colors))
<lang>
<pre>
All elements:
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
Red, Orange, Yellow, Green, Blue, Purple
First, fourth, and fifth elements:
Sunday, Wednesday, Thursday
Red, Green, Blue
Reverse first, fourth, and fifth elements:
Saturday, Wednesday, Tuesday
Purple, Yellow, Orange
</pre>
=={{header|Phix}}==
|