# 5.1 Simple iterative operations

1. Use the iterative function to implement summation (sum), cumulative summation (cum), maximum value (max), and minimum value member (maxp) calculations.

2. Calculate the cumulative sales of salesperson no. 10.

3. Number the orders of salesperson no. 10 from 1 and replace ORDERID with corresponding number.

## SPL

A | B | |
---|---|---|

1 | [1,10,9,7,1,10,4,9,6,7] | |

2 | =A1.iterate(~~+~,0) | /Summation |

3 | =A1.iterate@a(~~+~,0) | /Cumulative summation |

4 | =A1.iterate(if(~~<~,~,~~),-inf()) | /Maximum value |

5 | =A1.new(#:idx,~:value) | |

6 | =A5.iterate(if (~~==null || ~.value<~~.value,~,~~), null ) | /Minimum value member |

7 | =A5.iterate(if(~~==null || ~.value<~~.value,~,if(~.value==~~.value,~~|~,~~)), null ) | /Set of minimum value members |

8 | =file(“SALES.csv”).import@tc() | |

9 | =A8.select(SELLERID==10) | |

10 | =A9.derive(iterate(~~+AMOUNT,0):CUM_AMOUNT) | /Cumulative sales |

11 | =A9.derive(cum(AMOUNT):CUM_AMOUNT) | /Cumulative sales |

12 | =A10.run(ORDERID=iterate(~~+1,0)) | /Number the orders of salesperson |

13 | =A10.run(ORDERID=seq(SELLERID)) | /Number the orders of salesperson |

The iterative function A.iterate(x,a) is to calculate x for each member of A. In x, the symbols ‘~’ and ‘#’ can be used to respectively represent the current member and sequence number of A in a loop. It should be specially noted that the iterative function also provides the symbol ‘~~’ to represent the x calculated in the previous round of loop. When the loop starts, the initial value of ~~ is the parameter a. After looping through all members, the final result is returned.

After adding the @a option, a sequence composed of ~~ calculated from each round of loop will be returned once all members are looped through, and its length is the same as A.

A10 is to iteratively generate the cumulative sales of salesperson no. 10, which is the same as the result of A11.

A12 is to iteratively execute, and has the same effect as A13.

## SQL

SQL doesn’t provide a similar method.

## Python

Python doesn’t provide a similar method.

5.2 Early terminated iterative operation

Example codes for comparing SPL, SQL, and Python

*SPL Official Website**👉* *https://www.scudata.com*

*SPL Feedback and Help**👉* *https://www.reddit.com/r/esProc_SPL*

*SPL Learning Material**👉* *https://c.scudata.com*

*SPL Source Code and Package**👉* *https://github.com/SPLWare/esProc*

*Discord**👉* *https://discord.gg/cFTcUNs7*

*Youtube**👉* *https://www.youtube.com/@esProc_SPL*