Skip to content

Commit

Permalink
Merge pull request #1182 from joroKr21/to-sized-hlist
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 authored May 31, 2021
2 parents 26524dd + 580ef1a commit 81d7052
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
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

0 comments on commit 81d7052

Please sign in to comment.