From e7ec95d3350da6e6df9254db958ae2799ee66b9d Mon Sep 17 00:00:00 2001 From: Martin Grandrath Date: Tue, 19 Apr 2016 08:53:53 +0200 Subject: [PATCH] Remove bug and unnecessary `if` statement The condition of the `while` loop was incorrect. Also when you replace the default value for `to` you can get rid of the `if` condition. --- _posts/2016-04-15-laziness-is-a-virtue.md | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/_posts/2016-04-15-laziness-is-a-virtue.md b/_posts/2016-04-15-laziness-is-a-virtue.md index cfa82ca0c..d28c44f7b 100644 --- a/_posts/2016-04-15-laziness-is-a-virtue.md +++ b/_posts/2016-04-15-laziness-is-a-virtue.md @@ -219,18 +219,11 @@ Let's take a pass at writing the Sieve of Eratosthenes in lazy style. First off, [ja]: https://leanpub.com/javascriptallongesix {% highlight javascript %} -function * range (from = 0, to = null) { +function * range (from = 0, to = Number.MAX_VALUE) { let number = from; - if (to == null) { - while (true) { - yield number++ - } - } - else { - while (from <= to) { - yield number++; - } + while (number <= to) { + yield number++; } }