Skip to content

Commit

Permalink
Merge pull request #1184 from joroKr21/to-sized-hlist
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 authored Jun 1, 2021
2 parents 3c2645c + 06b7f94 commit 6154595
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
26 changes: 13 additions & 13 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,17 @@ object sized {
}

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

implicit val emptySizedToHList: Aux[Any, Nat._0, HNil] =
new ToHList[Any, Nat._0] {
type Out = HNil
def apply(s: Sized[Any, Nat._0]) = HNil
}
type Aux[-Repr, L <: Nat, O <: HList] = ToHList[Repr, L] {
type Out = O
}

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)
}
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]
}
}
}
20 changes: 8 additions & 12 deletions core/src/main/scala/shapeless/ops/traversables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,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)
}

ac: AdditiveCollection[CC[A]],
ti: ToInt[N],
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)
}
}

}

0 comments on commit 6154595

Please sign in to comment.