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
sourcevalue to be broken up. - We also provide the
sizeof the breakup chunks. - Lastly, we provide or indicate the breakup style we wish to use. It can either be:
Bk_LENBk_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
sizeparameter is an integer, it’ll represent either the maximum length of each sub-sequence that result from thebreakupor the maximum number of sub-sequences to break the parametersourceinto. -
If the
sizevalue is a sequence, it represents a list of the number of elements for each sub-sequence to be created. With a sequencesizeparameter, 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 provideBK_LENif we wish to have thesizeinteger parameter as the maximum length of the breakup chunks. -
BK_PIECES: We can also provideBK_PIECESif thesizeparameter 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
sizeis an integer value andstyleisBK_PIECES, the length of each breakup piece will be equal tosize, but ifsourcecannot be evenly divided to achieve this, then the piece from the left-hand size will have more value.
When
sizeis a sequence,styleis 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.esequence output1, output2, output3, sourcesource = {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.emodule. - Line 3: Declare the required variables.
- Line 5: We assign value to the
sourcevariable. - Line 8–10: We use the
breakup()method in different possible ways. - Line 12–16: Display the output to the screen.