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 vec_slice2() and vec_assign2() #1228

Open
wants to merge 3 commits into
base: add-chop2
Choose a base branch
from

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented Aug 19, 2020

Branched from #1226.

Following vec_chop2() and vec_map(), this further explores the idea of "extracting" variants of vctrs primitives.

  • With atomic vectors, vec_slice2() and vec_assign2() are equivalent to vec_slice() and vec_assign(). The only difference is that vec_slice2() zaps the names of the result.

  • vec_slice2() with lists makes use of the list storage requirement and calls .subset2().

  • vec_assign2() with lists wraps the RHS into a list. Genericity is ensured by vec_assign() which coerces the RHS (now a list) to the type of the LHS (a list type). This allows the class of the LHS to initialise attributes, possibly based on the contents of the RHS.

One big difference with [[<- is that NULL values are not treated as a deletion sentinel.

x <- list(foo = 1, bar = 2)

vec_assign2(x, 2, NULL)
#> $foo
#> [1] 1
#>
#> $bar
#> NULL

There is currently no way to remove elements:

vec_assign2(x, 2, zap())
#> Error: Can't zap elements.

These operations are unexported for now.

i <- vec_as_location2(i, vec_size(x))
.subset2(x, i)
} else {
out <- vec_slice(x, i)
Copy link
Member

Choose a reason for hiding this comment

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

Use vec_as_location2() on i?

value <- list(value)
}

vec_assign(x, i, value, x_arg = x_arg, value_arg = value_arg)
Copy link
Member

Choose a reason for hiding this comment

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

Use vec_as_location2() on i?

@DavisVaughan
Copy link
Member

Closes #141
Closes #626

@DavisVaughan
Copy link
Member

Should [[<-.vctrs_vctr use vec_assign2()? I guess it would change the behavior of assigning NULL into list-based vctrs

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.

2 participants