Skip to content

Commit

Permalink
support generic sequence to be order insenstive.
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Sep 6, 2017
1 parent 5dd5d71 commit 98b2e50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/src/main/scala/cats/sequence/sequence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package cats.sequence

import shapeless._
import cats.{Functor, Apply}
import shapeless.ops.hlist.ZipWithKeys
import cats.{Apply, Functor}
import shapeless.ops.hlist.{Align, ZipWithKeys}
import shapeless.ops.record.{Keys, Values}

import scala.annotation.implicitNotFound
Expand Down Expand Up @@ -95,18 +95,19 @@ object GenericSequencer extends MkGenericSequencer {

trait MkGenericSequencer {

implicit def mkGenericSequencer[L <: HList, T, SOut <: HList, FOut, F[_]]
implicit def mkGenericSequencer[L <: HList, T, SOut <: HList, FOut, LOut <: HList, F[_]]
( implicit
rs: RecordSequencer.Aux[L, FOut],
gen: LabelledGeneric.Aux[T, SOut],
gen: LabelledGeneric.Aux[T, LOut],
eqv: FOut =:= F[SOut],
align: Align[SOut, LOut],
F: Functor[F]
): GenericSequencer.Aux[L, T, F[T]] =
new GenericSequencer[L, T] {
type Out = F[T]

def apply(in: L): Out = {
F.map(rs(in))(gen.from)
F.map(rs(in))(l => gen.from(l.align))
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/scala/cats/sequence/sequence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class SequenceTests extends KittensSuite {
}
})

test("sequence gen with different sort")(check {
forAll { (x: Option[Int], y: Option[String], z: Option[Float]) =>
val myGen = sequenceGeneric[MyCase]
val expected = (x, y, z) mapN MyCase.apply

myGen(b = y, a = x, c = z) == expected
}
})

test("sequence gen for Either")(check {
forAll { (x: Either[String, Int], y: Either[String, String], z: Either[String, Float]) =>
val myGen = sequenceGeneric[MyCase]
Expand Down

0 comments on commit 98b2e50

Please sign in to comment.