Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert partial function creation #13

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions R/map.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
partial_qto_func <- function(f, collapse, sep) {
if (identical(f, qto_callout)) {
function(...) f(...)
} else if ((identical(f, qto_div))) {
function(...) f(..., collapse = collapse)
} else {
function(...) f(..., collapse = collapse, sep = sep)
}
}

resolve_mapping_function <- function(f = NULL,
type = NULL,
collapse = NULL,
sep = NULL,
call = NULL) {
f <- f %||% switch(type,
block = partial_qto_func(qto_block, collapse, sep),
div = partial_qto_func(qto_div, collapse, sep),
callout = partial_qto_func(qto_callout, collapse, sep),
heading = partial_qto_func(qto_heading, collapse, sep),
block = qto_block,
div = qto_div,
callout = qto_callout,
heading = qto_heading,
)
if (!is_function(f)) {
f <- as_function(f, call = call)
Expand All @@ -40,8 +28,7 @@ resolve_mapping_function <- function(f = NULL,
#' "heading".
#' @param .sep,.collapse Additional parameters passed to [qto_block()] if .f
#' does not return a quarto block class object. Ignored if .f does return a
#' quarto block class object. Also passed to the relevant .type function if it supports
#' the collapse and/or sep parameters.
#' quarto block class object.
#' @inheritParams rlang::args_error_context
#' @examples
#' qto_list <- map_qto(
Expand All @@ -64,8 +51,6 @@ map_qto <- function(.x,
.f <- resolve_mapping_function(
f = .f,
type = .type,
collapse = .collapse,
sep = .sep,
call = call
)
map(
Expand Down Expand Up @@ -123,8 +108,6 @@ pmap_qto <- function(.l,
.f <- resolve_mapping_function(
f = .f,
type = .type,
collapse = .collapse,
sep = .sep,
call = call
)
pmap(
Expand Down
3 changes: 1 addition & 2 deletions man/map_qto.Rd

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

3 changes: 1 addition & 2 deletions man/pmap_qto.Rd

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

7 changes: 2 additions & 5 deletions tests/testthat/_snaps/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@
qto_list
Output
[[1]]
foo
foo bar baz

[[2]]
bar

[[3]]
baz
a b c


# pmap_qto works
Expand Down
48 changes: 18 additions & 30 deletions tests/testthat/test_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,28 @@ check_types <- function(lst) {
test_that("resolve_mapping_function works", {
expect_type(resolve_mapping_function(f = ~ .x + 1L), "closure")

block_fn <- resolve_mapping_function(
type = "block",
sep = " ",
collapse = " "
)
block_fn <- resolve_mapping_function(type = "block")
expect_identical(
block_fn("Hello", c("world", "!")),
block_fn("Hello", c("world", "!"), sep = " ", collapse = " "),
qto_block("Hello", c("world", "!"), sep = " ", collapse = " ")
)

div_fn <- resolve_mapping_function(
type = "div",
sep = " ",
collapse = "bar"
)
div_fn <- resolve_mapping_function(type = "div")
expect_identical(
div_fn("foo", "baz"),
qto_div("foo", "bar", "baz")
div_fn("foo", "bar", "baz", collapse = " "),
qto_div("foo", "bar", "baz", collapse = " ")
)

callout_fn <- resolve_mapping_function(
type = "callout",
sep = " ",
collapse = "bar"
)
callout_fn <- resolve_mapping_function(type = "callout")
expect_identical(
callout_fn("foo", "baz"),
qto_callout("foo", "baz")
callout_fn("foo", "baz", collapse = TRUE),
qto_callout("foo", "baz", collapse = TRUE)
)

heading_fn <- resolve_mapping_function(
type = "heading",
sep = " ",
collapse = " "
)
heading_fn <- resolve_mapping_function(type = "heading")
expect_identical(
heading_fn("foo", "baz"),
qto_heading("foo", "baz", collapse = " ", sep = " ")
heading_fn("foo", "baz", sep = " ", collapse = " "),
qto_heading("foo", "baz", sep = " ", collapse = " ")
)

})
Expand All @@ -62,10 +46,14 @@ test_that("map_qto works", {


qto_list <- map_qto(
list("foo", "bar", "baz"),
.f = function(x) x
list(
c("foo", "bar", "baz"),
c("a", "b", "c")
),
.f = function(x) x,
.collapse = " "
)
expect_length(qto_list, 3L)
expect_length(qto_list, 2L)
expect_true(check_types(qto_list))
expect_snapshot(qto_list)
})
Expand Down