/
pop.R
129 lines (118 loc) · 2.96 KB
/
pop.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#' Get and Remove Element
#'
#' Search and return an element and remove it afterwards from the object.
#' If the element is not found, signal an error.
#' @name pop
#' @details
#' All functions work by reference, that is, the original object is altered.
#' `ref_pop(.x)` tries to access specific values.
#'
#' `ref_popleft(.x)` pops first element of a `Deque`.
#'
#' @param .x any `R` object.
#' @param index `character` name or `numeric` position of value to be popped
#' @param ... additional arguments to be passed to or from methods.
#' @seealso [peek()]
#' @export
ref_pop <- function(.x, ...) UseMethod("ref_pop")
#' @rdname pop
#' @export
ref_popleft <- function(.x, ...) UseMethod("ref_popleft")
#' @rdname pop
#' @return For `Deque` the first (`ref_popleft`) or last (`ref_pop`) element of
#' the deque after it was removed.
#' @export
#' @examples
#' # Deque
#' d = deque(1, 2, 3)
#' ref_pop(d)
#' ref_popleft(d)
#'
#' try({
#' ref_pop(deque()) # pop at empty Deque
#' })
ref_pop.Deque <- function(.x, ...) .x$pop()
#' @rdname pop
#' @export
ref_popleft.Deque <- function(.x, ...) .x$popleft()
#' @name DequeS3
#' @rdname DequeS3
#' @details
#' * `ref_pop(.x)` pop last element. If `.x` is empty, an error is given.
#' * `ref_popleft(.x)` pop first element. If `.x` is empty, an error is given.
#' @examples
#' d = deque(1, 2, 3)
#' ref_pop(d)
#' print(d)
#' ref_popleft(d)
#' print(d)
#'
#' try({
#' ref_pop(deque()) # pop at empty Deque
#' })
NULL
#' @rdname pop
#' @return For `Container` the value at the given index after it was removed from
#' the `Container` object. If `index` is not found, an error is raised.
#' @export
#' @examples
#'
#' # Container
#' co = container(a = 1, b = 1:3, d = "foo")
#' ref_pop(co, "b")
#' ref_pop(co, 1)
#'
#' try({
#' ref_pop(co, "x") # index 'x' not found
#' })
ref_pop.Container <- function(.x, index, ...) .x$pop(index)
#' @name ContainerS3
#' @rdname ContainerS3
#' @details
#' * `ref_pop(.x, index)` return element at given index and remove it
#' from the `container` object.
#' @examples
#'
#' co = container(a = 1, b = 1:3, d = "foo")
#' ref_pop(co, "b")
#' ref_pop(co, 1)
#'
#' try({
#' ref_pop(co, "x") # index 'x' not found
#' })
NULL
#' @rdname pop
#' @return For `dict.table`, returns the column at the given `index` after it was
#' removed from the `dict.table`. If column does not exist, an error is raised.
#' @export
#' @examples
#'
#' # dict.table
#' dit = dict.table(a = 1:3, b = 4:6)
#' ref_pop(dit, "a")
#' ref_pop(dit, 1)
#'
#' try({
#' ref_pop(dit, "x") # index 'x' not found
#' })
ref_pop.dict.table <- function(.x, index, ...)
{
elem <- at2(.x, index)
ref_delete_at.dict.table(.x, index)
elem
}
#' @name dict.table
#' @rdname dict.table
#' @details
#' * `ref_pop(.x, index)` return element at given column index and remove the
#' column from the dict.table object.
#' @examples
#'
#' dit = dict.table(a = 1:3, b = 4:6)
#' ref_pop(dit, "a")
#' ref_pop(dit, 1)
#'
#' try({
#' ref_pop(dit, "x") # index 'x' not found
#' })
NULL