Skip to content

Commit 3e7157b

Browse files
committed
ordering of special/general cases in ifs with return and omitted else
1 parent 0d0a5ab commit 3e7157b

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

unpythonic/env.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ def __getattr__(self, name):
7878
if not name.isidentifier():
7979
raise ValueError("'{}' is not a valid identifier".format(name))
8080
e = self._env # __getattr__ not called if direct attr lookup succeeds, no need for hook.
81-
if name in e:
82-
return e[name]
83-
else:
81+
if name not in e:
8482
raise AttributeError("name '{:s}' is not defined".format(name))
83+
return e[name]
8584

8685
# membership test (in, not in)
8786
def __contains__(self, k):

unpythonic/fploop.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,20 @@ def _(loop, i=0):
422422
# same without using side effects - use an accumulator parameter:
423423
@looped
424424
def out(loop, i=0, acc=[]):
425-
if i < 3:
426-
acc.append(i)
427-
return loop(i + 1)
428-
return acc
425+
if i >= 3:
426+
return acc
427+
acc.append(i)
428+
return loop(i + 1)
429429
assert out == [0, 1, 2]
430430

431431
# there's no "continue"; package your own:
432432
@looped
433433
def s(loop, acc=0, i=0):
434434
cont = lambda newacc=acc: loop(newacc, i + 1)
435-
if i <= 4:
436-
return cont()
437-
elif i == 10:
435+
if i == 10:
438436
return acc
437+
elif i <= 4:
438+
return cont()
439439
return cont(acc + i)
440440
assert s == 35
441441

@@ -493,9 +493,9 @@ def filter_fp(predicate, iterable):
493493
predicate = predicate or (lambda x: x) # None -> truth value test
494494
@looped_over(iterable, acc=())
495495
def out(loop, x, acc):
496-
if predicate(x):
497-
return loop(acc + (x,))
498-
return loop(acc)
496+
if not predicate(x):
497+
return loop(acc)
498+
return loop(acc + (x,))
499499
return out
500500
assert filter_fp(lambda x: x % 2 == 0, range(10)) == (0, 2, 4, 6, 8)
501501

@@ -629,9 +629,9 @@ def result():
629629
@looped
630630
def s(loop, acc=0, i=0):
631631
cont = lambda newacc=acc: loop(newacc, i + 1)
632-
if i < 10:
633-
return cont(acc + i)
634-
return acc
632+
if i >= 10:
633+
return acc
634+
return cont(acc + i)
635635
print("s is {:d}".format(s))
636636
return 2 * s
637637
assert result == 90

unpythonic/it.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ def flatten(iterable, pred=None):
441441

442442
def flatten1(iterable, pred=None):
443443
"""Like flatten, but process outermost level only."""
444-
if pred:
445-
return _flatten(iterable, pred, recursive=False)
446-
return chain.from_iterable(iterable) # itertools recipes: fast, no pred
444+
if not pred:
445+
return chain.from_iterable(iterable) # itertools recipes: fast, no pred
446+
return _flatten(iterable, pred, recursive=False)
447447

448448
def _flatten(iterable, pred=None, recursive=True):
449449
pred = pred or (lambda x: True) # unpythonic.fun.const(True), but dependency loop

0 commit comments

Comments
 (0)