What is the breakup() method in Euphoria?

Overview

The breakup() method breaks up a sequence into chunks of specified size following a predefined way of breaking up.

With the breakup method, we can do the following:

  • Provide a source value to be broken up.
  • We also provide the size of the breakup chunks.
  • Lastly, we provide or indicate the breakup style we wish to use. It can either be:
    • Bk_LEN
    • Bk_PIECES

We shall discuss these different styles later in this shot. But the style will determine how the outcome of the break up will be.

Syntax

breakup(source,size,style)

Parameters

  • source: The source parameter is a sequence value that the method will break up during execution. It is required.

  • size: This parameter is an object value, which is the size of the breakup chunks. Size can be any of the following:

    • If the size parameter is an integer, it’ll represent either the maximum length of each sub-sequence that result from the breakup or the maximum number of sub-sequences to break the parameter source into.

    • If the size value is a sequence, it represents a list of the number of elements for each sub-sequence to be created. With a sequence size parameter, we can specify different sizes for the breakup chunks.

  • style: This parameter is an integer representing the style in which the breakup is to be performed. The style is as follows:

    • BK_LEN: This is the default. We have to provide BK_LEN if we wish to have the size integer parameter as the maximum length of the breakup chunks.

    • BK_PIECES : We can also provide BK_PIECES if the size parameter represents the maximum number of sub-sequences (breakup pieces) to break the source into.

Return value

The breakup method returns a sequence containing the breakup pieces.

Note that if size is an integer value and style is BK_PIECES, the length of each breakup piece will be equal to size, but if source cannot be evenly divided to achieve this, then the piece from the left-hand size will have more value.

When size is a sequence, style is ignored, and the breakup piece will have size as specified in the sequence.

Code

In the code below is a snippet that shows how this breakup() works in an actual code.

include std/sequence.e
sequence output1, output2, output3, source
source = {1,2,3,4,5,5,6,7,7,8,1,3,4,5,6}
output1 = breakup(source,3,BK_LEN)
output2 = breakup(source,5,BK_PIECES)
output3 = breakup(source,{2,3,4,5},BK_LEN)
print(1,output1)
puts(1,"\n")
print(1,output2)
puts(1,"\n")
print(1,output3)

Explanation

  • Line 1: Add the sequence.e module.
  • Line 3: Declare the required variables.
  • Line 5: We assign value to the source variable.
  • Line 8–10: We use the breakup() method in different possible ways.
  • Line 12–16: Display the output to the screen.

Free Resources