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

Add Gen to split seq into n partitions #38

Open
japgolly opened this issue Nov 4, 2017 · 1 comment
Open

Add Gen to split seq into n partitions #38

japgolly opened this issue Nov 4, 2017 · 1 comment

Comments

@japgolly
Copy link
Owner

japgolly commented Nov 4, 2017

No description provided.

@japgolly
Copy link
Owner Author

japgolly commented Nov 5, 2017

Ahh now two similar things outside of Nyaya

// HashLogicTest.scala
    def sizedGroups[C[X] <: SeqLike[X, C[X]], A](input: C[A])(implicit ss: SizeSpec): Gen[Vector[C[A]]] = { // TODO Add to Nyaya
      val size = ss match {
        case x@ SizeSpec.Default => x.gen
        case x: SizeSpec.Exactly => x.gen
        case SizeSpec.OneOf(ns)  => Gen.chooseIndexed_!(ns)
      }
      Gen { ctx =>
        val groups = Vector.newBuilder[C[A]]
        @tailrec def go(as: C[A]): Unit =
          if (as.nonEmpty) {
            val n = size.run(ctx)
            groups += as.take(n)
            go(as.drop(n))
          }
        go(input)
        groups.result()
      }
    }
// ProjectStateTest.scala
  // TODO Move into Nyaya
  def Gen_batches[A](as: Vector[A], partitionSize: Range.Inclusive, keepRemainder: Boolean = true): Gen[Vector[Vector[A]]] = {
    val genSize = Gen.chooseInt(partitionSize.min, partitionSize.max)
    Gen { ctx =>
      val b = Vector.newBuilder[Vector[A]]
      @tailrec def go(rem: Vector[A]): Unit =
        if (rem.isEmpty)
          ()
        else if (rem.length >= partitionSize.min) {
          val n = genSize.run(ctx)
          b += rem.take(n)
          go(rem.drop(n))
        } else if (keepRemainder)
          b += rem
      go(as)
      b.result()
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant