Merge: Steps on iterations and ranges
authorJean Privat <jean@pryen.org>
Thu, 21 May 2015 00:32:31 +0000 (20:32 -0400)
committerJean Privat <jean@pryen.org>
Thu, 21 May 2015 00:32:31 +0000 (20:32 -0400)
commit77e92d228da5a9da74663a5146a3b2d40e774fc1
tree640b1ec9b12f49aff8ba18c60da94e3ce294fe92
parent479924a2c3bc6cd92ce606f2dc2e1d51d7093cd7
parentc2b2e99ab23b4e04090573e9bba6bc10a673ec87
Merge: Steps on iterations and ranges

A proposal to solve some things identified in #1339

This PR does 3 things:

* extends any Iterator to locally (`next_by`) or globally (`to_step`) advance with more than a single `next`
* add `Range.step` to have a generic bidirectional and stepable iterator

So now people can write

~~~nit
for i in [1..10].step(2) do print i # in order 1 3 5 7 9
for i in [10..1].step(-2) do print i # in reverse order 10 8 6 5 2
~~~

This is a neat (over?-) engineering. A maybe non-POLA thing is that empty ranges can still be iterated with a negative step.

~~~nit
assert [5..1].is_empty # but
assert [5..1].step(-2).to_a == [5,3,1]
~~~

Pull-Request: #1347
Reviewed-by: ArthurDelamare <arthur.delamare@viacesi.fr>
Reviewed-by: Etienne M. Gagnon <egagnon@j-meg.com>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Ait younes Mehdi Adel <overpex@gmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
lib/standard/collection/abstract_collection.nit
lib/standard/collection/range.nit