Page tree
Skip to end of metadata
Go to start of metadata

 

Examples using the ITERATE Function

 ITERATE(recordset, transform [, LOCAL ] )

For these examples use this dataset:

Initial Dataset
integerListRec:=record
    integer num;
end;

integerListEmpy:=loop(dataset([{0}],integerListRec),4,rows(left)+rows(left));

 

Examples:

 

Iterate.ecl
/*integer sequence by using counter*/
Icountup:=ITERATE(integerListEmpy,transform(integerListRec,self.num:=counter));
output(Icountup,,named('ListOfIntegers'));

/*integer sequence by adding one to previous record*/
/*LEFT represents the previous record, after it has been transformed */
ITERATE(integerListEmpy,transform(integerListRec,self.num:=LEFT.num + 1));

/*powers of two by doubling previous record*/
integerListRec doubleUp(integerListRec PreviousRec):=transform
    self.num:=if(PreviousRec.num=0,1,PreviousRec.num*2);
end;
ITERATE(integerListEmpy,doubleUp(LEFT));

/* running sum, transform the count-up list using original record and previous record */
integerListRec runningSum(IntegerListRec PreviousRec,IntegerListRec originalRec):=transform
    self.num:=PreviousRec.num + originalRec.num;
end;
ITERATE(Icountup,runningSum(LEFT,RIGHT));

/*Factorial*/
/*LEFT is always zero value originally. RIGHT represents the original record before the iteration transform*/
integerListRec fact(IntegerListRec PreviousRec,IntegerListRec originalRec):=transform
    self.num:=if(PreviousRec.num=0,1,PreviousRec.num) * originalRec.num;
end;
ITERATE(Icountup,fact(LEFT,RIGHT));
 
/*Fibonacci sequence by retaining previous value in an extra column*/
fibRec:=record
	integer num;
	integer prev;
end;
initialFib:=dataset([{1,0}],fibRec); //base condition
fibListEmpty:=initialFib + loop(dataset([{0,0}],fibRec),4,rows(left)+rows(left)); 
fibListEmpty;  //dataset with base condition in first record

fibRec fibStep(fibRec Prevrec, fibRec originalRec):=transform
self.prev:=if(Prevrec.num=0,originalRec.prev,Prevrec.num);
self.num:=if(Prevrec.num=0,originalRec.num,Prevrec.num + Prevrec.prev);
end;

ITERATE(fibListEmpty,fibStep(left,right));

/*Iterate using a Function, but not directly.  Wrap it in the Transform */
/*the function must return a row of the transform's record structure*/
fibRec fibFunction(fibRec Prevrec, fibRec originalRec,integer Cnt):=function
	r:= row({Prevrec.num + Prevrec.prev,Prevrec.num},fibRec);
	return if(Cnt=1, originalRec,r);
end;

fibRec fibFuncStep(fibRec Prevrec, fibRec originalRec,Cnt):=transform
	self:=fibFunction(prevRec,originalRec,Cnt);
end;

ITERATE(fibListInitial,fibFuncStep(left,right,counter));
 
/*summing Euler series approximation of pi*/
/*using a function of counter then summing the series*/
piRec:=record
    real8 pi;
    real8 partialSum;
end;

piEmpty(integer Log2NumRecords):=loop(dataset([{0,0}],piRec),Log2NumRecords,rows(left)+rows(left)); //initial empty set

piRec piEulerSeries(piRec PreviousSeries,Cnt):=transform
	self.partialSum:=PreviousSeries.partialSum+1/(Cnt*Cnt);
	self.pi:=sqrt(self.partialSum*6);
end;

series:=ITERATE(piEmpty(20),piEulerSeries(left,counter));
sample(series,100000); //view every 100,000th term in the series
  • No labels