Last active
August 29, 2015 14:04
-
-
Save ychaouche/f30d5e07012454fff0d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Suppose you want to write a program that asks the user for X numbers and store them in A, then for Y numbers and store them in B | |
| A = [] | |
| B = [] | |
| # First possibilty : using while loops | |
| # you need to have a counter | |
| i = 0 | |
| while (i < X): | |
| A.append(input()) | |
| # and increment it yourself | |
| i+=1 | |
| # you need to reset it | |
| i = 0 | |
| while (i < Y): | |
| B.append(input()) | |
| # and increment it again | |
| i+=1 | |
| So basically you need to repeat a certain thing x times, then another Y times, but python has only while loops and for loops. The while loop is not to repeat a certain thing N times, but to repeat things while a certain condition is True, that's why you have to use a counter, initialize it, increment it, and test against it. | |
| Second solution is to use for loops : | |
| # No need to create a counter | |
| for x in xrange(x): | |
| A.append(input()) | |
| # No need to increment the counter | |
| # no need to reset the counter | |
| for x in xrange(Y): | |
| B.append(input()) | |
| This is a lot better :), but there's still something slightly anoying : why would I still have to supply "x", when I don't need x ? In my code, I don't need to know in what loop iteration I am, i just need to repeat things N times, so why do I have to create a counter at all ? Isn't there a way to completely get rid of counters ? imagine a new kind of loop would be invented in python, like so : | |
| # No need to supply any variable ! | |
| repeat(x): | |
| A.append(input()) | |
| repeat(Y): | |
| B.append(input()) | |
| Wouldn't that be so cool ? unfortunately, python isn't flexible enough to allow for creating new language constructs (more advanced languages allow this). So I still have to use either for or while to loop over things. With that restriction in mind, how do I get the most closer possible to the upper code ? here's what I tried : | |
| def repeat(limit): | |
| if hasattr(repeat,"counter"): | |
| repeat.counter += 1 | |
| return repeat.counter < limit | |
| repeat.limit = limit | |
| repeat.counters = 0 | |
| return limit > 0 and True | |
| while repeat(x): | |
| A.append(input()) | |
| while repeat(Y): | |
| B.append(input()) | |
| This works fairly well for single nested loops. But it obviously doesn't work for nested loops. So here's another version that cares for nested loops : | |
| The question is : do you know any other possible implementation that would allow me to get the same interface as repeat ? | |
| Here is why all these so far suggested solutions (on #python) aren't what I want : | |
| suggested solution 1.1 : use itertools.repeat | |
| 1) you'll have to place your code inside a function and call that function inside itertools.repeat, somehow. It's different from the above repeat implementation where you can use it over arbitrary indented blocks of code. | |
| 2) even if it was inside a function, I can't figure out how to call it N times. | |
| 3) even if you do call it N times using itertools.repeat, see point 1). | |
| suggestion solution 1.2 : use itertools.repeat in a for loop | |
| import itertools | |
| for _ in itertools.repeat(None, N): | |
| do_something() | |
| you're still writing a for loop providing _ and not using it, which completely misses the point, look again : | |
| while repeat(5): | |
| do_x() | |
| do_y() | |
| do_z() | |
| Nothing is provided just for the sake of looping. | |
| suggested solution 2 : use itertools.count | |
| 1) you're half the way there. Yes, you get rid of manually incrementing the counter but you still need for a counter to be created manually. Example : | |
| c = itertools.count(1,1) # You still need this line | |
| while( c.next() < 7): | |
| a = 1 | |
| b = 4 | |
| d = 59 | |
| print "haha" | |
| # No need to increment anything, that's good. | |
| # c.incr() or whatever that would be | |
| suggested solution 3 : use a with statement | |
| I have never used it, but from what I saw, it's used for sweeping the try/catch code inside another function. How can you use this to repeat things ? I don't see | |
| suggested solution 4 : use an iterator, xrange, list comprehensions... | |
| No, no, no... | |
| If you write : | |
| [X() for _ in xrange(4)] | |
| you are : | |
| 1) creating a list for values you don't care about. | |
| 2) supplying a counter (_) | |
| if you write : | |
| for _ in xrange(4): | |
| X() | |
| see point 2) above | |
| if you write : | |
| def repeat(n): | |
| i = 0 | |
| while i < n : | |
| yield i < n | |
| i+1 | |
| Then how are you supposed to use repeat ? like this ? : | |
| for x in repeat(5): | |
| X() | |
| then see point 2) above. Also, this is basically xrange, so why not use xrange instead. | |
| suggested solution 4 : | |
| todos = [do_something] * N | |
| for doit in todos: | |
| doit() | |
| 1) You'll consume memory if N is very big. | |
| 2) You still have to embed your code inside a function. | |
| Any other cool ideas ? This is a serious question. Some people asked me what language do I come from, because they're not familiar with this repeat(N) syntax. My main programming language is python, but I'm sure I've seen this syntax in other languages. A visit of my bookmarks followed by online search shows that the following languages have this cool syntax : | |
| * [Ruby](http://ruby.about.com/od/rubyfeatures/a/loops_2.htm), it's just called times instead of repeat. | |
| * [Netlogo](http://ccl.northwestern.edu/netlogo/docs/dict/repeat.html) | |
| * [AppleScript](http://computers.tutsplus.com/tutorials/save-time-and-effort-with-applescript-repeat-loops--mac-45805) | |
| * [Verliog](http://www.asic-world.com/verilog/vbehave3.html) | |
| * [Rebol](http://re-bol.com/rebol.html#section-6.6.3) | |
| * [Forth](http://www.forth.com/starting-forth/sf6/sf6.html), : LABEL 10 0 DO something LOOP will repeat something 10 times, and you didn't have to supply a counter (Forth automatically stores it in a special variable I). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was the guy having the silly idea working with a "with" statement which, after some insight,
is utter bullshit. However I managed to hack a fake reentrantable repeat here, i'm sure you'll get the idea pretty fast, else feel free to shoot me a mail and we'll meet in #python again:
For human readability, the index built contains the filename and linenumber of the callee, so it might explode when you put two repeats in the same line.