From dd4fc2d614f878a7181a28a65cf84032f9f0860e Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 14 May 2015 21:14:05 -0400 Subject: [PATCH] standard/collection: add `Iterator::next_by` as a safe multiple `next` Signed-off-by: Jean Privat --- lib/standard/collection/abstract_collection.nit | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/standard/collection/abstract_collection.nit b/lib/standard/collection/abstract_collection.nit index 2f7e454..c33416f 100644 --- a/lib/standard/collection/abstract_collection.nit +++ b/lib/standard/collection/abstract_collection.nit @@ -191,6 +191,37 @@ interface Iterator[E] # Require `is_ok`. fun next is abstract + # Jump to the next item `step` times. + # + # ~~~ + # var i = [11, 22, 33, 44].iterator + # assert i.item == 11 + # i.next_by 2 + # assert i.item == 33 + # ~~~ + # + # `next_by` should be used instead of looping on `next` because is takes care + # of stopping if the end of iteration is reached prematurely whereas a loop of + # `next` will abort because of the precondition on `is_ok`. + # + # ~~~ + # i.next_by 100 + # assert not i.is_ok + # ~~~ + # + # If `step` is negative, this method aborts. + # But specific subclasses can change this and do something more meaningful instead. + # + # Require `is_ok` + fun next_by(step: Int) + do + assert step >= 0 + while is_ok and step > 0 do + next + step -= 1 + end + end + # Is there a current item ? fun is_ok: Bool is abstract -- 1.7.9.5