-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayer-list.R
73 lines (57 loc) · 1.63 KB
/
layer-list.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
70
71
72
73
new_layer_list = function(list) {
new("layer_list", .Data = list)
}
#' @rdname layer_list
#' @export
layer_list = function(...) {
new_layer_list(list(...))
}
# type conversion ---------------------------------------------------------
#' @rdname layer_list
#' @export
as_layer_list = function(x) {
UseMethod("as_layer_list")
}
#' @rdname layer_list
#' @export
as_layer_list.layer_list = function(x) {
x
}
#' @rdname layer_list
#' @export
as_layer_list.list = function(x) {
if (!is_layer_like(x)) {
stop0("All objects in a layer_list must be layer-like objects")
}
new_layer_list(as.list(x))
}
#' @rdname layer_list
#' @export
as_layer_list.LayerInstance = function(x) {
new_layer_list(list(x))
}
# layer concatenation -------------------------------------------------
#' @rdname layer_list
#' @export
setMethod("+", signature(e1 = "layer_list", e2 = "layer_list"), function(e1, e2) {
new_layer_list(c(e1, e2))
})
# layer list flattening ---------------------------------------------------
#' Flatten a list of layers so that nested lists of layers (a la list(..., ..., list(...)))
#' are flattened into a single level. Unlike unlist(), does not remove type information
#' of list elements / try to coerce everything to the same vector type
#' @noRd
flatten_layer_list = function(layers) {
if (is.list(layers)) {
do.call(c, lapply(layers, flatten_layer_list))
} else {
list(layers)
}
}
# printing ----------------------------------------------------------------
#' @rdname layer_list
#' @export
setMethod("show", signature(object = "layer_list"), function(object) {
cat("<layer_list>:\n")
print([email protected])
})