Ruby
class CountFromTo
def initialize ( pFrom , pTo , pStep )
@mFrom = pFrom
@To = pTo
@mStep = pStep
end
def CountNow
while @mFrom < @To do
@mFrom = @mFrom + @mStep
yield @mFrom
end
end
end
theCls = CountFromTo.new 3 , 50 , 5
theCls.CountNow {|i| puts "The i #{i}" }
Python
class CounterFromTo:
def __init__ (self , pFrom , pTo , pStep):
self.pFrom = pFrom
self.mTo = pTo
self.mStep = pStep
def __iter__ (self):
return self
def next (self ):
if self.current > self.high:
raise StopIteration
valueToRet = self.mFrom
self.mFrom += self.mStep
return valueToRet
for nextI in CounterFromTo ( 3 , 40 , 4 ):
print ( nextI)
and
def CounterFromTo ( pFrom , pTo , pStep):
current = pFrom
while current <= pTo:
yield current
current += pStep
for nextI in CounterFromTo ( 3 , 40 , 4 ):
print ( nextI)
and
gen = (n for n in xrange(0,11))
def __init__ (self , pFrom , pTo , pStep):
self.pFrom = pFrom
self.mTo = pTo
self.mStep = pStep
def __iter__ (self):
return self
def next (self ):
if self.current > self.high:
raise StopIteration
valueToRet = self.mFrom
self.mFrom += self.mStep
return valueToRet
for nextI in CounterFromTo ( 3 , 40 , 4 ):
print ( nextI)
current = pFrom
while current <= pTo:
yield current
current += pStep
for nextI in CounterFromTo ( 3 , 40 , 4 ):
print ( nextI)
gen = (n for n in xrange(0,11))
אין תגובות:
הוסף רשומת תגובה