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 join method to StringExpression #9459

Merged
merged 2 commits into from
Sep 17, 2020
Merged

Conversation

nawatts
Copy link
Contributor

@nawatts nawatts commented Sep 17, 2020

Requested in #9188.

This is similar to hl.delimit. However, unlike hl.delimit, this throws a TypeError if a collection with a non-string element type is provided. This matches the behavior of Python's str.join.

:class:`.StringExpression`
Joined string expression.
"""
if collection.dtype.element_type != tstr:
Copy link
Contributor

Choose a reason for hiding this comment

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

should we convert elements to string automatically if of another type? Would let you do hl.str(',').join([1,2,3])

Copy link
Contributor

Choose a reason for hiding this comment

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

not necessary though, up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started out having this call hl.delimit, but added the element type check to make it behave more like Python's string.join, which raises a TypeError if passed an iterable with non-string elements.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think checking first and then calling delimit() would replicate Python functionality?

Copy link
Contributor

Choose a reason for hiding this comment

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

Regardless of whether we do the element type check or not, we should still call hl.delimit afterwards as opposed to mkString, to minimize how often we leak java implementation details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I misunderstood the comment and thought you were suggesting replace the entire implementation with a call to delimit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After thinking about it some more, I like the type check as a guard rail. Automatic type conversion would be useful for things like joining an array of numbers. On the other hand, it would also let you accidentally join an array of structs when it's more likely you wanted to join from one of the struct's fields.

hail/python/hail/expr/expressions/typed_expressions.py Outdated Show resolved Hide resolved
@tpoterba
Copy link
Contributor

thanks Nick!

@danking danking merged commit 0878ee4 into hail-is:main Sep 17, 2020
pwc2 pushed a commit to pwc2/hail that referenced this pull request Sep 21, 2020
* Add join method to StringExpression

* Use hl.delimit
@nawatts nawatts deleted the string-join-method branch May 6, 2021 12:37
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

4 participants