split_around_by_predicate: Split Around First Predicate Match

View source: R/20-api-generics.R

split_around_by_predicateR Documentation

Split Around First Predicate Match

Description

Splits at the first point where a predicate function becomes TRUE while scanning the sequence.

Usage

split_around_by_predicate(t, predicate, monoid_name, accumulator = NULL)

Arguments

t

A flexseq (or subclass).

predicate

Function applied to accumulated monoid values.

monoid_name

Name of the monoid used for scanning.

accumulator

Optional starting accumulator value.

Details

This function generally requires the sequence be annotated with a measure_monoid(); see the examples and measure_monoid() for more information.

value is the matched leaf entry for the input structure:

  • flexseq: stored user element.

  • ordered_sequence: list(value, key).

  • priority_queue: list(value, priority).

  • interval_index: list(value, start, end).

left and right preserve subclass when the input is a subclass of flexseq.

Value

A list with fields:

  • left: elements before the split point.

  • value: the matched element at the split point.

  • right: elements after the split point.

Examples

x <- flexseq("a", "b", "c", "d")

# Each element e has measure 1; the accumulated measure for
# a set of elements is computed by the associative function `+`
# along with the identity value 0.
size_monoid <- measure_monoid(`+`, 0L, function(e) 1L)
x2 <- add_monoids(x, list(size = size_monoid))

# the first time the measure stored in the size monoid
# accumulates to greater than or equal to 3 is the 3rd
# element in sequence.
split_around_by_predicate(x2, function(v) v >= 3L, "size")

# Split at the first element
split_around_by_predicate(x2, function(v) v >= 1L, "size")

# Split at the last element
split_around_by_predicate(x2, function(v) v >= 4L, "size")

Immutables documentation built on April 29, 2026, 1:06 a.m.