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 scanLeft/scanLeftTail to all non-empty collections #4595

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Prillan
Copy link

@Prillan Prillan commented May 3, 2024

I was quite surprised to find that there were no scanLeft on any of the non-empty collections. Furthermore, the tail of a scan on a non-empty collection is itself non-empty, which might be a useful property to expose. The name scanLeftTail was the best I could come up with for now and it's of course open to bike-shedding.

Putting out this PR as a draft to get some input on it. Will add tests and documentation if you all consider it useful!

val nel = NonEmptyList.of(1, 2, 3)
val f: (Int, Int) => Int = ???
// Before
nel.toList.scanLeftNel(0)(f)
// After
nel.scanLeft(0)(f)

// And, before
nel.tail.scanLeftNel(f(0, nel.head))(f)
// After
nel.scanLeftTail(0)(f)

The others (NonEmptyChain, etc.) don't have the scanLeftNel syntax and requires more work to get the same result.

Finally, the implementations in this draft are a bit mixed. Don't worry about that for now. Again, I'm mostly checking if people would find this useful or not.

Prillan added 2 commits May 3, 2024 15:50
Considering doing a scan on a non-empty collection yields a collection
with at least _two_ elements, this method helps users discover that
fact.
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.

1 participant