Freelance software grandad
software created
extended or repaired
Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations
Here's a fairly direct transliteration of some of the code presented in Thomas Guest's Zippy Triples served with Python. As you might anticipate, it's a little wordier than the Python original, but it's still a nice example of using iterators in an unusual way to neatly solve a problem.
import java.util.Iterator; import java.util.List; import uk.co.jezuk.mango.Collections; import uk.co.jezuk.mango.Iterators; public class ZippyTriples { static public void main(final String[] args) { List<String> long_weekend = Collections.list("Friday", "Saturday", "Sunday", "Monday"); Iterator<List<String>> yesterday_today_tomorrow = prev_this_next(long_weekend); while(yesterday_today_tomorrow.hasNext()) { List<String> ytt = yesterday_today_tomorrow.next(); for(String d : ytt) { System.out.print(d); System.out.print(" "); } System.out.println(); } } static Iterator<List<String>> prev_this_next(final List<String> items) { Iterator<String> chain = Iterators.ChainIterator(null, items, null); List<Iterator<String>> prev_this_next = Iterators.TeeIterator(chain, 3); Iterator<String> prev = prev_this_next.get(0); Iterator<String> current = prev_this_next.get(1); Iterator<String> next = prev_this_next.get(2); current.next(); next.next(); next.next(); return Iterators.ZipIterator(prev, current, next); } }
Freelance software grandad
software created
extended or repaired
Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations