-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathwhen.Rd
More file actions
57 lines (52 loc) · 1.72 KB
/
when.Rd
File metadata and controls
57 lines (52 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/deprec-when.R
\name{when}
\alias{when}
\title{Match/validate a set of conditions for an object and continue with the action
associated with the first valid match.}
\usage{
when(., ...)
}
\arguments{
\item{.}{the value to match against}
\item{...}{formulas; each containing a condition as LHS and an action as RHS.
named arguments will define additional values.}
}
\value{
The value resulting from the action of the first valid
match/condition is returned. If no matches are found, and no default is
given, NULL will be returned.
Validity of the conditions are tested with \code{isTRUE}, or equivalently
with \code{identical(condition, TRUE)}.
In other words conditions resulting in more than one logical will never
be valid. Note that the input value is always treated as a single object,
as opposed to the \code{ifelse} function.
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}
This function was deprecated in purrr 1.0.0 because it's not related to the
core purpose of purrr. You can pull your code out of a pipe and use regular
\code{if}/\verb{else} statements instead.
\code{when()} is a flavour of pattern matching (or an if-else abstraction) in
which a value is matched against a sequence of condition-action sets. When a
valid match/condition is found the action is executed and the result of the
action is returned.
}
\examples{
1:10 |>
when(
sum(.) <= 50 ~ sum(.),
sum(.) <= 100 ~ sum(.)/2,
~ 0
)
# now
x <- 1:10
if (sum(x) < 10) {
sum(x)
} else if (sum(x) < 100) {
sum(x) / 2
} else {
0
}
}
\keyword{internal}