-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cats.syntax | ||
|
||
import scala.collection.immutable.{SortedSet, SortedMap} | ||
import cats.data.NonEmptySet | ||
import cats.Order | ||
|
||
trait SetSyntax { | ||
implicit final def catsSyntaxSet[A](se: SortedSet[A]): SetOps[A] = new SetOps(se) | ||
} | ||
|
||
final class SetOps[A](val se: SortedSet[A]) extends AnyVal { | ||
|
||
/** | ||
* Returns an Option of NonEmptySet from a SortedSet | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import scala.collection.immutable.SortedSet | ||
* scala> import cats.data.NonEmptySet | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val result1: SortedSet[Int] = SortedSet(1, 2) | ||
* scala> result1.toNes | ||
* res0: Option[NonEmptySet[Int]] = Some(TreeSet(1, 2)) | ||
* | ||
* scala> val result2: SortedSet[Int] = SortedSet.empty[Int] | ||
* scala> result2.toNes | ||
* res1: Option[NonEmptySet[Int]] = None | ||
* }}} | ||
*/ | ||
def toNes: Option[NonEmptySet[A]] = NonEmptySet.fromSet(se) | ||
|
||
/** | ||
* Groups elements inside this `SortedSet` according to the `Order` of the keys | ||
* produced by the given mapping function. | ||
* | ||
* {{{ | ||
* scala> import cats.data.NonEmptySet | ||
* scala> import scala.collection.immutable.{SortedMap, SortedSet} | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val sortedSet = SortedSet(12, -2, 3, -5) | ||
* | ||
* scala> sortedSet.groupByNes(_ >= 0) | ||
* res0: SortedMap[Boolean, NonEmptySet[Int]] = Map(false -> TreeSet(-5, -2), true -> TreeSet(3, 12)) | ||
* }}} | ||
*/ | ||
def groupByNes[B](f: A => B)(implicit B: Order[B]): SortedMap[B, NonEmptySet[A]] = { | ||
implicit val ordering = B.toOrdering | ||
toNes.fold(SortedMap.empty[B, NonEmptySet[A]])(_.groupBy(f).toSortedMap) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters