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

Offer 'type.convert' in interface? #28

Closed
MichaelChirico opened this issue Dec 14, 2024 · 2 comments · Fixed by #29
Closed

Offer 'type.convert' in interface? #28

MichaelChirico opened this issue Dec 14, 2024 · 2 comments · Fixed by #29

Comments

@MichaelChirico
Copy link
Contributor

Finally giving {nc} a try, it's quite handy!

It took me a while to find out capture_first_vec() offers a way to do type conversion:

Each function must be un-named, and is used to convert the previous capture group.

This is nice, but maybe "too much" flexibility. I think utils::type.convert() would offer a much simpler interface here, instead of 4 different as.integer entries, I could just pass type.convert=TRUE and let R figure out the appropriate type for me. That's also the approach data.table::tstrsplit() takes which would make it more familiar as well.

@tdhock tdhock mentioned this issue Dec 16, 2024
@tdhock
Copy link
Owner

tdhock commented Dec 16, 2024

Hey that is a great idea! can you please try #29 and tell me if that is what you had in mind?

@tdhock
Copy link
Owner

tdhock commented Dec 20, 2024

for completeness the 4 different as.integer entries were something like this, which is similar to a new example:

nc::capture_first_vec(
  c("p=0,4 v=3,-3","p=6,3 v=-1,-3"),
  "p=",
  px="[0-9]", as.integer,
  ",",
  py="[0-9]", as.integer,
  " v=",
  vx="[-0-9]+", as.integer,
  ",",
  vy="[-0-9]+", as.integer)

one way to use the old code (before type.convert arg) would be by defining a sub-pattern list as below:

ipat <- list("[-0-9]+", as.integer)
nc::capture_first_vec(
  c("p=0,4 v=3,-3","p=6,3 v=-1,-3"),
  "p=",
  px=ipat,
  ",",
  py=ipat,
  " v=",
  vx=ipat,
  ",",
  vy=ipat)

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 a pull request may close this issue.

2 participants