What kind of operation this function performs?

Hi all,

Someone of you could, please explain me, which kind of alteration, or calculus this function performs?

object SequenceCounter {

  val ascending = SequenceCounter((left: Char, right: Char) => right == left + 1)

  val constant = SequenceCounter((left: Char, right: Char) => right == left)

  val descending = SequenceCounter((left: Char, right: Char) => right == left - 1)
}

case class SequenceCounter(
                            isSequence: (Char, Char) => Boolean,
                            maximum: Int = 1,
                            partial: Int = 1,
                            last: Char = 0
                          ) {
  def count(input: String): Int =
    if (input == null || input == "NULL" || input.isEmpty)
      0
    else {
      def map(accumulator: SequenceCounter, value: Char): SequenceCounter =
        if (!isSequence(accumulator.last, value))
          accumulator.copy(
            partial = 1,
            last = value
          )
        else
          accumulator.copy(
            maximum = Math.max(accumulator.maximum, accumulator.partial + 1),
            partial = accumulator.partial + 1,
            last = value
          )

      def reduce(left: SequenceCounter, right: SequenceCounter): SequenceCounter =
        copy(maximum = Math.max(left.maximum, right.maximum))

      input
        .toCharArray
        .aggregate(this)(map, reduce)
        .maximum
    }
}

Thank you !

This looks like it tries to find consecutive characters in a string, that fulfill some pairwise condition, and return the length of the longest of these substrings.

So for each character in the string, it compares it with the previous character using the isSequence function. If that returns true, it increases the partial variable, which counts for how many consecutive chars isSequence was true. When isSequence returns false, the counter is stored in maximum if it was larger than the previous value and then reset.

The aggregate function works like a foldLeft, in this case it is identical, because reduce is not called for non-parallel collections.

With the given SequenceCounter instances in the companion object, it returns the length of the longest sequence of consecutive ascending / descending (according to their numeric value) or identical characters. So e.g. SequenceCounter.ascending.count("abdefgklmNOP") will return 4, as defg is the longest substring consisting of consecutive chars.

Thank you so much !