Skip to content

Commit

Permalink
Add TraverseFilter instance for Set to alleycats
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastro committed Feb 5, 2019
1 parent d3b57b2 commit 5e5147b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
21 changes: 12 additions & 9 deletions alleycats-core/src/main/scala/alleycats/std/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package std

import export._

@reexports(
EmptyKInstances,
ListInstances,
OptionInstances,
SetInstances,
TryInstances,
IterableInstances,
FutureInstances
) object all extends LegacySetInstances with LegacyTryInstances with LegacyIterableInstances with MapInstances
@reexports(EmptyKInstances,
ListInstances,
OptionInstances,
SetInstances,
TryInstances,
IterableInstances,
FutureInstances) object all
extends LegacySetInstances
with LegacySetInstancesBinCompat0
with LegacyTryInstances
with LegacyIterableInstances
with MapInstances
22 changes: 20 additions & 2 deletions alleycats-core/src/main/scala/alleycats/std/set.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package alleycats.std

import cats.{Applicative, Eval, Foldable, Monad, Monoid, Traverse}
import cats.{Applicative, Eval, Foldable, Monad, Monoid, Traverse, TraverseFilter}
import export._

import scala.annotation.tailrec
Expand Down Expand Up @@ -116,14 +116,32 @@ object SetInstances {
override def collectFirstSome[A, B](fa: Set[A])(f: A => Option[B]): Option[B] =
fa.collectFirst(Function.unlift(f))
}

@export(Orphan)
implicit val setTraverseFilter: TraverseFilter[Set] =
new TraverseFilter[Set] {
val traverse: Traverse[Set] = setTraverse

def traverseFilter[G[_], A, B](fa: Set[A])(f: A => G[Option[B]])(implicit G: Applicative[G]): G[Set[B]] =
fa.foldLeft(G.pure(Set.empty[B])) { (gSet, a) =>
G.map2(f(a), gSet) {
case (Some(b), set) => set + b
case (None, set) => set
}
}
}
}

@reexports(SetInstances)
object set extends LegacySetInstances
object set extends LegacySetInstances with LegacySetInstancesBinCompat0

// TODO: remove when cats.{ Set, Traverse } support export-hook
trait LegacySetInstances {
implicit def legacySetMonad(implicit e: ExportOrphan[Monad[Set]]): Monad[Set] = e.instance

implicit def legacySetTraverse(implicit e: ExportOrphan[Traverse[Set]]): Traverse[Set] = e.instance
}

trait LegacySetInstancesBinCompat0 {
implicit def legacySetTraverseFilter(implicit e: ExportOrphan[TraverseFilter[Set]]): TraverseFilter[Set] = e.instance
}
4 changes: 4 additions & 0 deletions alleycats-tests/src/test/scala/alleycats/tests/SetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package alleycats.tests
import alleycats.laws.discipline._
import cats.Foldable
import cats.kernel.laws.discipline.SerializableTests
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.TraverseFilterTests

import alleycats.std.all._

class SetSuite extends AlleycatsSuite {
checkAll("FlatMapRec[Set]", FlatMapRecTests[Set].tailRecM[Int])

checkAll("Foldable[Set]", SerializableTests.serializable(Foldable[Set]))

checkAll("TraverseFilter[Set]", TraverseFilterTests[Set].traverseFilter[Int, Int, Int])
}

0 comments on commit 5e5147b

Please sign in to comment.