source: utilitiespy/tudelft/utilities/tools/Iterator.py@ 1030

Last change on this file since 1030 was 667, checked in by wouter, 12 months ago

added tools to utilities package to include tools for java2python translator

File size: 537 bytes
RevLine 
[667]1
2
3from collections.abc import Iterator
4from itertools import tee
5from typing import Tuple
6
7def hasNext(it)->Tuple[bool, Iterator]:
8 '''
9 A support function to test hasNext for standard python iterators.
10 usage, assuming it is already created (eg it=iter([1,2,3]):
11 nx, it = hasNext(it)
12
13 After the call, nx will be True iff it has a next value.
14 The new it will be in original state (we have to damage the original iterator)
15 '''
16
17 it,it1=tee(it)
18 try:
19 next(it1)
20 return True, it
21 except:
22 return False, it
23
Note: See TracBrowser for help on using the repository browser.