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 operator+ overload for T and Vector<T> #75

Open
htiek opened this issue Feb 1, 2023 · 2 comments
Open

Add operator+ overload for T and Vector<T> #75

htiek opened this issue Feb 1, 2023 · 2 comments

Comments

@htiek
Copy link
Collaborator

htiek commented Feb 1, 2023

Currently, you can write vec + elem to get back a new Vector that consists of all the elements of vec with elem appended to the. end. However, the reverse elem + vec does not compile. We should consider adding an overload of operator+ that addresses this.

@zelenski
Copy link
Owner

vec + elem same as elem + vec ? The latter seems to show intent to prepend element rather than append. Do you see a significant use case for it?

I am generally a fan of reduce/maintain smaller library footprint, esp. for interfaces we publish to students...

@htiek
Copy link
Collaborator Author

htiek commented Sep 13, 2023

I agree that a smaller interface is a nice thing to have.

There's a nice parallel between the overloads for Vector<T> and for std::string. You can write str + ch to form a new string with a character at the end, and you can write ch + str to form a new string with a character added at the beginning. I had some students ask me a while back why the same doesn't work for Vector. This particularly came up with some recursion warm-up exercises where code for strings looks really different than code for Vectors. An example:

/* Totally fine */
string toLowerCase(const string& input) {
    if (input == "") return "";
    return tolower(input[0]) + toLowerCase(input.substr(1));
}

/* Oops, doesn't compile. */
Vector<char> toLowerCase(const Vector<char>& input) {
    if (input.isEmpty()) return { };
    return tolower(input[0]) + toLowerCase(input.subList(1));
}

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

No branches or pull requests

2 participants