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 OrderedSet class to standard lib #257

Merged
merged 2 commits into from
Oct 22, 2015
Merged

Add OrderedSet class to standard lib #257

merged 2 commits into from
Oct 22, 2015

Conversation

kaeluka
Copy link
Contributor

@kaeluka kaeluka commented Oct 20, 2015

It's based on a binary tree, moderately well tested, not optimised.

@EliasC
Copy link
Contributor

EliasC commented Oct 21, 2015

The Assert bundle is redundant. See assertTrue and assertFalse (with "see", of course I mean look them up in Desugarer.hs, since there is no documentation for it...)

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 21, 2015

If so, I'd suggest removing the native implementation. The stdlib is easier to maintain. Agree?

@EliasC
Copy link
Contributor

EliasC commented Oct 21, 2015

@kaeluka: assertTrue has printf-like functionality which the bundle does not, but everything we can express in encore code rather than special cases in the compiler is a lot better I think. I support phasing out assertTrue. Since this includes changing existing test cases, maybe it makes sense to break this out into a PR of its own?

@EliasC
Copy link
Contributor

EliasC commented Oct 21, 2015

When the warning system is in place, we could mark assertTrue as deprecated.

@supercooldave
Copy link

I agree with removing build-in implementations.

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 21, 2015

Ok, so if this is has passed review (has it?), I suggest we wait for plenary to be merged, then I'll rebase this on top of the new master.

@EliasC
Copy link
Contributor

EliasC commented Oct 21, 2015

I only saw the Assert bundle and wanted to let you know about assertTrue. I can check the rest of the implementation shortly.

Would you separate Assert into a PR of its own?

--
-- Takes a binary relation that returns true if the first argument should go
-- left of the second argument and a binary relation that is used as equality
-- for elements.
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a break of abstraction. goesLeft reveals that OrderedSet uses a tree internally. I suggest replacing cmp and goesLeft by a strcmp like return value (and then moving to something enumy when we have support for this).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 21, 2015

@EliasC: I reacted to your inline comments and the leak of abstraction issue. Pulling out Assert into an extra PR could be done, but since that would be only 14 lines in total and this PR depends on it, I don't see a big benefit.

@EliasC
Copy link
Contributor

EliasC commented Oct 21, 2015

that would be only 14 lines in total

It would be good to also update the existing tests that use assertTrue (although that could be a separate PR as well)

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 21, 2015

That has nothing to do with the Assert lib. Should be a separate PR.

@EliasC
Copy link
Contributor

EliasC commented Oct 22, 2015

I would say changing test cases to use the Assert lib has something to do with the Assert lib, but let's leave it for a later PR anyway.

@EliasC
Copy link
Contributor

EliasC commented Oct 22, 2015

There's one thing in the iterator interface I would like to fix. Consider the following program:

class Main
  def main() : void
    let lt = \ (x : int, y : int) -> x < y
        eq = \ (x : int, y : int) -> x == y
        set = new OrderedSet<int>(lt, eq)
    in{
      for i in [1..10]{
        set.add(i);
      };
      let iter = set.iter()
      in{
        while iter.hasNext(){
          print iter.getCurrent();
          iter.step();
        };
      }
    }

In the last loop, iter.hasNext() checks if there is a next element, not if there is a current element. This means that the loop prints the numbers 1 through 9. To print the last number we need a call to getCurrent outside of the loop. I think hasNext should be called hasMore and return true iff calling getCurrent is possible (probably a check like this.current != null).

@EliasC
Copy link
Contributor

EliasC commented Oct 22, 2015

If you're not going to separate the Assert lib into a PR of its own (which I still think you should), could you at least put it into a commit of its own?

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 22, 2015

Sure, if you show me how! :)

@EliasC
Copy link
Contributor

EliasC commented Oct 22, 2015

Will do! :)

@TobiasWrigstad
Copy link
Contributor

Please put it in a PR of its own. I went hunting for it this morning and could not find it, because I was not expecting to find it hidden in something labelled "ordered set".

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 22, 2015

I'll do that when I'll get around to it, after OOPSLA most likely.

@TobiasWrigstad
Copy link
Contributor

On a related note. There are 80+ uses of assertFalse and assertTrue in the stdlib tests. That will need refactoring before we remove anything. I am not a huge fan of removing something which has more features so casually.

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 22, 2015

See discussion above!

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 22, 2015

I now removed the assert lib from this PR.

@kaeluka
Copy link
Contributor Author

kaeluka commented Oct 22, 2015

@EliasC: Is there anything missing now?

@albertnetymk
Copy link
Contributor

It feels odd to deviate from the main stream approaches Java and C# on this well-understood concept. Making for-loop to support enumerable or iterable natively (for e in ...) would get rid of all the ambiguity, but requires some change to the compiler itself, I think.

An pragmatic approach seems to use the Java convention (hasNext and next) for now, IMO.

An ordered set is based on a binary tree. This implementation is not
balanced yet.
@EliasC
Copy link
Contributor

EliasC commented Oct 22, 2015

This looks good now! Will merge next time I see it :)

EliasC added a commit that referenced this pull request Oct 22, 2015
Add OrderedSet class to standard lib
@EliasC EliasC merged commit 62f1cab into parapluu:master Oct 22, 2015
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.

5 participants