Hacker News new | past | comments | ask | show | jobs | submit login

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.




Right, 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))


Doesn't that python code give you a list of 500,500 elements, rather than a list of 1000 elements as originally specified?


In 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: