Wednesday, August 01, 2012

Typing more to type less

My code is littered with bits like this:


public final class TimesIterable
        implements Iterable<Integer> {
    private final int n;

    public static Iterable<Integer> upTo(final int n) {
        return new TimesIterable(n);

    public TimesIterable(final int n) {
        this.n = n;

    public Iterator<Integer> iterator() {
        return new UnmodifiableIterator<Integer>() {
            private int i = 0;

            public boolean hasNext() {
                return i < n;

            public Integer next() {
                return ++i;

All so I can write:

import static TimesIterable.upto;
import static java.lang.System.out;

public static void main(final String... args) {
    for (int n : upTo(4))

To my surprise Google is not quite there yet. (I'd prefer to be wrong).

Post a Comment