Hacker News new | ask | show | jobs
by pazderski 959 days ago
The example to find suspicious temperature changes (btw Kelvin does not use ° anymore since 1968) is unnecessary complex.

  List<List<Reading>> findSuspicious(Stream<Reading> source) {
    var suspicious = new ArrayList<List<Reading>>();
    Reading previous = null;
    for (Reading next : source.toList()) {
        if (previous != null && isSuspicious(previous, next))
            suspicious.add(List.of(previous, next));
        previous = next;
    }
    return suspicious;
  }
Doesn't look bad to me. Actually the streaming example here is quite underwhelming. It still needs a separate check for the first "iteration" (window.size() == 2). While reading the classic for-loop example I expected that especially that is something the streaming approach can handle better.

In my opinion people overdo streams. I'm not sure I ever saw someone actually using the parallelization which is one of the stronger features of streams. Instead I get entire method bodies with a lot going on all shoved in one line which is for good reason discouraged anywhere else. And streams are tedious to debug. I really dislike when people (auto)refactor a 'for(var x : list) { ... }' into a 'list.stream.forEach(x -> { ... })' without any other benefit/change.

Just this week I encountered a quite crazy case where someone wrote a

   IntStream.iterate(0, i -> i < getSize(), i -> i + 1).mapToObj(this::getObjectByIndex).toList()
Two anonymous classes and who knows how many extra method calls just to not write a simple for-loop where you even could initialize the list with the known size.