Skip to content

Update Custom Set tests to follow standard, update API #114

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

Merged
merged 1 commit into from
Apr 28, 2016

Conversation

IanWhitney
Copy link
Contributor

@IanWhitney IanWhitney commented Apr 20, 2016

I'm addressing two things in this commit.

Bringing Tests in line with the Standard

Our suite of tests for Custom Set did not follow the standard json file in x-common They tests have different function names, don't cover symmetric_difference and introduced some functions (is_superset) that aren't in the standard test file.

I have not implemented the entire standard test suite as it covers sets of mixed types which I don't think is even possible in Rust. Vectors have to be homogeneous. Tuples can mix types, but can't change size. Mixed type sets may be possible, but implementing that seems way beyond the scope
of this exercise.

I think this suite of tests is too long. But until I get a PR in to shorten the standard test suite, I figured I might as well follow it.

Updating the API

I had a couple of problems with the current approach. First, the tests hide the actual API in a helper method. This was probably done to reduce duplication, but I find that it obscures the function under test. So I removed those helpers.

The tests also pushed towards a bunch of implementation that I found unnecessary, specifically the whole Iter implementation. There might be ways to get the current test suite passing without implementing Iter, but I got pretty stuck. My new implementation does not require this implementation, since equality comparisons are now handled by implementing PartialEq.

@IanWhitney
Copy link
Contributor Author

Before merge, I'd like:

  • To hopefully get a reduced custom-set.json file into x-common, so that I can delete about 40% of those tests.
  • Discuss the API, which I think is fine, but I'm probably wrong.
  • Improve the implementation in the example file. I'm sure that can be better.

@IanWhitney
Copy link
Contributor Author

PR submitted to reduce the size of the test suite.

exercism/problem-specifications#232

@IanWhitney
Copy link
Contributor Author

@petertseng

since the PR to slim down the test suite is in limbo, I'm wondering if you have any thoughts on my new API or implementation?


pub fn len(&self) -> usize {
self.elements.len()
impl<T: PartialEq + Ord + Clone> CustomSet<T> {
Copy link
Member

Choose a reason for hiding this comment

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

out of curiosity: since https://doc.rust-lang.org/std/cmp/trait.Ord.html tells me Ord depends on PartialEq, does having PartialEq here make a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PartialEq is not necessary. Thanks for noticing that.

@petertseng
Copy link
Member

May be possible to use more iterator methods rather than creating a vector and conditionally pushing things onto it, but I admit this may be my style bias talking. There is a desire that the example implementation be idiomatic; my current shortcoming is I'm not heavily involved with Rust enough to know whether it is idiomatic to always use iterator methods where possible instead of conditionally pushing onto a mutable vec.

@IanWhitney
Copy link
Contributor Author

I definitely try to favor iterators. Let me poke at the code and see what I can do.

@IanWhitney
Copy link
Contributor Author

If this looks good I'd like to merge it. Depending on the outcome of exercism/problem-specifications#232, there might be another PR that tweaks this exercise further.

CustomSet::new(self.collection
.iter()
.filter(|c| !other.contains(c))
.map(|c| c.clone())
Copy link
Member

Choose a reason for hiding this comment

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

@petertseng
Copy link
Member

I have some small comments, otherwise I feel OK.

Check whether all the collect::<Vec<_>>() could be changed to just collect() as well - I feel like type inference should work there, but I could be wrong.

Just so it's clear what I reviewed, I didn't examine the entire test suite for matching with the exercism common tests so I'm pretty much assuming that if everything's passing it's good. This will mean that if there's a test that's missing I won't know.

@IanWhitney
Copy link
Contributor Author

The tests follow the current standard json file, though not in the exact same order. I did leave out the parts of the current standard that implement mixed-type sets because I didn't think that was appropriate for a Rust implementation. But all the other tests are there. All 64 of them.

@petertseng
Copy link
Member

All right, my last question is just about whether delete should borrow the element it's deleting - I had forgotten about that one and didn't expand comments on outdated diffs

@IanWhitney
Copy link
Contributor Author

Looks like there are two conversations that are folded away in outdated diffs

  1. Should delete borrow
  2. Should new borrow its inputs

I think we're ok with saying 'No' to the 2nd question. However, I would like to pursue the idea of structuring 2-3 exercises so that they introduce lifetimes. That is an unique concept to Rust (afaik) and it is one people struggle with.

As for question 1, now that I see the similarity to HashSet, that change makes sense to me.

IanWhitney pushed a commit to IanWhitney/xrust that referenced this pull request Apr 28, 2016
@petertseng
Copy link
Member

👍

@IanWhitney
Copy link
Contributor Author

Ok. I'll rebase down to a single commit & merge.

I'm addressing two things in this commit.

Bringing Tests in line with the Standard
====

Our suite of tests for Custom Set did not follow the [standard
json file in
x-common](https://github.com/exercism/x-common/blob/d6cd42db16fce5090bb486821a36d0fb1bfb7df2/custom-set.json)
They tests have different function names, don't cover
symmetric_difference and introduced some functions (`is_superset`) that aren't in the
standard test file.

I have not implemented the entire standard test suite as it covers sets
of [mixed
types](https://github.com/exercism/x-common/blob/d6cd42db16fce5090bb486821a36d0fb1bfb7df2/custom-set.json#L475)
which I don't think is even possible in Rust. Vectors have to be
homogeneous. Tuples can mix types, but can't change size. Mixed type
sets may be possible, but implementing that seems way beyond the scope
of this exercise.

I think [this suite of tests is too
long](exercism/discussions#10). But until I
get a PR in to shorten the standard test suite, I figured I might as
well follow it.

Updating the API
====

I had a couple of problems with the current approach. First, the tests
hide the actual API in a helper method. This was probably done to reduce
duplication, but I find that it obscures the function under test. So I
removed those.

The tests also pushed towards a bunch of implementation that I found
unnecessary, specifically the whole [Iter
implementation](https://github.com/exercism/xrust/blob/04b665e989b578c4e46ff100b1b0a00d5df9bb0d/exercises/custom-set/example.rs#L118).
There might be ways to get the current test suite passing without
implementing Iter, but I got pretty stuck. My new implementation does
not require this implementation, since equality comparisons are now
handled by via PartialEq.
@IanWhitney IanWhitney changed the title Update Custom Set tests to follow standard, update API (not yet ready for merge) Update Custom Set tests to follow standard, update API Apr 28, 2016
@IanWhitney IanWhitney merged commit 6256aaa into exercism:master Apr 28, 2016
@IanWhitney IanWhitney deleted the update_custom_set branch May 1, 2016 17:53
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.

3 participants