diff --git a/presentation.pptx b/presentation.pptx index 6c0923e..b90c7e9 100644 Binary files a/presentation.pptx and b/presentation.pptx differ diff --git a/src/test/java/EasyCollectionsTest.java b/src/test/java/EasyCollectionsTest.java index 73c1cd9..edf9442 100644 --- a/src/test/java/EasyCollectionsTest.java +++ b/src/test/java/EasyCollectionsTest.java @@ -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; @@ -91,55 +95,91 @@ public class EasyCollectionsTest { public static void println(Object s) { System.out.println(String.valueOf(s)); } - public static Comparator COLOR_COMP = - (a, b) -> (a.getRed() + a.getGreen() + a.getBlue()) - (b.getRed() + b.getGreen() + b.getBlue()); +// public static Comparator COLOR_COMP = +// (a, b) -> (a.getRed() + a.getGreen() + a.getBlue()) - (b.getRed() + b.getGreen() + b.getBlue()); + +// public static T ex(T t) throws IOException { return t; } @Test public void colorSquare() { ImList 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 counts = imgData - .foldLeft(PersistentTreeMap.ofComp(COLOR_COMP), + ImMap counts = imgData + .foldLeft(PersistentHashMap.empty(), (accum, c) -> accum.assoc(c, accum.getOrElse(c, 0) + 1)); println("counts: " + counts); - UnEntry mostPopularColor = counts + UnEntry mostPopularColor = counts.seq() .foldLeft((UnEntry) 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 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 counts = imgData -// .foldLeft(PersistentHashMap.empty(), -// (accum, c) -> accum.assoc(c, accum.getOrElse(c, 0) + 1)); -// -// println("counts: " + counts); -// -// UnEntry mostPopularColor = counts.seq() -// .foldLeft((UnEntry) 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 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 counts = new HashMap<>(); + for (Color c : imgData) { + counts.put(c, counts.getOrDefault(c, 0) + 1); + } + + println("counts: " + counts.toString().substring(0, 50)); + + Map.Entry max = Tuple2.of((Color) null, 0); + for (Map.Entry 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 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 counts = imgData.stream() + .reduce(new HashMap<>(), + (HashMap accum, Color c) -> { + accum.put(c, accum.getOrDefault(c, 0) + 1); + return accum; + }, + (HashMap accum1, HashMap accum2) -> { + for (Map.Entry 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> mostPopularColor = counts.entrySet().stream().max((e1, e2) -> e1.getValue() - e2.getValue()); + + println("mostPopularColor: " + mostPopularColor.get()); + println("number of unique colors: " + counts.size()); + } + + }