.splitWhen
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 5.5k
- Forks
- 1.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 5
Description
Hiya,
I largely use cats for advent of code solutions, just to provide some common utility functions which are handy for dealing with collections.
See my last contribution for .slidingN.
The idea for this is to provide a .splitWhen which will take a Foldable[A], and split it when the supplied predicate returns true.
So something quick which I wrote for advent of code (list only, but should be workable for Foldable as well):
https://gist.github.com/Slakah/f17614e62bc4173ac4b11d653e2f63f8
def splitWhen[A](list: List[A], f: A => Boolean): List[List[A]] = {
list.reverse.foldLeft((List.empty[List[A]], true)) {
case ((Nil, _), e) if f(e) => (Nil, true)
case ((Nil, _), e) => (List(List(e)), false)
case ((head :: tail, _), e) if f(e) => (head :: tail, true)
case ((head :: tail, false), e) => ((e :: head) :: tail, false)
case ((head :: tail, true), e) => (List(e) :: head :: tail, false)
}._1
}
Any feedback appreciated! Don't know that I have much time to work on this though!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked .slidingN pull request and the provided gist, then investigate how Cats exposes collection operations for Foldable. Clarify the intended split semantics and identify the appropriate API and tests; done should mean a documented .splitWhen operation works across supported Foldable values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100