% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/30-api-flexseq-dollar.R,
%   R/30-api-flexseq-indexing.R
\name{$.flexseq}
\alias{$.flexseq}
\alias{$<-.flexseq}
\alias{[.flexseq}
\alias{[[.flexseq}
\alias{[<-.flexseq}
\alias{[[<-.flexseq}
\title{Flexseq Indexing}
\usage{
\method{$}{flexseq}(x, name)

\method{$}{flexseq}(x, name) <- value

\method{[}{flexseq}(x, i, ...)

\method{[[}{flexseq}(x, i, ...)

\method{[}{flexseq}(x, i) <- value

\method{[[}{flexseq}(x, i) <- value
}
\arguments{
\item{x}{A \code{flexseq}.}

\item{name}{Element name (for \code{$} and \verb{$<-}).}

\item{value}{Replacement values; recycled to selected index length.}

\item{i}{Positive integer indices, character element names, or logical mask.
For \code{[[}, a single integer or character name.}

\item{...}{Unused.}
}
\value{
For \code{$}: the matched element.

For \verb{$<-}: updated tree with one named element replaced.

For \code{[}: a new \code{flexseq} containing selected elements in query order.
For character indexing, missing names are represented as \code{NULL} elements.

For \code{[[}: the extracted element (internal name metadata is removed).

For \verb{[<-}: a new \code{flexseq} with selected elements replaced.

For \verb{[[<-}: a new \code{flexseq} with one element replaced.
}
\description{
Index, replace, and extract elements of a \code{flexseq} by position or name.
}
\details{
Selector behavior:
\itemize{
\item Integer indexing (\code{[}) returns elements in the requested order.
\item Character indexing (\code{[}) returns elements in requested name order; unknown
names become \code{NULL} elements in the output.
\item Logical indexing (\code{[}) is positional and follows logical-mask selection.
}

Replacement behavior:
\itemize{
\item \verb{[<-} is persistent and recycles \code{value} to the number of selected
positions (with standard R recycling warnings where applicable).
\item \verb{[[<-} replaces one element; assigning \code{NULL} removes that element.
}
}
\examples{

# $ extracts by name
x <- as_flexseq(setNames(as.list(1:3), c("a", "b", "c")))
x$b

# $<- replaces by name
x <- as_flexseq(setNames(as.list(1:3), c("a", "b", "c")))
x$b <- 20
x$b
x <- as_flexseq(letters[1:6])
x

x2 <- x[c(2, 4, 6)]
x2

# named lookups return NULL for missing names
x3 <- as_flexseq(setNames(as.list(letters[1:4]), c("w", "x", "y", "z")))
x4 <- x3[c("y", "missing", "w")]
x4

# logical indexing supports recycling
x[c(TRUE, FALSE)]

# [[ extracts one element
x <- as_flexseq(letters[1:5])
x[[3]]

x2 <- as_flexseq(setNames(as.list(letters[1:3]), c("a1", "a2", "a3")))
x2[["a2"]]

# [<- replaces selected elements
x <- as_flexseq(1:6)
x

x2 <- x
x2[c(2, 5)] <- list(20, 50)
x2

# character replacement uses element names
x3 <- as_flexseq(setNames(as.list(1:4), c("a", "b", "c", "d")))
x3[c("d", "a")] <- list(40, 10)
x3

# logical replacement supports recycling
x4 <- x
x4[c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)] <- list(1)
x4

# [[<- replaces one element
x <- as_flexseq(letters[1:4])
x2 <- x
x2[[2]] <- "ZZ"
x2

x3 <- as_flexseq(setNames(as.list(1:3), c("x", "y", "z")))
x3[["y"]] <- 99
x3

# assigning NULL removes one element
x4 <- as_flexseq(letters[1:4])
x4[[2]] <- NULL
x4
}
