File: values.R

package info (click to toggle)
r-cran-future 1.11.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,380 kB
  • sloc: sh: 14; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download
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
70
71
72
#' Gets all values in an object
#'
#' Gets all values in an environment, a list, or a list environment
#' and returns an object of the same class (and dimensions).
#' All future elements are replaced by their corresponding
#' \code{value()} values.  For all other elements, the existing
#' object is kept.
#'
#' @param x An environment, a list, or a list environment.
#' @param \dots Additional arguments passed to \code{value()} of each future.
#'
#' @return An object of same type as \code{x} and with the same names
#' and/or dimensions, if set.
#'
#' @export
values <- function(x, ...) UseMethod("values")

#' @export
values.list <- function(x, ...) {
  y <- futures(x)
  y <- resolve(y)

  for (ii in seq_along(y)) {
    tmp <- y[[ii]]
    if (inherits(tmp, "Future")) {
      v <- value(tmp, ...)
      if (is.null(v)) {
        y[ii] <- list(NULL)
      } else {
        y[[ii]] <- v
        v <- NULL
      }
    }
  }
  y
}

#' @export
values.environment <- function(x, ...) {
  y <- futures(x)
  y <- resolve(y)
  names <- ls(envir = y, all.names = TRUE)
  for (key in names) {
    tmp <- y[[key]]
    if (inherits(tmp, "Future")) y[[key]] <- value(tmp, ...)
  }
  y
}

#' @export
values.listenv <- function(x, ...) {
  y <- futures(x)
  y <- resolve(y)
  for (ii in seq_along(y)) {
    tmp <- y[[ii]]
    if (inherits(tmp, "Future")) {
      v <- value(tmp, ...)
      if (is.null(v)) {
        y[ii] <- list(NULL)
      } else {
        y[[ii]] <- v
        v <- NULL
      }
    }
  }
  y
}

#' @export
values.Future <- function(x, ...) {
  value(x, ...)
}