What is the repeat_pattern() method in Euphoria?

Overview

We use the repeat_pattern() method to create a sequence with a pattern repeated the number of times as indicated.

With the repeat_pattern() method, we must provide the sample pattern we want to repeat as a sequence. We’ll have to give the number of times we would like the pattern to be made. The repeat_pattern() method will make the pattern as we intend.

Syntax

repeat_pattern(pattern,count)

Parameters

  • pattern: This parameter is an object value that serves as the sample of the pattern we’d want to repeat as much as possible.

  • count: This integer value determines how many times the pattern will be created.

For instance, if we wish to create a repeated pattern out of a variable A = {1,2,3} which we want to repeat five times. Using the repeat_pattern() method, we’ll get an outcome such as: {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}.

Return value

This function will return a sequence containing a repeated pattern. The length of the returned sequence is obtained by multiplying the count by the length of the pattern.

length = count*length(pattern)

If the method fails due to the wrong pattern parameter, it will return an empty sequence.

Code

Let’s see the code below on how to use the repeat_pattern() method.

include std/sequence.e
sequence pattern = {1,2,3}
sequence pat = "This day"
sequence output1, output2
output1 = repeat_pattern(pat,4)
output2 = repeat_pattern(pattern,4)
printf(1,"\n%s\n",{output1})
print(1,output2)

Explanation

  • Line 1: Add the sequence.e module.
  • Line 2–4: We declare some variables.
  • Line 6–7: We use the repeat_pattern() method to create a pattern.
  • Line 9–10: We display the patterns four times.

Free Resources