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

Added reversed Python function for arrays and strings #4820

Merged
merged 4 commits into from Dec 3, 2018

Conversation

jigold
Copy link
Collaborator

@jigold jigold commented Nov 21, 2018

No description provided.

@tpoterba
Copy link
Contributor

how hard would it be to add a step argument for StringSlice and ArraySlice? Then this would just be a[::-1]!

def reverse(seq):
seq = range(0, len(seq)).map(lambda i: seq[len(seq) - 1 - i])
if x.dtype == tstr:
seq = hl.delimit(seq, '')
Copy link
Contributor

Choose a reason for hiding this comment

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

this is probably really inefficient

seq = hl.delimit(seq, '')
return seq

return bind(lambda seq: cond(len(seq) != 0, reverse(seq), seq), x)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you need the length-0 check here? it'll work either way, no?

@jigold
Copy link
Collaborator Author

jigold commented Nov 21, 2018

I think it would be relatively straightforward to add the step to array slicing. I looked at StringSlice and it's implemented by doing a memcpy of the bytes for the entire slice. I'd have to think about it some more.

Copy link
Contributor

@danking danking left a comment

Choose a reason for hiding this comment

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

modulo the one code change this seems fine to me.


typ = x.dtype
x = range(0, len(x)).map(lambda i: x[len(x) - 1 - i])
if typ == tstr:
Copy link
Contributor

Choose a reason for hiding this comment

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

typ is only used once, so just inline x.dtype here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't inline the type because x is mutated and will always be Array[String] after line 2 even if the input is a string.

@jigold
Copy link
Collaborator Author

jigold commented Nov 26, 2018

@tpoterba Are you okay with this?

@tpoterba
Copy link
Contributor

yes totally!

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