From d6bfc3b9cd2f0b05284f96521c1466a328bf162a Mon Sep 17 00:00:00 2001 From: DavisVaughan Date: Thu, 4 Nov 2021 16:24:53 -0400 Subject: [PATCH] Implement `tidyr_chop2()` Since we don't have `vec_chop2()` yet https://github.com/r-lib/vctrs/pull/1226 --- R/utils.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/R/utils.R b/R/utils.R index 0ecedd9a9..f6be5cab0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -130,6 +130,24 @@ tidyr_new_list <- function(x) { x } +# What `vec_chop2()` would be. +# Equivalent to `vec_chop(x)`, but moves names of `x` to the result. +tidyr_chop2 <- function(x) { + names <- vec_names(x) + + if (!is.null(names)) { + x <- vec_set_names(x, NULL) + } + + out <- vec_chop(x) + + if (!is.null(names)) { + out <- vec_set_names(out, names) + } + + out +} + apply_names_sep <- function(outer, inner, names_sep) { as.character(glue("{outer}{names_sep}{inner}")) }