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

Optimize sized.ToHList and ToSizedHList #1182

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions core/src/main/scala/shapeless/ops/sizeds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package shapeless
package ops

import shapeless.ops.hlist.Fill

object sized {
/**
* Type class supporting conversion of this `Sized` to an `HList` whose elements have the same type as in `Repr`.
Expand All @@ -29,19 +31,34 @@ object sized {
}

object ToHList {
type Aux[Repr, L <: Nat, Out0 <: HList] = ToHList[Repr, L] { type Out = Out0 }
type Aux[-Repr, L <: Nat, O <: HList] = ToHList[Repr, L] {
type Out = O
}

implicit val emptySizedToHList: Aux[Any, Nat._0, HNil] =
@deprecated("Use instance instead", "2.3.8")
val emptySizedToHList: Aux[Any, Nat._0, HNil] =
new ToHList[Any, Nat._0] {
type Out = HNil
def apply(s: Sized[Any, Nat._0]) = HNil
def apply(s: Sized[Any, Nat._0]): HNil = HNil
}

implicit def nonEmptySizedToHList[Repr, L <: Nat]
(implicit itl: IsRegularIterable[Repr], ev: AdditiveCollection[Repr], ts: ToHList[Repr, L]): Aux[Repr, Succ[L], itl.A :: ts.Out] =
new ToHList[Repr, Succ[L]] {
type Out = itl.A :: ts.Out
def apply(s: Sized[Repr, Succ[L]]) = s.head :: ts(s.tail)
}
@deprecated("Use instance instead", "2.3.8")
def nonEmptySizedToHList[Repr, L <: Nat](
implicit itl: IsRegularIterable[Repr],
ev: AdditiveCollection[Repr],
ts: ToHList[Repr, L]
): Aux[Repr, Succ[L], itl.A :: ts.Out] = new ToHList[Repr, Succ[L]] {
type Out = itl.A :: ts.Out
def apply(s: Sized[Repr, Succ[L]]): Out = s.head :: ts(s.tail)
}

implicit def instance[Repr, T, N <: Nat, O <: HList](
implicit itl: IsRegularIterable[Repr] { type A = T },
fill: Fill.Aux[N, T, O]
): Aux[Repr, N, O] = new ToHList[Repr, N] {
type Out = O
def apply(s: Sized[Repr, N]): O =
itl(s.unsized).foldRight[HList](HNil)(_ :: _).asInstanceOf[O]
}
}
}
14 changes: 6 additions & 8 deletions core/src/main/scala_2.13+/shapeless/ops/traversables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,14 @@ object traversable {
type Out = Out0
}

implicit def instance[CC[T] <: Iterable[T], A, N <: Nat](
implicit def instance[CC[T] <: Iterable[T], A, N <: Nat, O <: HList](
implicit gt: IsRegularIterable[CC[A]],
ac: AdditiveCollection[CC[A]],
ti: ToInt[N],
th: ToHList[CC[A], N]
): Aux[CC, A, N, Option[th.Out]] =
new ToSizedHList[CC, A, N] {
type Out = Option[th.Out]
def apply(as: CC[A]): Out =
as.sized[N].map(_.toHList)
}
th: ToHList.Aux[CC[A], N, O]
): Aux[CC, A, N, Option[O]] = new ToSizedHList[CC, A, N] {
type Out = Option[O]
def apply(as: CC[A]): Out = as.sized[N].map(th.apply)
}
}
}
1 change: 0 additions & 1 deletion core/src/test/scala/shapeless/hlist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package shapeless

import org.junit.Test
import org.junit.Assert._

import test._
import testutil._

Expand Down