What is the pivot() method in Euphoria?

Overview

As the name implies, the pivot() method targets a value and carries out computations and operations around this particular value, giving it pivotal attention. This is a simple breakdown of what this method means.

More technically, the pivot() method in Euphoria is built into the sequence.e module of the standard library. It takes in a pivot_value and splits another value from the source when the pivot_value is found in the source, producing three sets of subsequence.

The three subsequence sets are:

  • Values in the source less than the pivot_value.
  • Values that are equal to the pivot_value in the source.
  • Values that are greater than the pivot value.

Syntax

pivot(source, pivot_value)

Parameters

  • source: The source is an object-type variable, which will be split around a pivot.

  • pivot_value: This can be an integer or a sequence. It will serve as the point in the source at which the split will occur.

Return value

It will return a sequence with three subsequences, as explained below.

  • In the first subsequence, values in the source are less than the pivot_value.

  • The second subsequence contains values equal to the pivot_value in the source.

  • The third subsequence contains values greater than the pivot_value in the source.

Example

--include the neccessary module
include std/sequence.e
--declare a variable
sequence output
--save the outcome of using pivot method
output = pivot( {12, 4, 6.7, 9, 9, -3.8, 9, 14, 3.341, -8}, 9 )
--print the outcome to screen
print(1,output)

Explanation

  • Line 3: We include the sequence.e module.
  • Line 5: We declare the sequence output.
  • Line 7: We save the outcome of using the pivot() method in the output variable.
  • Line 9: We print the result to screen.

Free Resources