5 ms·
As someone who hasn't written Java since college, I am not familiar with streams. Is this similar in principle to a Python/JS generator function?
by bastawhiz 8y ago
As someone who hasn't written Java since college, I am not familiar with streams. Is this similar in principle to a Python/JS generator function?
- agumonkey 8y agoI wonder how people who stopped suff.. writing java before java 8 feel about lambda expressions and streams.
- nine_k 8y agoWith a great relief.
- agumonkey 8y agoThat's my opinion too, I don't think I can consider ever touching java prior r8.
- MarkMc 8y agoI've been writing Java for 20 years. Streams are a definite improvement, but half the time I find myself rewriting a streams approach to an old-fashioned loop to make the code more readable.
- firepoet 8y agoWhen you say "readable," do you mean to OO programmers? Functional programmers? All programmers? For example, as a 5-year Clojure convert who wrote Java for 15 years before that, old-fashioned loops are less readable now, as they hide the essentials of what is really happening. For example, you might be mapping a collection from one type of value to another, or reducing it to some other form, etc. But both use loops. You have to scan all over the place in the loop construct to see what it's really doing. I'd much rather see the function that is happening than the procedure by which the function is accomplished.
- deleted 8y ago[deleted]
- MarkMc 8y agoReadable to me, and I assume most other Java programmers. Here's an example I recently came across [0]: Option A: static <X, Y, Z> Map<X, Z> transform(Map<? extends X, ? extends Y> input, Function<Y, Z> function) { return input.keySet().stream() .collect(Collectors.toMap(Function.identity(), key -> function.apply(input.get(key)))); } Option B: static <X, Y, Z> Map<X, Z> transform1(Map<? extends X, ? extends Y> input, Function<Y, Z> function) { Map<X, Z> result = new HashMap<>(); input.forEach((k, v) -> result.put(k, function.apply(v))); return result; } Personally I find Option B more readable. Another example: Option A: protected boolean isCashBasis(CalendarDate effectiveDate) { return priorVatReturns.stream() .filter(r -> r.period.contains(effectiveDate)) .findFirst() .map(r -> r.basis == AccountingBasis.CASH_BASIS) .orElseGet(() -> super.isCashBasis(effectiveDate)); } Option B: protected boolean isCashBasis(CalendarDate effectiveDate) { for (VatReturn r : priorVatReturns) { if (r.period.contains(effectiveDate)) { return r.basis == AccountingBasis.CASH_BASIS; } } return super.isCashBasis(effectiveDate); } Again I would choose Option B, although I suppose Option A isn't too bad. [0] https://stackoverflow.com/a/25905196/76295 https://stackoverflow.com/a/25905196/76295
- tristanperry 8y agoEssentially yes - a Java stream returns an iterable object, which we can perform further operations such as filtering down, mapping to something else or 'collecting' it into another object (et al).
- random3 8y agoStreams are a bit more general. They allow you to use functional operations on sequence of elements (e.g. map, filter, reduce, etc.) to build a processing pipeline which can execute async and in parallel. ``` widgets.stream() .filter(b -> b.getColor() == RED) .mapToInt(b -> b.getWeight()) .sum(); ``` https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html https://docs.oracle.com/javase/8/docs/api/java/util/stream/p... They aren't specific to Java. In fact they require features such as lambdas that made them less practical until Java 8.
- bunderbunder 8y agoIt's more comparable to Python iterators in combination with itertools, sequence comprehensions, and a few other things. It's arguably more powerful in that it's got some more bits and bobs out-of-the-box, and it's easier to do parallel stuff with it. But it's also arguably less powerful in that it's not particularly open for anyone who isn't a JDK developer to adapt for their own purposes. To make your own Python iterator that will interact nicely with anything else that's designed to operate on iterators, you only have one method you have to implement. To make your own implementation of Java's Stream interface, you've got a rather daunting list of 40 you have to implement.