3 ms·
This hurts my eyes, it doesn't look like Java, what would this look like in Java: int sum = widgets.stream() .filter(b -> b.getColor
by jebblue 13y ago
This hurts my eyes, it doesn't look like Java, what would this look like in Java:
int sum = widgets.stream()
.filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum();
- talklittle 13y agoList<Widget> filtered = filter(widgets, new Filter<Widget>() { @Override public boolean keep(Widget w) { return w.getColor() == RED; } }); List<Integer> weights = mapToInt(filtered, new Mapper<Widget, Integer>() { @Override public Integer map(Widget w) { return w.getWeight(); } }); int sum = sum(weights);
- jebblue 13y agoNice, thanks! Now _that_ I get.
- overgard 13y agoSeems too short. Maybe you could split it into 5 files? You don't even have a factory. Much less a factory factory. This code will never cut it in the enterprise world.
- jebblue 13y agoIt turns out there's a good blog article that explains why the less enterprisey way is better since it remove the iteration loop of the collection and lets the computer [figure out how to express the sum of weights of the red objects in a collection of blocks]: http://blog.hartveld.com/2013/03/jdk-8-33-stream-api.html http://blog.hartveld.com/2013/03/jdk-8-33-stream-api.html I've been around too long to trust the computer to figure out how best to do something. If I'm wrong then Java 8 leads the way to the end of programmers, similar to how robotics mostly ended how cars were manufactured.
- Jayschwa 13y agoint sum = 0; for (w : widgets) { if (w.getColor() == RED) sum += w.getWeight(); }