Live data from Hacker News

Java Streams and State

blog.frankel.ch

31–33 of 33 posts

Re: Java Streams and State

#31
post #22

Earlier quoted context omitted.

I'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.

When 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.…

[deleted]

Re: Java Streams and State

#32
post #22

Earlier quoted context omitted.

I'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.

When 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.…

Readable to me, and I assume most other Java programmers. Here's an example I recently came across [0]:

Option A:

  static  Map transform(Map input,
    Function function) {
      return input.keySet().stream()
          .collect(Collectors.toMap(Function.identity(),
                                    key -> function.apply(input.get(key))));
  }
Option B:

  static  Map transform1(Map input,
    Function function) {
      Map 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

Re: Java Streams and State

#33
post #32

Earlier quoted context omitted.

When 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.…

Readable to me, and I assume most other Java programmers. Here's an example I recently came across [0]: Option A: static Map transform(Map input, Function function) { return input.keySet().stream() .collect(Collectors.toMap(Function.identity(), key -> function.apply(input.get(key)))); } Option B: static Map transform1(Map input, Function function) { Map result = new HashMap (); input.forEach((k, v) -> result.put(k, f…

everytime I use generic statically typed oop I can't stop thinking about clojure or ml languages.. The amount of noise is deafening.
Post reply on HN