-
Notifications
You must be signed in to change notification settings - Fork 3
/
xPartition.R
executable file
·69 lines (57 loc) · 1.4 KB
/
xPartition.R
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
58
59
60
61
62
63
64
65
66
67
68
69
#' xPartition
#'
#' Divide elements in a collection into two collections
#' based on a predicate function.
#'
#' @section Type Signature:
#' (any -> <logical>) -> |any| -> [[any]]
#'
#' @param
#' pred a predicate. The predicate function with which to partition
#' the input collection.
#'
#' @param
#' coll a collection. The collection to partition into
#' two collections.
#'
#' @param
#' ... see above.
#'
#' @return
#' Returns two lists; a list of elements in a collection for which a
#' predicate returns true, and a list of elements in a collection for which
#' a predicate returns false or na.
#'
#' @section Corner Cases:
#' If \bold{coll} is empty the empty list is returned. If all the
#' elements return only true/only false, then one of two sublists will be the
#' empty list.
#'
#' @family filtering_functions
#'
#' @template
#' Variadic
#'
#' @example
#' inst/examples/example-xPartition.R
#'
#' @rdname xPartition
#' @export
xPartition <- MakeFun('xPartition', function (pred, coll) {
if (length(coll) == 0) {
list()
} else {
ind <- vapply(coll, function (elem) {
is_match <- pred(elem)
MACRO(Must_Be_Flag(is_match, pred))
isTRUE(is_match)
}, logical(1))
true_ind <- !elem_is_na(ind) & ind
list(
as.list(coll[true_ind]),
as.list(coll[!true_ind]) )
}
})
#' @rdname xPartition
#' @export
xPartition_ <- MakeVariadic(xPartition, 'coll')