Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm rusty but doesn't the increase depend differently, the second adjust lets you decide to assign a value for the variable increase much later, whereas the first adjust fixes the value of increase at the point of evaluating that very assignment statement. There's a name for this, dynamic scoping or something.


Yes, the two are not identical - in the former, increase is not the part of the closure, in the latter it is.

Test:

  increase = True

  adjust1 = (lambda x: x + 1) if increase else (lambda x: x - 1)
  adjust2 = lambda x: x + 1 if increase else x - 1 

  print(adjust1(42))
  print(adjust2(42))

  increase = False

  print(adjust1(42))
  print(adjust2(42))


Yes, my argument relies on immutability of all values (including `increase`). Also, under lazy evaluation we can call `adjust2` immediately, since we know it's a `lambda`; yet attempting to call `adjust1` will force evaluation of `increase`; not good if `increase = provable(collatz_conjecture)`!




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

Search: