What is the merge() method in Euphoria?
Overview
In Euphoria, merge() is a built-in function used to join members of two sequences together to form a single one. This method adds up the members of two sequences and returns them as a single sequence. This sequence will have the members of the first sequence placed before that of the second sequence, in ascending order.
Syntax
merge(sequenceA, sequenceB,userFunct, userData)
Parameters
-
sequeceA: This is the first sequence that will be in the merged sequence. -
sequenceB: This is the second sequence. -
userFunct: This is an optional parameter. It represents a user-defined custom comparison function. This parameter will define how the comparison between the members of the two sequences will be done. -
userD: This is an optional parameter which is not required. This is because it is an empty string sequence by default.
Breakdown of the default logic behind this function
Two sequence variables, A and B, are to be merged using the merge() method.
The Euphoria parser, while adding the sequence to the new sequence starting with A, will compare the members of the two sequences, and stop adding A to the new sequence when it meets a member of A that is larger in value than the first member of B. It starts to insert the members of B into the new sequence. It will stop adding members of B to continue with A from where it stopped at, if the parser gets to a member of sequence B that is greater in value than the value of A at that position of A where it initially stopped. It continues until all elements are merged, with some kind of ascending order sorting done.
Yes, this explanation might sound a little bit confusing now, but with a critical study of the code below and its output, we’ll see how helpful the above explanation really is.
Example
--include library fileinclude std/sort.e--declare variablessequence mergeResultsequence mergerA = {1,2,5,7,4}, mergeB = {1,3,7,9}--call the merge functionmergeResult = merge(mergerA, mergeB)print(1,mergeResult)
Explanation
- Line 2: We include the standard library file
sort.eto allow for the use of the function. - Lines 5–6: We declare some sequences and assign values.
- Line 9: We call the
merge()function on previous declared sequences. - Line 10: We print the output to the console.