Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Add Iterable.takeUntil() or similar #5617

Open
CeylonMigrationBot opened this issue Sep 1, 2015 · 10 comments
Open

Add Iterable.takeUntil() or similar #5617

CeylonMigrationBot opened this issue Sep 1, 2015 · 10 comments

Comments

@CeylonMigrationBot
Copy link

[@jvasileff] The method:

{Element*} takeUntil(Boolean stopping(Element element))

would be similar to takeWhile, with the following differences:

  • The passed in function would return true to signal the end of the stream, rather than false
  • The element that caused the "stop" would be included in the resultant stream (takeWhile discards the "final" element).

This is useful if your goal is to take all elements up to and including the "element you are looking for". found could also be used as the name for the passed in function.

[Migrated from ceylon/ceylon.language#739]

@CeylonMigrationBot
Copy link
Author

[@quintesse] Although I understand why you want this I personally feel it's wrong to use takeUntil for this. Logically the only difference between a takeWhile and a takeUntil should be the fact that the stop condition is inverted. I think people might find it hard to remember which of the two is the one that includes and which is the one that excludes the element at the point the stop condition is met.

I'd rather have a boolean argument inclusive or a different name for the method itself, ie takeWhileInclusive (personally I prefer being verbose but clear).

@CeylonMigrationBot
Copy link
Author

[@jvasileff] Yeah, I agree that takeUntil does not imply inclusion of the stopper.

There's a similar discussion here, with the following names suggested:

  • takeUntil
  • takeUntilInclusive
  • takeWhileInclusive
  • skipAfter
  • terminateAfter

@CeylonMigrationBot
Copy link
Author

[@tombentley] Can this wait till 1.3?

@CeylonMigrationBot
Copy link
Author

[@gavinking] Yes. (OTOH if someone can think of a decent name for it, it could be implemented in 5 mins.) In the absence of such a name I will reassign it.

@CeylonMigrationBot
Copy link
Author

[@quintesse] The other option would be to introduce a delay() method that takes a first value and a function that together work to return each value one invocation "late", eg seq.takeWhile(delay(true, (c)=>c!=',')). Just brilliant ;)

@CeylonMigrationBot
Copy link
Author

[@gavinking] @quintesse interesting idea...

@CeylonMigrationBot
Copy link
Author

[@jvasileff] IMO delay is too complicated to obviate the need for the unnamed function, but very cool @quintesse!

I almost adopted it for my project, but terminateAfter(p) can return the nonempty {Element+}, and takeWhile(delay(true, p)) can't.

@CeylonMigrationBot
Copy link
Author

[@luolong] I second that. As for names, what about these:

  • Iterable.takeWhile, Iterable.takeUntil
  • Iterable.skipWhile, Iterable.skipUntil

The convention would be that methodes with *While are inclusive and methods with *Until are exclusive.

(I have no idea what would be the use cases for skipAfter and terminateAfter -- to me it seems like an alias to takeWhile/takeUntil)

@CeylonMigrationBot
Copy link
Author

[@quintesse]
@luolong sure, we could do that but to me that just means that people will continuously look up the docs to see what was the difference again between the two.

We had this (very long) discussion with span() and measure() and even though I've done work on the implementation of those two I still confuse them (and even worse when using x..y vs x:y because you won't even get documentation hints).

Now if this was a common convention in other languages and everybody was accustomed to the idea perhaps.... maybe, just maybe, we could make a comparison with while loops vs do-until loops and make it easier to remember that way. Hmmm.

@CeylonMigrationBot
Copy link
Author

[@FroMage] Sorry, wrong thread. I deleted my comment.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant