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 tokens_xptr object #2210

Merged
merged 177 commits into from
Apr 9, 2023
Merged

Add tokens_xptr object #2210

merged 177 commits into from
Apr 9, 2023

Conversation

koheiw
Copy link
Collaborator

@koheiw koheiw commented Feb 15, 2023

For #2208, I created a tokens_xptr object, a sub-class of tokens, whose underlying object is an external pointer instead of a list. All the underlying C++ code for tokens_* functions are also upgraded to accept the new object.

Users can switch between tokens and tokens_xptr objects via as.tokens() and as.tokens_xptr() whenever they want to. If they apply tokens_* functions to old objects, as.tokens_xptr() is applied to the input and as.tokens() to the output internally, so they do not even notice the changes in underlying functions.

Apart from the detail of the implementation, remaining design issues are when the tokens_xptr methods should return a deep copy of the input objects. Currently, a deep copy is returned after subsetting (e.g. [], tokens_subset() and tokens_sample()).

We can create tokens_xptr.corpus(), or tokens.corpus(xptr = TRUE), to get tokens_xptr objects straight away. tokens.tokens() is already a wrapper around tokens.tokens_xptr().

We should also add env to functions that perform NSE just like I did in tokens_group() and tokens_sample(). Otherwise, developers find it difficult to nest them in their functions.

For now, I kept the old code for dfm.tokens() and tokens_recompile.tokens() to compare results.

@kbenoit

This comment was marked as resolved.

@koheiw

This comment was marked as resolved.

@kbenoit

This comment was marked as resolved.

@kbenoit

This comment was marked as resolved.

R/tokens.R Show resolved Hide resolved
R/tokens_xptr.R Outdated Show resolved Hide resolved
@kbenoit
Copy link
Collaborator

kbenoit commented Apr 4, 2023

I like the addition of the xptr logical in tokens(). One thing about naming, it suggests we should name this new object tokensx, since our grammar is objectclass_verb, so that tokens_xptr suggests a function that takes in and returns a tokens object.

So the constructor and object class could just be called tokensx() and tokensx, although I like tokens(x, xptr = TRUE) even better.

What do you think?

@koheiw
Copy link
Collaborator Author

koheiw commented Apr 4, 2023

I like the addition of the xptr logical in tokens(). One thing about naming, it suggests we should name this new object tokensx, since our grammar is objectclass_verb, so that tokens_xptr suggests a function that takes in and returns a tokens object.

tokens_xptr is a sub-class of tokens, so there is no problem for tokens() to return the object.

> class(tokens("aa", xptr = TRUE))
[1] "tokens_xptr" "tokens" 

@koheiw koheiw merged commit cb7e666 into v4 Apr 9, 2023
@koheiw koheiw deleted the dev-xtokens branch April 9, 2023 02:55
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.

None yet

3 participants