Skip to content

Commit

Permalink
feat: as_callbacks returns named list (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc authored Dec 4, 2024
1 parent 7885c4c commit 051f091
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mlr3misc (development version)

* feat: `as_callbacks()` returns a list named by the callback ids now.

# mlr3misc 0.16.0

* fix: `crate()` is using the correct 'topenv' environment now.
Expand Down
5 changes: 3 additions & 2 deletions R/Callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ as_callbacks.NULL = function(x, ...) { # nolint
#' @rdname as_callback
#' @export
as_callbacks.list = function(x, clone = FALSE, ...) { # nolint
lapply(x, as_callback, clone = clone, ...)
callbacks = lapply(x, as_callback, clone = clone, ...)
set_names(callbacks, map(callbacks, "id"))
}

#' @rdname as_callback
#' @export
as_callbacks.Callback = function(x, clone = FALSE, ...) { # nolint
list(if (clone) x$clone(deep = TRUE) else x)
set_names(list(if (clone) x$clone(deep = TRUE) else x), x$id)
}

#' @title Call Callbacks
Expand Down
3 changes: 2 additions & 1 deletion man/mlr3misc-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions tests/testthat/test_Callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ test_that("call_back() function works", {
expect_equal(context$d, 1)
})



test_that("as_callbacks.Callback works", {
CallbackTest = R6Class("CallbackTest",
inherit = Callback,
Expand All @@ -104,4 +102,10 @@ test_that("as_callbacks.Callback works", {
test_callback = CallbackTest$new(id = "mlr3misc.test", label = "Test Callback", man = "mlr3misc::Callback")

expect_list(as_callbacks(test_callback))
expect_names(names(as_callbacks(test_callback)), identical.to = "mlr3misc.test")

test_callback_2 = CallbackTest$new(id = "mlr3misc.test_2", label = "Test Callback", man = "mlr3misc::Callback")

expect_list(as_callbacks(list(test_callback, test_callback_2)))
expect_names(names(as_callbacks(list(test_callback, test_callback_2))), identical.to = c("mlr3misc.test", "mlr3misc.test_2"))
})

0 comments on commit 051f091

Please sign in to comment.