-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionUtils.R
41 lines (35 loc) · 1.08 KB
/
CollectionUtils.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
constructCollectionUtils <- function() {
public <- list()
public$lapply <- function(x, func, reporter = "generic lapply", verbose = TRUE) {
count <- 0
x %>% lapply(function(currEntry) {
count <<- count + 1
if (verbose) {
print(paste0(reporter, ": processing item ", count, " of ", length(x)))
}
func(currEntry)
})
}
public$lapplyWithName <- function(x, func, reporter = "generic lapplyWithName", verbose = TRUE) {
# func should take in 2 arguments - function(currEntryName, currItem)
entries <- x %>% names()
names(entries) <- entries
entries %>% public$lapply(function(currEntryName) {
func(currEntryName, x[[currEntryName]])
}, reporter, verbose)
}
public$sapply <- function(x, func, reporter = "generic sapply") {
count <- 0
x %>% sapply(function(currEntry) {
count <<- count + 1
print(paste0(reporter, ": processing item ", count, " of ", length(x)))
func(currEntry)
})
}
public$foreach <- function(x, func) {
for (i in x) {
func(i)
}
}
return(public)
}