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

add sep argument to str_dup #564

Merged
merged 14 commits into from
Aug 20, 2024
Merged

add sep argument to str_dup #564

merged 14 commits into from
Aug 20, 2024

Conversation

edward-burn
Copy link
Contributor

closes #487

Adds a sep argument to str_dup. Becasue this is not supported by stri_dup, do separately and use lapply to go through strings.

closes tidyverse#487

Adds a sep argument to str_dup. Becasue this is not supported by stri_dup, do separately and use lapply to go through strings.
Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for so much for your work. I've written a bunch of comments, but overall the direction is good, and I really appreciate you putting the time in. (And volunteering to be publicly eviscerated).

NEWS.md Outdated Show resolved Hide resolved
NEWS.md Outdated Show resolved Hide resolved
tests/testthat/test-dup.R Outdated Show resolved Hide resolved
tests/testthat/test-dup.R Outdated Show resolved Hide resolved
tests/testthat/test-dup.R Outdated Show resolved Hide resolved
R/dup.R Outdated
# stri_dup does not currently support sep
check_string(sep)
lapply(seq_along(string), function(i) {
if (length(times) == 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love how we have to condition on the length of times to handle vectorisation (i.e. when times is a scalar and string is a vector). Could we instead take the output from the vctrs::vec_size_common and use that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried this but the result of vctrs::vec_size_common is the length of the string input, not the length of times

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ooops needs to be vec_recycle_common()

R/dup.R Outdated
} else {
# stri_dup does not currently support sep
check_string(sep)
lapply(seq_along(string), function(i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use map_chr() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be being a bit dim, but I couldn't manage to get this to work. The problem I was hitting was that map_chr wants to return a length 1 vector.

R/dup.R Outdated Show resolved Hide resolved
@edward-burn
Copy link
Contributor Author

@hadley thanks for your review (and adding some nice comments in amongst the requested changes 😄). I have left a few comments above.

stri_dup(string, times)
str_dup <- function(string, times, sep = NULL) {
input <- vctrs::vec_recycle_common(string = string, times = times)
check_string(sep, allow_null = TRUE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to move the other input check up here so that all the input checks are together.

@@ -13,3 +13,21 @@ test_that("0 duplicates equals empty string", {
test_that("uses tidyverse recycling rules", {
expect_error(str_dup(1:2, 1:3), class = "vctrs_error_incompatible_size")
})

test_that("uses sep argument", {
expect_equal(str_dup("abc", 1, sep = "-"), "abc")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to make it a bit more clear was different in each test by keeping more stuff the same.

@hadley hadley merged commit 0b92cfb into tidyverse:main Aug 20, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: sep argument in str_dup.
2 participants