split_by_predicate: Split into Left and Right Parts

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

split_by_predicateR Documentation

Split into Left and Right Parts

Description

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

Usage

split_by_predicate(x, predicate, monoid_name)

Arguments

x

A flexseq (or subclass).

predicate

Function applied to accumulated monoid values.

monoid_name

Name of the monoid used for scanning.

Details

This is the two-way variant of split_around_by_predicate().

As with split_around_by_predicate(), a common setup is a custom monoid created with measure_monoid() and attached via add_monoids().

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

Value

A list with fields:

  • left: elements before the split point.

  • right: elements from the split point onward.

Examples

x <- flexseq("a", "b", "c", "d")
size_monoid <- measure_monoid(`+`, 0L, function(e) 1L)
x2 <- add_monoids(x, list(size = size_monoid))
split_by_predicate(x2, function(v) v >= 3L, "size")

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

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