Stream: contributing

Topic: List.takeWhile


view this post on Zulip iko (Apr 20 2023 at 09:02):

How would you feel about an implementation like this?
Screenshot_20230420_120155.png

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:04):

takeWhile usually only returns the part of the list that it takes, right? this is more like a split

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:04):

(which is still useful!)

view this post on Zulip iko (Apr 20 2023 at 09:05):

I think this is the behaviour that is expected in the issue: https://github.com/roc-lang/roc/issues/5138

I agree though, it doesn't really match the name

view this post on Zulip iko (Apr 20 2023 at 09:07):

maybe breakOn?

view this post on Zulip iko (Apr 20 2023 at 09:09):

Not sure about the Predicate type itself though. I don't think it can be made anonymous.

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:10):

I think that is too fancy, I don't think the predicate should be recursive

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:11):

it would be more efficient to fold to find the index, and then slice, for instance

view this post on Zulip iko (Apr 20 2023 at 09:18):

Folkert de Vries said:

it would be more efficient to fold to find the index, and then slice, for instance

I think the type I proposed still allows this sort of implementation

The recursive predicate allows you to make a decision using an arbitrart number of elements. I think this is the general case for what was described in the issue. Using a simple boolean predicate would not allow for making a decision based on two consecutive elements.

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:20):

this is true, but would that be convenient to use in practice?

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:20):

this makes me think of a fold, which is a traversal that keeps some state

view this post on Zulip iko (Apr 20 2023 at 09:22):

In practice the type I proposed can be used as just a simple boolean predicate (Take and Break) without using NeedMore.

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:24):

yes I understand, but is the inclusion of NeedMore really worth it. it makes the signature more complicated

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:25):

and its usecases can be covered in other ways

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:25):

perhaps to resolve this: can you create an example that uses NeedMorethat might be included in the documentation?

view this post on Zulip iko (Apr 20 2023 at 09:25):

I think we are actually debating if the function proposed in the Github issue is needed in builtins at all.

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:25):

then let's see if we like that code

view this post on Zulip iko (Apr 20 2023 at 09:26):

sure

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:26):

yeah I think we should include the simpler (standard) version of takeWhile : List a, (a -> Bool) -> List a

view this post on Zulip Folkert de Vries (Apr 20 2023 at 09:27):

and anything fancier than what that can do, can be done with List.walk and variants, plus a List.splitAt : List a, Nat -> List a

view this post on Zulip Richard Feldman (Apr 20 2023 at 12:26):

makes sense to me!

view this post on Zulip Luke Boswell (Apr 20 2023 at 12:27):

Good discussion, thanks for clarification Folkert. I copied what I had into the issue, glad to see some design in here :smile:

view this post on Zulip iko (Apr 20 2023 at 14:51):

To be clear: we decided to not implement takeWhile as described in the Github issue, and instead decided to add a more traditional boolean-based takeWhile?

view this post on Zulip Richard Feldman (Apr 20 2023 at 16:01):

yep!


Last updated: Jul 06 2025 at 12:14 UTC