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

first and last with nchar #23960

Merged
merged 5 commits into from
Oct 16, 2017
Merged

first and last with nchar #23960

merged 5 commits into from
Oct 16, 2017

Conversation

bkamins
Copy link
Member

@bkamins bkamins commented Oct 2, 2017

Implements new functionality of first and last for AbstractString as discussed in #23765.

"""
first(str::AbstractString, nchar::Integer)

Get a string consisting of first `nchar` characters of `str`.
Copy link
Member

Choose a reason for hiding this comment

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

"the first"? Same for "last".

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

NEWS.md Outdated
@@ -236,8 +236,12 @@ This section lists changes that do not have deprecation warnings.
Library improvements
--------------------

* Functions `first` and `last` now accept `nchar` argument for `AbstractString`.
If this argument is used they reutrn a substring consisting of first/last `nchar`
Copy link
Member

Choose a reason for hiding this comment

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

"substring" could be confused with SubString.

Copy link
Member Author

Choose a reason for hiding this comment

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

replaced "substring" with "string"

NEWS.md Outdated
@@ -236,8 +236,12 @@ This section lists changes that do not have deprecation warnings.
Library improvements
--------------------

* Functions `first` and `last` now accept `nchar` argument for `AbstractString`.
If this argument is used they reutrn a string consisting of first/last `nchar`
Copy link
Member

Choose a reason for hiding this comment

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

reutrn -> return

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@bkamins
Copy link
Member Author

bkamins commented Oct 2, 2017

I have improved the implementation. The old version were not type stable for AbstractString different than String.

@stevengj
Copy link
Member

stevengj commented Oct 2, 2017

It seems weird to add these for strings but not for arrays and other iterable collections.

return str[1:nchar]
end
str[1:nextind(str, 1, nchar-1)]
end
Copy link
Member

Choose a reason for hiding this comment

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

Should it return a SubString?

Copy link
Member Author

Choose a reason for hiding this comment

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

My recommendation in #23765 is that not. Usually first and last would be used with a small nchar so there is not much gain from SubString (or actually a loss if original string is very large, we get only a small part of it and the original string can be discarded). Observe that it can be a SubString if the original string is a SubString.

@bkamins
Copy link
Member Author

bkamins commented Oct 2, 2017

@stevengj regarding adding first and last to other iterable collections was actually proposed in #23765. However, it requires a separate implementation for strings if we want it to be efficient. I am now concentrating on improving API for strings (as per discussion in #23765).
Therefore I wanted to make first and last for general colletions a separate PR.

@kshyatt
Copy link
Contributor

kshyatt commented Oct 16, 2017

@stevengj and @fredrikekre are we satisfied with this? I can't see anything that wasn't addressed (but I'm tired) and it's been open a long time.

@fredrikekre fredrikekre merged commit 7e7a03f into JuliaLang:master Oct 16, 2017
@bkamins bkamins deleted the firstlast branch October 16, 2017 22:31
if 0 <= nchar <= 1
return str[1:nchar]
end
str[1:nextind(str, 1, nchar-1)]
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it will give a BoundsError for nchar > length(str). Shouldn't it be min(endof(str), nextind(str, 1, nchar-1))?

Copy link
Member

Choose a reason for hiding this comment

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

Or is BoundsError intended here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was intended. The idea is to have an invariant length(first(s, nchar)) == nchar. Similarly for last.
This might be changed the way you propose - then also docstring should be changed as it now promises nchar characters in the produced string.
I slightly prefer the current implementation (as it ensures the invariant) but I can see the rationale behind your proposal so I am OK change it.

Copy link
Member

Choose a reason for hiding this comment

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

If we changed it to "at most" nchar elements of the string, then it would be similar to take.

My feeling is that using "at most" nchar elements would be a big more flexible, and it wouldn't hurt the uses where you want exactly nchar elements except that it wouldn't throw an error for a string of the wrong size.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. I will make a PR to have a clear decision point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:strings "Strings!"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants