Skip to content

Commit

Permalink
Better instance definition
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Nov 16, 2023
1 parent f8f7f68 commit 3a277b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions guava/src/main/scala-2/magnolify/guava/FunnelImplicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ trait FunnelImplicits {
implicit val shortFunnel: Funnel[Short] = FunnelInstances.shortFunnel()

implicit def charSequenceFunnel[T <: CharSequence]: Funnel[T] =
FunnelInstances.charSequenceFunnel[T]
FunnelInstances.charSequenceFunnel[T]()

// There is an implicit Option[T] => Iterable[T]
implicit def iterableFunnel[T, C[_]](implicit
fnl: Funnel[T],
ti: C[T] => Iterable[T]
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl).contramap(ti)
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl)

implicit def funnelOps[T](fnl: Funnel[T]): FunnelOps[T] = new FunnelOps(fnl)
}
Expand Down
4 changes: 2 additions & 2 deletions guava/src/main/scala-3/magnolify/guava/FunnelImplicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ trait FunnelImplicits:
given shortFunnel: Funnel[Short] = FunnelInstances.shortFunnel()

given charSequenceFunnel[T <: CharSequence]: Funnel[T] =
FunnelInstances.charSequenceFunnel[T]
FunnelInstances.charSequenceFunnel[T]()

// There is an implicit Option[T] => Iterable[T]
given iterableFunnel[T, C[_]](using
fnl: Funnel[T],
ti: C[T] => Iterable[T]
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl).contramap(ti)
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl)

extension [T](fnl: Funnel[T])
def contramap[U](f: U => T): Funnel[U] = new Funnel[U]:
Expand Down
8 changes: 5 additions & 3 deletions guava/src/main/scala/magnolify/guava/FunnelInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ object FunnelInstances {
def charFunnel(): Funnel[Char] = funnel[Char](_.putChar(_))
def shortFunnel(): Funnel[Short] = funnel[Short](_.putShort(_))

def charSequenceFunnel[T <: CharSequence]: Funnel[T] =
def charSequenceFunnel[T <: CharSequence](): Funnel[T] =
Funnels.unencodedCharsFunnel().asInstanceOf[Funnel[T]]
def iterableFunnel[T](fnl: Funnel[T]): Funnel[Iterable[T]] =

// There is an implicit Option[T] => Iterable[T]
def iterableFunnel[T, C[_]](fnl: Funnel[T])(implicit ti: C[T] => Iterable[T]): Funnel[C[T]] =
funnel { (sink, xs) =>
var i = 0
xs.foreach { x =>
ti(xs).foreach { x =>
fnl.funnel(x, sink)
i += 1
}
Expand Down

0 comments on commit 3a277b4

Please sign in to comment.