glue icon indicating copy to clipboard operation
glue copied to clipboard

Type stability of glue_collapse()

Open krlmlr opened this issue 7 years ago • 5 comments

length(glue::glue_collapse(character()))
#> [1] 0
length(glue::glue_collapse(""))
#> [1] 1
length(glue::glue_collapse(letters))
#> [1] 1

Created on 2018-04-13 by the reprex package (v0.2.0).

Perhaps glue_collapse() should always returned a string?

krlmlr avatar Apr 13 '18 19:04 krlmlr

Those are all characters, how is this not type stable?

jimhester avatar Apr 13 '18 19:04 jimhester

The length varies (length 1 vs. length 0), I can't map_chr(..., glue_collapse). Maybe there's a better term for this feature?

krlmlr avatar Apr 13 '18 20:04 krlmlr

Ah I see, I think 0 length inputs generating 0 length outputs is the correct behavior, but can see how it makes this particular idiom more painful.

jimhester avatar Apr 13 '18 20:04 jimhester

Maybe for vectorized operations, but not for summaries?

library(tidyverse)
tibble(a = numeric()) %>% summarize(sum(a))
#> # A tibble: 1 x 1
#>   `sum(a)`
#>      <dbl>
#> 1       0.

Created on 2018-04-13 by the reprex package (v0.2.0).

krlmlr avatar Apr 13 '18 20:04 krlmlr

Do we want a length-stable variant, perhaps glue_collapse1()?

https://github.com/krlmlr/dm/blob/23b37be9280a227bd21cad8d652a3f6a632ef76a/R/paste.R#L95-L101

krlmlr avatar Apr 12 '20 16:04 krlmlr