Skip to content

Commit

Permalink
Added more stuff for presentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen K. Peterson committed Jun 11, 2015
1 parent 0b88eb0 commit fb46e2e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 36 deletions.
Binary file modified presentation.pptx
Binary file not shown.
112 changes: 76 additions & 36 deletions src/test/java/EasyCollectionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@

import org.junit.Test;
import org.organicdesign.fp.collections.ImList;
import org.organicdesign.fp.collections.ImMapOrdered;
import org.organicdesign.fp.collections.PersistentTreeMap;
import org.organicdesign.fp.collections.ImMap;
import org.organicdesign.fp.collections.PersistentHashMap;
import org.organicdesign.fp.collections.PersistentVector;
import org.organicdesign.fp.collections.UnMap.UnEntry;
import org.organicdesign.fp.tuple.Tuple2;

import java.awt.*;
import java.util.Comparator;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -91,55 +95,91 @@ public class EasyCollectionsTest {

public static void println(Object s) { System.out.println(String.valueOf(s)); }

public static Comparator<Color> COLOR_COMP =
(a, b) -> (a.getRed() + a.getGreen() + a.getBlue()) - (b.getRed() + b.getGreen() + b.getBlue());
// public static Comparator<Color> COLOR_COMP =
// (a, b) -> (a.getRed() + a.getGreen() + a.getBlue()) - (b.getRed() + b.getGreen() + b.getBlue());

// public static <T> T ex(T t) throws IOException { return t; }

@Test public void colorSquare() {
ImList<Color> imgData = PersistentVector.empty();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
imgData = imgData.append(new Color(i, j, 255));
imgData = imgData.append(new Color(i, (i + j) / 2, 255));
}
}
println("imgData: " + imgData);

ImMapOrdered<Color,Integer> counts = imgData
.foldLeft(PersistentTreeMap.ofComp(COLOR_COMP),
ImMap<Color,Integer> counts = imgData
.foldLeft(PersistentHashMap.empty(),
(accum, c) -> accum.assoc(c, accum.getOrElse(c, 0) + 1));

println("counts: " + counts);

UnEntry<Color,Integer> mostPopularColor = counts
UnEntry<Color,Integer> mostPopularColor = counts.seq()
.foldLeft((UnEntry<Color,Integer>) Tuple2.of((Color) null, 0),
(max, entry) ->
(entry.getValue() > max.getValue()) ? entry : max);
(max, entry) -> (entry.getValue() > max.getValue()) ? entry : max);

println("mostPopularColor: " + mostPopularColor);
println("number of unique colors: " + counts.size());
}

// // TODO: Shows bug in PersistentHashMap (see counts.toString())
// @Test public void colorSquare2() {
// ImList<Color> imgData = PersistentVector.empty();
// for (int i = 0; i < 256; i++) {
// for (int j = 0; j < 256; j++) {
// imgData = imgData.append(new Color(i, j, 255));
// }
// }
// println("imgData: " + imgData);
//
// ImMap<Color,Integer> counts = imgData
// .foldLeft(PersistentHashMap.empty(),
// (accum, c) -> accum.assoc(c, accum.getOrElse(c, 0) + 1));
//
// println("counts: " + counts);
//
// UnEntry<Color,Integer> mostPopularColor = counts.seq()
// .foldLeft((UnEntry<Color,Integer>) Tuple2.of((Color) null, 0),
// (max, entry) ->
// (entry.getValue() > max.getValue()) ? entry : max);
//
// println("mostPopularColor: " + mostPopularColor);
// println("number of unique colors: " + counts.size());
// }
@Test public void colorSquare2() {
List<Color> imgData = new ArrayList<>();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
imgData.add(new Color(i, (i + j) / 2, 255));
}
}
println("imgData: " + imgData.toString().substring(0, 50));

Map<Color,Integer> counts = new HashMap<>();
for (Color c : imgData) {
counts.put(c, counts.getOrDefault(c, 0) + 1);
}

println("counts: " + counts.toString().substring(0, 50));

Map.Entry<Color,Integer> max = Tuple2.of((Color) null, 0);
for (Map.Entry<Color,Integer> entry : counts.entrySet()) {
if (entry.getValue() > max.getValue()) {
max = entry;
}
}

println("mostPopularColor: " + max);
println("number of unique colors: " + counts.size());
}

@Test public void colorSquare3() {
List<Color> imgData = new ArrayList<>();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
imgData.add(new Color(i, (i + j) / 2, 255));
}
}
println("imgData: " + imgData.toString().substring(0, 50));

Map<Color,Integer> counts = imgData.stream()
.reduce(new HashMap<>(),
(HashMap<Color,Integer> accum, Color c) -> {
accum.put(c, accum.getOrDefault(c, 0) + 1);
return accum;
},
(HashMap<Color,Integer> accum1, HashMap<Color,Integer> accum2) -> {
for (Map.Entry<Color,Integer> e : accum2.entrySet()) {
Color key = e.getKey();
accum1.put(key, accum1.getOrDefault(key, 0) + e.getValue());
}
return accum1;
});

println("counts: " + counts.toString().substring(0, 50));

Optional<Map.Entry<Color,Integer>> mostPopularColor = counts.entrySet().stream().max((e1, e2) -> e1.getValue() - e2.getValue());

println("mostPopularColor: " + mostPopularColor.get());
println("number of unique colors: " + counts.size());
}


}

0 comments on commit fb46e2e

Please sign in to comment.