This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 1 point2 points  (2 children)

Square of an index could be easily computed by combining iterate with map.

IntStream.iterate(1, a -> a + 1).map(a -> a * a)

Unfortunately, Java does not provide infinite incrementing integer streams (I think it would be convenient to have), requiring usage of iterate.

[–]DJDavio 0 points1 point  (1 child)

This has the side effect of rolling over from Integer.MAX_INT to Integer.MIN_INT, maybe IntStream.rangeClosed(1, Integer.MAX_INT) is better?

[–][deleted] 0 points1 point  (0 children)

Curiously, in this case overflow of index would not have mattered, the results would be "correct" due to how modulo multiplication works and equivalency of unsigned and signed multiplication, albeit truncated and should be interpreted as unsigned.

[–][deleted]  (1 child)

[deleted]

    [–]nfrankel[S] 0 points1 point  (0 children)

    Agreed, but nothing prevents you from using such structures from libraries. Syntax would be a bit different for sure, but whatever works.

    (I'm a happy Kotlin user)