Skip to content

Add join method to StringExpression#9459

Merged
danking merged 2 commits intohail-is:mainfrom
nawatts:string-join-method
Sep 17, 2020
Merged

Add join method to StringExpression#9459
danking merged 2 commits intohail-is:mainfrom
nawatts:string-join-method

Conversation

@nawatts
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread hail/python/hail/expr/expressions/typed_expressions.py Outdated
@tpoterba
Copy link
Copy Markdown
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.

4 participants