There are several methods that are used to manipulate sequences in Euphoria. When it comes to adding elements to a sequence, the add_item()
method comes in handy because of how it doesn’t just add to the sequence, but can also sort and reorder the sequence in the preferred manner.
add_item()
method?The add_item()
method is a built-in method in Euphoria. It is part of the standard library sequence methods. The add_item() method introduces a new member/item to a sequence either in
add_item(to_be_added,add_to,order)
to_be_added
: This is the element/item that
will be added to the sequence,add_to
, as indicated in the function.add_to
: This is the sequence to which a new item/member will be added. The new item is added either at the start or the end of the sequence.order
: This indicates in what way the to_be_added
parameter affects the add_to
parameter.The order parameter has the following legal options:
to_be_added
element is prepended to the add_to
sequence. This is the default option.ADD_APPEND
: This is when the to_be_added
element is appended to the tail of the add_to
sequence.add_to
sequence an in ascending order, after inserting the to_be_added
element.add_to
sequence in a descending order, after inserting the to_be_added
element.Note: To use this method, we must include the
sequence.e
file in our program, because this file is part of the standard library in Euphoria.
include std/sequence.e --define variables sequence to_be_added = {1,7,4}, add_to = {5,6,3,8,2} sequence added1, added2, added3, added4 --call the function added1 = add_item(to_be_added, add_to, ADD_PREPEND) added2 = add_item(to_be_added, add_to, ADD_APPEND) added3 = add_item(to_be_added, add_to, ADD_SORT_UP) added4 = add_item(to_be_added, add_to, ADD_SORT_DOWN) --print outcome to display puts(1,"Prepend: ") print(1,added1) puts(1,"\n Append: ") print(1,added2) puts(1,"\n Ascending order: ") print(1,added3) puts(1,"\n Descending order: ") print(1,added4)
We can clearly see how the different order
parameter affected the output of the function and what to expect when any of them is used.
Let’s look at a-line-by-line breakdown of the code written above:
In line 1, we include the necessary file.
In lines 4–5, we define the required variables.
In lines 8–11, we save the output, using the add_item()
method.
In lines 14–21, we print the output of the whole function to the console.
RELATED TAGS
CONTRIBUTOR
View all Courses