What is the remove_subseq method in Euphoria?

Overview

The remove_subseq method is a sequence manipulation method that is used to remove all subsequences found in a sequence.

This method works by checking a sequence target for any subsequnce subseq and removing all occurrences of subsequence found. This method also provides an option to replace the found subseq with other values alt_value.

Syntax

remove_subseq(target, alt_value)

Parameter

  • target: This is the sequence from which we’re interested in removing subsequences.

  • alt_value: This is an object value that will be used to replace any subsequence encountered in target. It is optional and has a default value of SET_NOALT. This means that no alternative value will be used to replace the subsequence if it is found.

Return value

The value returned is a sequence with all subsequences removed. The returned value contains just the atoms from target and the optional alt_value that replaces the removed subsequence.

Code

In the code snippet below, we check a few sequence values for subsequences, using the remove_subseq() method to remove the subsequences found.

include std/sequence.e
--declare variables
sequence target1, target2, output1, output2
--assign values to variables
target1 = {1,2,3,{4,5},{6,7,8},{9,{10,11},12}}
target2 = {3,5,6,{7,5},8,9}
output1 = remove_subseq(target1) --no alt_value provided
output2 = remove_subseq(target2,39) --alt_value of 3 provided
--print to screen the output of using remove_subseq
print(1,output1)
puts(1,"\n")
print(1,output2)

It is important to point out that alt_value is an object. An object is either an atom or a sequence. The atoms, in the current definition of the language, are in fact numbers, and a sequence is a dynamic collection of atoms and other sequences, i.e. of any kind of object at all.

Explanation

  • Line 1: We include the sequence.e module.

  • Line 4: We declare variables.

  • Lines 7–8: We assign values to the variables.

  • Lines 10–11: We use the remove_subseq() method and store its returned value in the variables output1 and output2.

  • Lines 14 and 16: We print the output to the screen.

Free Resources