Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10406

Re: for :each style question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!pnx.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: for :each style question
Date Thu, 01 Dec 2011 23:36:04 +0100
Lines 76
Message-ID <9jqdqlFkoaU1@mid.individual.net> (permalink)
References <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net ONBqNDLLTnbE1GbfY4+T0gTSObHz2x8lypuEQMUOszqP/2p+A=
Cancel-Lock sha1:M4IopLmH7eNbNasP+jCtLqsedVk=
User-Agent Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
In-Reply-To <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10406

Show key headers only | View raw


On 12/01/2011 12:32 AM, Roedy Green wrote:
> In a for:each loop, sometimes you want to treat the first and or last
> element specially.
>
> The obvious way to handle is to revert to a for int i= loop and check
> for special values of i.
>
> You can keep the for:each style if you have a boolean first= true that
> you set false to detect the first.
>
> I don't know of an equivalent way to detect the last.

The trick for detecting the last element is to defer evaluation of each 
element by one iteration and handle the last one after the loop.

> In the olden days I would have handled the first and last cases
> outside the loop, with the loop running over the middle elements. You
> can't do that with for:each.
>
> What do you consider the best style to deal with this?

For the fun of it, here is one way to do it - although I find it quite 
silly:

package clj;

import java.util.Arrays;

public final class FirstLast {

	private static enum Pos {
		NONE, FIRST, LATER
	}

	public static <T> void firstLast(final Iterable<T> items) {
		Pos p = Pos.NONE;
		T last = null;

		for (final T item : items) {
			switch (p) {
			case NONE:
				p = Pos.FIRST;
				break;
			case FIRST:
				System.out.println("First: " + last);
				p = Pos.LATER;
				break;
			case LATER:
				System.out.println("Next: " + last);
				break;
			}

			last = item;
		}

		if (p != Pos.NONE) {
			System.out.println("Last: " + last);
		}
	}

	public static void main(String[] args) {
		System.out.println("empty");
		firstLast(Arrays.asList());
		System.out.println("one");
		firstLast(Arrays.asList("a"));
		System.out.println("two");
		firstLast(Arrays.asList("a", "b"));
		System.out.println("more");
		firstLast(Arrays.asList("a", "b", "c", "d"));
	}

}

Kind regards

	robert

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

for :each style question Roedy Green <see_website@mindprod.com.invalid> - 2011-11-30 15:32 -0800
  Re: for :each style question Arne Vajhøj <arne@vajhoej.dk> - 2011-11-30 19:28 -0500
    Re: for :each style question Chris Riesbeck <Chris.Riesbeck@gmail.com> - 2011-12-05 14:28 -0600
  Re: for :each style question Knute Johnson <nospam@knutejohnson.com> - 2011-11-30 16:49 -0800
    Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-11-30 18:23 -0800
      Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-10 13:28 +0100
        Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-12 12:07 -0800
          Re: for :each style question Roedy Green <see_website@mindprod.com.invalid> - 2011-12-13 08:08 -0800
            Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-13 12:07 -0800
          Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-17 16:20 +0100
        Re: for :each style question Jim Janney <jjanney@shell.xmission.com> - 2011-12-14 14:31 -0700
          Re: for :each style question Lew <lewbloch@gmail.com> - 2011-12-14 16:53 -0800
            Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-15 16:16 -0800
          Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-17 12:31 +0100
  Re: for :each style question Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-30 21:35 -0500
  Re: for :each style question Lew <lewbloch@gmail.com> - 2011-11-30 19:12 -0800
  Re: for :each style question Robert Klemme <shortcutter@googlemail.com> - 2011-12-01 23:36 +0100
  Re: for :each style question Steven Simpson <ss@domain.invalid> - 2011-12-04 19:43 +0000

csiph-web