package l8er;

import static com.googlecode.totallylazy.Option.none;
import static com.googlecode.totallylazy.Sequences.sequence;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.googlecode.totallylazy.Callable1;
import com.googlecode.totallylazy.Callable2;
import com.googlecode.totallylazy.Predicate;

public class TotallyLazy {

	private static final Predicate<Integer> even = new Predicate<Integer>() {
		@Override public boolean matches(final Integer n) {
			return (n & 1) != 1;
		}
	};

	private static final Predicate<Integer> odd = new Predicate<Integer>() {
		@Override public boolean matches(final Integer n) {
			return !even.matches(n);
		}
	};

	private static final Callable1<Integer, String> toString = new Callable1<Integer, String>() {
		@Override public String call(final Integer n) throws Exception {
			return String.valueOf(n);
		}
	};

	private static final Callable2<Integer, Integer, Integer> sum = new Callable2<Integer, Integer, Integer>() {
		@Override public Integer call(final Integer n, final Integer m) throws Exception {
			return n + m;
		}
	};

	private static final Callable2<Integer, Integer, Integer> add = sum;

	@Test
	public void evenTest() {
		assertFalse(even.matches(7));
		assertTrue(even.matches(14));
		assertTrue(odd.matches(7));
		assertFalse(odd.matches(14));
	}

	@Test
	public void examples() {
		assertThat(sequence(1, 2, 3, 4).filter(even), is(sequence(2, 4)));
		assertThat(sequence(1, 2).map(toString), is(sequence("1", "2")));
		assertThat(sequence(1, 2).mapConcurrently(toString), is(sequence("1", "2")));
		assertThat(sequence(1, 2, 3).take(2), is(sequence(1, 2)));
		assertThat(sequence(1, 2, 3).drop(2), is(sequence(3)));
		assertThat(sequence(1, 2, 3).tail(), is(sequence(2, 3)));
		assertThat(sequence(1, 2, 3).head(), is(1));
		assertThat(sequence(1, 2, 3).reduce(sum), is(6));
		assertThat(sequence(1, 3, 5).find(even), is(none()));
		assertTrue(sequence(1, 2, 3).contains(2));
		assertTrue(sequence(1, 2, 3).exists(even));
		assertFalse(sequence(1, 2, 3).forAll(odd));
		assertThat(sequence(1, 2, 3).foldLeft(0, add), is(6));
		assertThat(sequence(1, 2, 3).toString(), is("1,2,3"));
		assertThat(sequence(1, 2, 3).toString(":"), is("1:2:3"));

	}
}

@@ -534,97 +534,11 @@
  = n
-ew Predicate%3CInteger%3E() %7B%0A%09%09@Override public boolean matches(final Integer n) %7B%0A%09%09%09return
+ -%3E
  (n 
@@ -548,25 +548,16 @@
 ) != 1;%0A
-%09%09%7D%0A%09%7D;%0A%0A
 %09private
@@ -600,97 +600,11 @@
  = n
-ew Predicate%3CInteger%3E() %7B%0A%09%09@Override public boolean matches(final Integer n) %7B%0A%09%09%09return
+ -%3E
  !ev
@@ -614,33 +614,24 @@
 matches(n);%0A
-%09%09%7D%0A%09%7D;%0A%0A
 %09private sta
@@ -682,368 +682,106 @@
 g = 
-new Callable1%3CInteger, String%3E() %7B%0A%09%09@Override public String call(final Integer n) throws Exception %7B%0A%09%09%09return String.valueOf(n);%0A%09%09%7D%0A%09%7D;%0A%0A%09private static final Callable2%3CInteger, Integer, Integer%3E sum = new Callable2%3CInteger, Integer, Integer%3E() %7B%0A%09%09@Override public Integer call(final Integer n, final Integer m) throws Exception %7B%0A%09%09%09return n + m;%0A%09%09%7D%0A%09%7D;%0A
+String::valueOf;%0A%09private static final Callable2%3CInteger, Integer, Integer%3E sum = (n, m) -%3E n + m;
 %0A%09pr

Use lambdas and method references