14

The apidoc of distinct in SeqLike says:

Builds a new sequence from this sequence without any duplicate elements. Returns: A new sequence which contains the first occurrence of every element of this sequence.

Do I feel it correct that no ordering guarantee is provided? More generally, do methods of SeqLike provide any process-in-order (and return-in-order) guarantee?

2 Answers 2

15

On the contrary: operations on Seqs guarantee the output order (unless the API says otherwise). This is one of the basic properties of sequences, where the order matters, versus sets, where only containment matters.

3
  • So SeqLike doesn't have to guarante but Seq has?
    – ron
    Jul 19, 2011 at 21:36
  • I don't think this is technically correct. While sequences obviously have order I couldn't find anything that would mention preserving it on every operation. I agree that some people would expect this, but I don't think an implementation that puts sequence items into a Set and returns them in any order would violate any contract. Having said that I believe current implementation preserves order.
    – pazabo
    Mar 26, 2015 at 6:23
  • @pazabo You're probably right that there is no strong guarantee—neither from a specification, nor, of course from any tool, compiler, linter, etc., but anyone going against this principle can easily be accused of violating the principle of least surprise, and the standard lib doesn't do it. Mar 26, 2015 at 7:57
1

It depends on the collection you were using in the first place. If you had a list you'll get your order. If on the other hand you had a set, then probably not.

2
  • A set isn't SeqLike, though and the OP's stated the collection referred to is SeqLike Jul 18, 2011 at 16:30
  • Yeah, I guess my answer is more general... I should have expressed myself more clearly.
    – agilesteel
    Jul 18, 2011 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.