Skip to content

Instantly share code, notes, and snippets.

@kiritsuku
Created March 6, 2012 23:19
Show Gist options
  • Save kiritsuku/1989706 to your computer and use it in GitHub Desktop.
Save kiritsuku/1989706 to your computer and use it in GitHub Desktop.
JDK1.8, Project Lambda, test code for list
import java.util.Comparator;
import static java.util.Comparators.*;
import java.util.functions.*;
/**
* The list dependency can be found at: https://gist.github.com/1989662
* <p>
* ATTENTION: This code compiles only with JDK1.8 and project lambda which can
* be found at http://jdk8.java.net/lambda/ and is not production ready.
*
* @author Antoras
* @version 0.1
* @since JDK1.8, Mar 6, 2012
*/
public class ListTest {
final List<Integer> xs = List.of(1, 2, 3, 4, 5);
public static void main(final String... args) {
new ListTest();
}
public ListTest() {
get();
cons();
mkString();
iterate();
sum();
contains_find_exists();
reverse();
map();
flatten();
sort();
stringHandling();
persons();
}
void stringHandling() {
final String str = "Hello world. How are you? This is what I want to know.";
final String s1 = List.from(str)
.filter(new Predicate<Character>() {
public boolean eval(final Character c) {
return "aeiou".indexOf(c) == -1;
}
})
.map(new Mapper<Character, Character>() {
public Character map(final Character c) {
return c < 'o' ? Character.toLowerCase(c) : Character.toUpperCase(c);
}
})
.drop(5)
.take(15)
.mkString();
final String s = List.from(str) // converst the string to a List<Chararcter>
.filter(c -> "aeiou".indexOf(c) == -1) // deletes vowels
.map(c -> c < 'o' ? Character.toLowerCase(c) : Character.toUpperCase(c)) // useless transformation ;)
.drop(5) // deletes the first 5 chars
.take(15) // choses the next 15 chars
.mkString(); // converts the List back to a String
final String ss = List.from(str)
.filter(c -> !isVowel(c))
.map(c -> doStupidThings(c))
.drop(5)
.take(15)
.mkString();
assertEquals(s, ss);
}
boolean isVowel(char c) {
return "aeiou".indexOf(c) != -1;
}
char doStupidThings(char c) {
return c < 'o' ? Character.toLowerCase(c) : Character.toUpperCase(c);
}
void sort() {
final List<Integer> xs = List.of(12, 3, 7, 9, 624, 67, 75, 61, 1, 35);
final List<Integer> ys = List.of(1, 3, 7, 9, 12, 35, 61, 67, 75, 624);
assertEquals(ys, xs.qsort(new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return o1 - o2;
}
}));
assertEquals(ys, xs.qsort((i1, i2) -> i1-i2));
assertEquals(ys, xs.qsort(ListTest::comp));
}
static int comp(Integer i1, Integer i2) {
return i1-i2;
}
void take_drop() {
assertEquals(List.of(1, 2, 3), xs.take(3));
assertEquals(List.of(4, 5), xs.drop(3));
}
void contains_find_exists() {
assertEquals(true, xs.contains(3));
assertEquals(Option.some(4), xs.find(new Predicate<Integer>() {
public boolean eval(final Integer a) {
return a % 4 == 0;
}
}));
assertEquals(Option.some(4), xs.find(x -> x % 4 == 0));
assertEquals(true, xs.exists(new Predicate<Integer>() {
public boolean eval(final Integer a) {
return a % 4 == 0;
}
}));
assertEquals(true, xs.exists(x -> x % 4 == 0));
}
void get() {
assertEquals(xs.get(3), 4);
}
void cons() {
assertEquals(List.of(0, 1, 2, 3, 4, 5), xs.cons(0));
assertEquals(List.of(6, 7, 8, 1, 2, 3, 4, 5), xs.consAll(List.of(6, 7, 8)));
assertEquals(List.of(1, 2, 3, 4, 5, 6, 7, 8), xs.addAll(List.of(6, 7, 8)));
}
void mkString() {
assertEquals("List(1, 2, 3, 4, 5)", xs.toString());
assertEquals("[1, 2, 3, 4, 5]", xs.mkString("[", ", ", "]"));
}
void iterate() {
// Iterates through the elements. No need for the for-each loop any more.
xs.forEach(new Block<Integer>() {
public void apply(final Integer a) {
System.out.print(a);
}
});
System.out.println();
// cool
xs.forEach(x -> System.out.print(x));
System.out.println();
// more cool
xs.forEach(System.out::print);
System.out.println();
}
void sum() {
final int sum1 = xs.foldLeft(0, new Function2<Integer, Integer, Integer>() {
public Integer apply(final Integer p1, final Integer p2) {
return p1 + p2;
}
});
final int sum2 = xs.foldLeft(0, (i1, i2) -> i1 + i2);
final int sum3 = xs.foldLeft(0, ListTest::add);
assertEquals(sum1, 15);
assertEquals(sum2, 15);
assertEquals(sum3, 15);
}
static int add(int i1, int i2) {
return i1+i2;
}
void reverse() {
final List<Integer> ys = List.of(5, 4, 3, 2, 1);
assertEquals(ys, xs.reverse());
}
void map() {
{
final List<Integer> ys = List.of(2, 4, 6, 8, 10);
final List<Integer> zs1 = xs.map(new Mapper<Integer, Integer>() {
public Integer map(final Integer p1) {
return p1 * 2;
}
});
final List<Integer> zs2 = xs.map(i -> i * 2);
assertEquals(ys, zs1);
assertEquals(ys, zs2);
}
{
final List<String> ys = List.of("1?", "2!", "3?", "4!", "5?");
final List<String> zs1 = xs.map(new Mapper<Integer, String>() {
public String map(final Integer p1) {
return p1 + (p1 % 2 == 0 ? "!" : "?");
}
});
final List<String> zs2 = xs.map(i -> i + (i % 2 == 0 ? "!" : "?"));
assertEquals(ys, zs1);
assertEquals(ys, zs2);
}
}
void flatten() {
final List<Object> xs = List.of(1, 2, List.of(3, 4, List.of(5)), List.nil(), 6);
assertEquals(List.of(1, 2, 3, 4, 5, 6), xs.flatten());
}
void assertEquals(final Object expected, final Object actual) {
if (!expected.equals(actual)) {
throw new AssertionError();
}
}
void persons() {
List<Person> xs = List.of(
new Person("hugo", 21),
new Person("franz", 34),
new Person("sarah", 13),
new Person("helmut", 27));
System.out.println("xs: " + xs.mkString("", ", ", ""));
Mapper<Person, Integer> ageSorter = Person::getAge; // inline this line does not work yet
List<Person> zs = xs.qsort(comparing(ageSorter));
System.out.println("zs:" + zs);
List<Person> ys = xs
.map(p -> new Person(p.getName(), p.getAge()+1))
.filter(p -> p.getAge() < 27);
System.out.println("ys: " + ys.mkString("", ", ", ""));
int sumOfAges = ys
.map(Person::getAge)
.reduceLeft((a1, a2)-> a1+a2);
System.out.println(sumOfAges);
}
}
class Person {
private final String name;
private final int age;
public Person(final String name, final int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
// TODO return type int does not work with inlined lambdas
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "Person(" + name + ", " + age + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment