Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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).

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.

@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.

})

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
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