Live data from Hacker News

Java Streams and State

blog.frankel.ch

11–20 of 33 posts

Re: Java Streams and State

#11

> Notice how state was introduced? It made the code easier to read. That quote shows that the author kinda misses the point here. type FibPair = (Int, Int) fibSeed :: FibPair fibSeed = (0, 1) fibNext :: FibPair -> FibPair fibNext (p, v) = (v, v + p) fibList :: [FibPair] fibList = iterate fibNext fibSeed The above code does exactly the same as his Fibonacci example, and it's written in pure Haskell. I'd argue the abov…

Right, his Fibonacci class is completely immutable, i.e. it's purely functional.

He's confusing different usages of the word "state" - exhortations to avoid state are almost always about mutable state, but he doesn't appear to recognize that distinction.

Re: Java Streams and State

#12

> Notice how state was introduced? It made the code easier to read. That quote shows that the author kinda misses the point here. type FibPair = (Int, Int) fibSeed :: FibPair fibSeed = (0, 1) fibNext :: FibPair -> FibPair fibNext (p, v) = (v, v + p) fibList :: [FibPair] fibList = iterate fibNext fibSeed The above code does exactly the same as his Fibonacci example, and it's written in pure Haskell. I'd argue the abov…

That code is not at all readable, even knowing Haskell.

Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it.

    vals = {}

    def fib(n):
      if n 

Re: Java Streams and State

#13

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?

I wonder how people who stopped suff.. writing java before java 8 feel about lambda expressions and streams.

With a great relief.

Re: Java Streams and State

#14
post #11

> Notice how state was introduced? It made the code easier to read. That quote shows that the author kinda misses the point here. type FibPair = (Int, Int) fibSeed :: FibPair fibSeed = (0, 1) fibNext :: FibPair -> FibPair fibNext (p, v) = (v, v + p) fibList :: [FibPair] fibList = iterate fibNext fibSeed The above code does exactly the same as his Fibonacci example, and it's written in pure Haskell. I'd argue the abov…

Right, his Fibonacci class is completely immutable, i.e. it's purely functional. He's confusing different usages of the word "state" - exhortations to avoid state are almost always about mutable state, but he doesn't appear to recognize that distinction.

Tell that to the person at work who recently came back from a FP workshop and has declared war on all state...

Developers to tend to go from extremes.

Re: Java Streams and State

#15

> Notice how state was introduced? It made the code easier to read. That quote shows that the author kinda misses the point here. type FibPair = (Int, Int) fibSeed :: FibPair fibSeed = (0, 1) fibNext :: FibPair -> FibPair fibNext (p, v) = (v, v + p) fibList :: [FibPair] fibList = iterate fibNext fibSeed The above code does exactly the same as his Fibonacci example, and it's written in pure Haskell. I'd argue the abov…

That code is not at all readable, even knowing Haskell. Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it. vals = {} def fib(n): if n

Your code is doing something pretty different from the examples above. You're just calculating the nth fibonacci number, not creating an infinite fibonacci _sequence_. Eliding the explicit recursive structure is the point of the exercise!

Admittedly Python's facilities for doing that with some mutable state are pretty nice:

  from itertools import islice

  def fib():
      a, b = 0, 1
      while True:
          yield a
          a, b = b, a + b

  list(islice(fib(), 10)) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Re: Java Streams and State

#16
It's a shame the author never actually ran the code, it has bugs.

For example, the Factorial next() function should be:

  return new Factorial (index + 1, value * (index + 1));
And of course the iterate line should be:

  Stream.iterate (Factorial.SEED, Factorial::next)

Re: Java Streams and State

#17
post #13

Earlier quoted context omitted.

I wonder how people who stopped suff.. writing java before java 8 feel about lambda expressions and streams.

With a great relief.

That's my opinion too, I don't think I can consider ever touching java prior r8.

Re: Java Streams and State

#18
I don't think the stateful approach with .generate() is thread-safe if you make the stream parallel. The API doc says it's an unordered stream, parallelizing it would mean concurrent reads/writes in the supplier.

Stream.iterate() docs on the other hand guarantee happens-before ordering for invocations.

Re: Java Streams and State

#19
post #14
post #11

Earlier quoted context omitted.

Right, his Fibonacci class is completely immutable, i.e. it's purely functional. He's confusing different usages of the word "state" - exhortations to avoid state are almost always about mutable state, but he doesn't appear to recognize that distinction.

Tell that to the person at work who recently came back from a FP workshop and has declared war on all state... Developers to tend to go from extremes.

The question is what he means by "all state".

If we take the OP post as definitional, then eliminating state means eliminating compound data structures, because that's the usage of "state" implied by that Fibonacci example.

Hopefully the person at your work has something less extreme in mind. Ask him to explain what he means by "state".

Re: Java Streams and State

#20

Earlier quoted context omitted.

That code is not at all readable, even knowing Haskell. Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it. vals = {} def fib(n): if n

Your code is doing something pretty different from the examples above. You're just calculating the nth fibonacci number, not creating an infinite fibonacci _sequence_. Eliding the explicit recursive structure is the point of the exercise! Admittedly Python's facilities for doing that with some mutable state are pretty nice: from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a + b list(i…

I wasn't speaking to the goal of generating a sequence that can be lazily evaluated.

I was speaking to readability.

Post reply on HN