[RSS 0.91]
Friday 21 May, 2010
#Zippy triples served with Java and Mango

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);
    }
}
Hello,

very nice example. Are You also programming in PHP?

It is better and more friendly than Python I think.

Regards,

Laz
Laz Taylor [e] [w], 9th Jun 2010

Enter your comment

Name Email
URL
If you have an account, please log in.
No account? Just create one.



Jez Higgins