Functional Programming Patterns
Predicate Filter
Keep Matching Values
A predicate is a function that returns TRUE or FALSE for each input value.
Program
Play the script to change the minimum value used by the predicate.
predicate_filter.R
Replay: real traced execution (multi-file project)
min_value <- 4
values <- c(1, 3, 5, 7)
keep <- function(x) x >= min_value
selected <- values[vapply(values, keep, logical(1))]
count <- length(selected)
label <- paste("count", count, sep = ":")
cat(label, "\n", sep = "")
min_value <- 2
values <- c(1, 3, 5, 7)
keep <- function(x) x >= min_value
selected <- values[vapply(values, keep, logical(1))]
count <- length(selected)
label <- paste("count", count, sep = ":")
cat(label, "\n", sep = "")
min_value <- 6
values <- c(1, 3, 5, 7)
keep <- function(x) x >= min_value
selected <- values[vapply(values, keep, logical(1))]
count <- length(selected)
label <- paste("count", count, sep = ":")
cat(label, "\n", sep = "")
min_value ← 4
1min_value <- 42values <- c(1, 3, 5, 7)values this step4min_valuevalues ← 1, 3, 5, 7
1min_value <- 42values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_valuevalues this step1, 3, 5, 7valueskeep ← x >= 4
2values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]values this stepx >= 4keep4min_valueselected ← 5, 7
3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)values this step5, 7selected1, 3, 5, 7valuesx >= 4keepcount ← 2
4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)6label <- paste("count", count, sep = ":")values this step2count5, 7selectedlabel ← count:2
5count <- length(selected)6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")values this stepcount:2label2countcat(label, " ", sep = "")
6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")outputcount:2values this stepcount:2label
min_value ← 2
1min_value <- 22values <- c(1, 3, 5, 7)values this step2min_valuevalues ← 1, 3, 5, 7
1min_value <- 22values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_valuevalues this step1, 3, 5, 7valueskeep ← x >= 2
2values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]values this stepx >= 2keep2min_valueselected ← 3, 5, 7
3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)values this step3, 5, 7selected1, 3, 5, 7valuesx >= 2keepcount ← 3
4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)6label <- paste("count", count, sep = ":")values this step3count3, 5, 7selectedlabel ← count:3
5count <- length(selected)6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")values this stepcount:3label3countcat(label, " ", sep = "")
6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")outputcount:3values this stepcount:3label
min_value ← 6
1min_value <- 62values <- c(1, 3, 5, 7)values this step6min_valuevalues ← 1, 3, 5, 7
1min_value <- 62values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_valuevalues this step1, 3, 5, 7valueskeep ← x >= 6
2values <- c(1, 3, 5, 7)3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]values this stepx >= 6keep6min_valueselected ← 7
3keep <- function(x) x >= min_value4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)values this step7selected1, 3, 5, 7valuesx >= 6keepcount ← 1
4selected <- values[vapply(values, keep, logical(1))]5count <- length(selected)6label <- paste("count", count, sep = ":")values this step1count7selectedlabel ← count:1
5count <- length(selected)6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")values this stepcount:1label1countcat(label, " ", sep = "")
6label <- paste("count", count, sep = ":")7cat(label, "\n", sep = "")outputcount:1values this stepcount:1label
predicate
The `keep` function returns one logical value for each input.
logical map
`vapply(values, keep, logical(1))` evaluates the predicate across the vector.
filter
Indexing with the logical result keeps only matching values.