3 ms·
You misread the question. I didn't mean (map even? (range 1000)), but a sequence of length 1000 where the elements are 1 * true, 2 * false, 3 * true, 4 * false
by j-pb 11y ago
You misread the question.
I didn't mean (map even? (range 1000)), but a sequence of length 1000 where the elements are 1 * true, 2 * false, 3 * true, 4 * false and so on.
To compare it with what your code produces.
[true false false true true true false false false false ...]
[true false true false true false true false true false ...]
Edited for more clarity.
- deleted 11y ago[deleted]
- dragonwriter 11y agoRight, sorry, in python that's something like: from itertools import chain, repeat chain.from_iterable([repeat(i % 2 != 0, i) for i in range(1,1000)]) In Java 8, I think the solution with streams would be fairly straightforward, something like: IntStream.range(1,1001).flatMap(i -> Stream<Boolean>.generate( () -> (x & 1) != 0 ).limit(i))
- sokoloff 11y agoDoesn't that python code give you a list of 500,500 elements, rather than a list of 1000 elements as originally specified?
- dragonwriter 11y agoIn both cases yes, and in both cases because I made the same stupid mistake when both languages have good means to do infinite streams and "take" equivalents. (And that's worse in the Java case, since I used those in the inner function but not the outer one.) So, I think I need to take a break from comment-box programming for a while; its clearly not working out for me today.