What is the series() method in Euphoria?
Overview
Building a series is a common arithmetic operation where a sequentially ordered set of numbers is produced following a laid-down pattern. We can build a simple geometric or linear series in Euphoria using the series() method.
The series() method is used to generate a geometric or a linear series. It does so by using a start_value that is the initial member of the series. Then, it increments it with the increment until a certain amount of series items are generated. This amount is determined by count. The operator parameter indicates the type of series (linear or geometric).
Syntax
series(start,increment,count,operator)
Parameters
start: This is an object and is the first value of the series to be returned. It is a required parameter.increment: This is also an object and a required parameter. It is the value by which the series will be incremented either linearly or geometrically. A linear increment is done by adding the value to the last element of the series to get the next one. A geometric increment is done by multiplying the value with the last element of the series at any point in time to get the next.count: This is an integer value and a required parameter. It indicates the number of items generated by the series.operator: This is the arithmetic operator which acts as the series builder. It is an integer value. This parameter is optional and has a default value of+.
Return value
This function will return a new sequence series built from the object supplied.
Note: When a negative value is supplied as a
countor the increment value provided is not a valid object,0is returned. However, with a validcount, a sequence is returned whose length is equal to thecount. This is the length of the returnedcountsequence.
Example
--include some modules neededinclude std/sequence.e--define some variablesobject start = 2object increment = 3integer count = 10integer operator = 42--save the output of the series method in some variablessequence output1 = series(start,increment,count)sequence output2 = series(start,increment,count,operator)--display the outcome of using the series.print(1,output1)puts(1,"\n")print(1,output2)
Explanation
The second series is a geometric series formed using the multiplication operator indicated by its integer value 42. This integer value is obtained by printing print(1,"*"). It gives an integer representation of any operator in Euphoria.
- Line 2: We include the
sequence.emodule from the standard library. - Lines 5–8: We define the necessary variable.
- Lines 11 and 12: We use the
series()method to create a series. - Lines 15–17: We display the output.