scala.collection.GenTraversable Scala Examples
The following examples show how to use scala.collection.GenTraversable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: CollectingInstances.scala From kontextfrei with Apache License 2.0 | 5 votes |
package com.danielwestheide.kontextfrei.scalatest import com.danielwestheide.kontextfrei.DCollectionOps import org.scalatest.enablers.Collecting import scala.collection.GenTraversable import scala.reflect.ClassTag trait CollectingInstances { implicit def collectingDCollection[A: ClassTag, DCollection[_]]( implicit ops: DCollectionOps[DCollection]) : Collecting[A, DCollection[A]] = new Collecting[A, DCollection[A]] { override def loneElementOf(collection: DCollection[A]): Option[A] = { val as = ops.collectAsArray(collection) if (as.length == 1) Some(as(0)) else None } override def sizeOf(collection: DCollection[A]): Int = ops.count(collection).toInt override def genTraversableFrom( collection: DCollection[A]): GenTraversable[A] = ops.collectAsArray(collection) } } object CollectingInstances extends CollectingInstances
Example 2
Source File: QueryParams.scala From finagle-postgres with Apache License 2.0 | 5 votes |
package com.twitter.finagle.postgres.generic import scala.annotation.implicitNotFound import scala.collection.GenTraversable import com.twitter.finagle.postgres.Param import com.twitter.finagle.postgres.values.ValueEncoder import shapeless.ops.hlist.{Length, LiftAll, Mapper, ToList, ToTraversable, Tupler, Unifier, Zip} import shapeless.ops.nat.ToInt import shapeless.ops.record.Keys import shapeless.{Generic, HList, HNil, LUBConstraint, LabelledGeneric, Nat, Poly1} @implicitNotFound( """Could not represent the given value(s) of type ${T} as query parameters. The value must either be a scalar with a ValueEncoder instance, a Seq whose type parameter has a ValueEncoder instance, or a homogeneous tuple whose type has a ValueEncoder instance.""" ) trait QueryParams[T] { def apply(t: T): Seq[Param[_]] def placeholders(t: T, start: Int): Seq[String] } object QueryParams extends QueryParams0 { implicit def seq[F[A] <: Seq[A], T](implicit encoder: ValueEncoder[T]): QueryParams[F[T]] = new QueryParams[F[T]] { @inline final def apply(ts: F[T]): Seq[Param[_]] = ts.map(t => Param(t)) @inline final def placeholders(ts: F[T], start: Int) = (start until (start + ts.length)).map(i => s"$$$i") } } trait QueryParams0 extends QueryParams1 { self: QueryParams.type => implicit def tuple[A <: Product, L <: HList, P <: HList, N <: Nat](implicit gen: Generic.Aux[A, L], length: Length.Aux[L, N], tupler: Tupler.Aux[L, A], toInt: ToInt[N], mapper: Mapper.Aux[toParam.type, L, P], toTraversable: ToTraversable.Aux[P, Seq, Param[_]] ): QueryParams[A] = new QueryParams[A] { @inline final def apply(a: A): Seq[Param[_]] = toTraversable(mapper(gen.to(a))) @inline final def placeholders(a: A, start: Int) = (start until (start + toInt())).map(i => s"$$$i") } } trait QueryParams1 { self: QueryParams.type => implicit def single[T](implicit encoder: ValueEncoder[T]): QueryParams[T] = new QueryParams[T] { def apply(t: T): Seq[Param[T]] = Seq(Param(t)) def placeholders(t: T, start: Int) = Seq(s"$$$start") } }
Example 3
Source File: VecMapSpec.scala From chinese-restaurant-process with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.stats.tables.clustering import com.monsanto.stats.tables.UnitSpec import org.scalatest._ import enablers.Collecting import scala.collection.GenTraversable import prop.GeneratorDrivenPropertyChecks import org.scalactic.Equality import org.scalactic.anyvals.{ PosZInt, PosZDouble } import org.scalacheck.{Arbitrary, Gen} import org.scalacheck.Gen._ import scala.collection.mutable import com.monsanto.stats.tables._ class VecMapSpec extends UnitSpec { val allTopicVectorResults: Vector[TopicVectorInput] = MnMGen.getData() val p5 = ModelParams(5, 1, 1) val crp = new CRP(p5, allTopicVectorResults) "A VecMap" should { "offer a toMap method that returns a Map equal to the Map passed to its factory method" in { crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9)).toMap shouldEqual Map(1 -> 1, 2 -> 4, 3 -> 9) } "be equal to another VecMap created with an equal Map" in { crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9)) shouldEqual crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9)) } "offer a size method that returns the size of the initializing Map" in { crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9, 5 -> 25)).size shouldEqual 4 crp.VecMap(Map.empty).size shouldEqual 0 crp.VecMap(Map(10 -> 9)).size shouldEqual 1 } "offer a + method that combines sums (does Matrix addition on this one-row matrix)" in { val vecMap = crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9, 5 -> 25)) vecMap + crp.VecMap(Map.empty) shouldEqual vecMap crp.VecMap(Map.empty) + vecMap shouldEqual vecMap vecMap + vecMap shouldEqual crp.VecMap(Map(1 -> 2, 2 -> 8, 3 -> 18, 5 -> 50)) crp.VecMap(Map.empty) + crp.VecMap(Map.empty) shouldEqual crp.VecMap(Map.empty) vecMap + crp.VecMap(Map(77 -> 77, 88 -> 88, 99 -> 99)) shouldEqual crp.VecMap(Map(1 -> 1, 2 -> 4, 3 -> 9, 5 -> 25, 77 -> 77, 88 -> 88, 99 -> 99)) vecMap + crp.VecMap(Map(77 -> 77, 88 -> 88, 99 -> 99)) shouldEqual crp.VecMap(Map(2 -> 4, 3 -> 9, 5 -> 25, 77 -> 77, 88 -> 88, 99 -> 99, 1 -> 1)) } } }
Example 4
Source File: RestaurantSpec.scala From chinese-restaurant-process with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.stats.tables.clustering import com.monsanto.stats.tables.UnitSpec import org.scalatest._ import Inspectors._ import enablers.Collecting import scala.collection.GenTraversable import com.monsanto.stats.tables._ class RestaurantSpec extends UnitSpec { val allTopicVectorResults: Vector[TopicVectorInput] = MnMGen.getData() val p5 = ModelParams(5, 1, 1) val crp = new CRP(p5, allTopicVectorResults) val allTopicVectors = allTopicVectorResults.map(crp.TopicVector.from(_)) "The Restaurant companion" should { "offer a factory method that computes the assignments" in { val r = crp.Restaurant(crp.initializeClusters()) for (topicVectorIdx <- 0 until allTopicVectors.size) { val clusterIdx = r.assignment(topicVectorIdx) r.clusters(clusterIdx).topicVectors(0) shouldEqual allTopicVectors(topicVectorIdx) } } "offer a logPosteriorProbability method that computes same value as an alternative version" in { val allTopicVectorResults: Vector[TopicVectorInput] = MnMGen.getData() val p5 = ModelParams(5, 1, 1) val crp = new CRP(p5, allTopicVectorResults) val allTopicVectors: Vector[crp.TopicVector] = allTopicVectorResults.map(crp.TopicVector.from(_)) val clusters: Vector[crp.Cluster] = crp.initializeClusters(true) def dataLikelihoodIgnoringClusterCount: Double = { clusters.map(_.cValue).sum } def clusterSizeHistogram: scala.collection.Map[Int, Int] = { clusters.groupBy(_.size).mapValues(_.size).toMap } // CountOfClusters * ( lnGamma(beta) * vocabSize - lnGamma(beta * vocabSize) ) def clusterCountPart(vocabSize: Int, beta: Double): Double = clusters.size * (crp.logGamma(beta) * vocabSize - crp.logGamma(beta * vocabSize)) def logPosteriorProbability(params: ModelParams): Double = { val dLICC = dataLikelihoodIgnoringClusterCount val ccP = clusterCountPart(params.topicVectorSize, params.beta) val cA = logProbabilityOfClusteringArrangement(clusters.size, params.alpha, clusterSizeHistogram) (dLICC - ccP) + cA } def logProbabilityOfClusteringArrangement( clusterCount: Int, alpha: Double, clusterSizeHistogram: scala.collection.Map[Int, Int] // m ): Double = { // clusterCount * Math.log(alpha) - Sum_i( m_i * ln(i) + logGamma(m_i + 1) ) val sum = clusterSizeHistogram.foldLeft(0.0) { case (sum, (clusterSize, countOfThisSize)) => sum + countOfThisSize * Math.log(clusterSize) + crp.logGamma(countOfThisSize + 1.0) } clusterCount * Math.log(alpha) - sum } val rest = crp.Restaurant(clusters) rest.logPosteriorProbability shouldEqual logPosteriorProbability(p5) } } }
Example 5
Source File: sized.scala From perf_tester with Apache License 2.0 | 5 votes |
package shapeless package syntax import scala.collection.{ GenTraversable, GenTraversableLike } object sized { implicit def genTraversableSizedConv[CC[X] <: GenTraversable[X], T](cc : CC[T]) (implicit conv : CC[T] => GenTraversableLike[T, CC[T]], ev : AdditiveCollection[CC[T]]) = new SizedConv[T, CC[T]](cc) implicit def stringSizedConv(s : String) = new SizedConv[Char, String](s) } final class SizedConv[A, Repr <% GenTraversableLike[A, Repr] : AdditiveCollection](r : Repr) { import ops.nat._ import Sized._ def sized[L <: Nat](implicit toInt : ToInt[L]) = if(r.size == toInt()) Some(wrap[Repr, L](r)) else None def sized(l: Nat)(implicit toInt : ToInt[l.N]) = if(r.size == toInt()) Some(wrap[Repr, l.N](r)) else None def ensureSized[L <: Nat](implicit toInt : ToInt[L]) = { assert(r.size == toInt()) wrap[Repr, L](r) } }
Example 6
Source File: traversables.scala From perf_tester with Apache License 2.0 | 5 votes |
package shapeless package ops import scala.collection.{ GenTraversable, GenTraversableLike } object traversable { object ToSizedHList { def apply[CC[T] <: GenTraversable[T], A, N <: Nat]( implicit ev: ToSizedHList[CC, A, N] ): ToSizedHList.Aux[CC, A, N, ev.Out] = ev import syntax.sized._ import ops.nat._ import ops.sized._ type Aux[CC[T] <: GenTraversable[T], A, N <: Nat, Out0] = ToSizedHList[CC, A, N] { type Out = Out0 } implicit def instance[CC[T] <: GenTraversable[T], A, N <: Nat]( implicit gt: CC[A] => GenTraversableLike[A, 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) } } }
Example 7
Source File: StreamDataPipe.scala From DynaML with Apache License 2.0 | 5 votes |
package io.github.mandar2812.dynaml.pipes import scala.collection.GenTraversable //import scalaxy.streams.optimize trait StreamFlatMapPipe[I, J] extends StreamDataPipe[I, Stream[J], Stream[J]] { override def run(data: Stream[I]) = data.flatMap(pipe) } trait StreamFilterPipe[I] extends StreamDataPipe[I, Boolean, Stream[I]] { override def run(data: Stream[I]): Stream[I] = data.filter(pipe) } trait StreamPartitionPipe[I] extends StreamDataPipe[I, Boolean, (Stream[I], Stream[I])] { override def run(data: Stream[I]): (Stream[I], Stream[I]) = data.partition(pipe) } trait StreamSideEffectPipe[I] extends StreamDataPipe[I, Unit, Unit] { override def run(data: Stream[I]): Unit = data.foreach(pipe) } object StreamDataPipe { def toStreamPipe[I, S <: GenTraversable[I]] = new DataPipe[S, Stream[I]] { override def run(data: S) = data.toStream } //Stream pipes which map from the original domain to a new one def apply[I, J](mapFunc: (I) => J): StreamMapPipe[I, J] = new StreamMapPipe[I, J] { val pipe = mapFunc } def apply[I, J](map: DataPipe[I, J]): StreamMapPipe[I, J] = new StreamMapPipe[I, J] { val pipe = map.run _ } //Stream pipes which act as filters def apply[I](mapFunc: (I) => Boolean): StreamFilterPipe[I] = new StreamFilterPipe[I] { val pipe = mapFunc } def apply[I](mapFunc: DataPipe[I, Boolean]): StreamFilterPipe[I] = new StreamFilterPipe[I] { val pipe = mapFunc.run _ } //stream pipes with side effects def apply[I](seFunc: (I) => Unit): StreamSideEffectPipe[I] = new StreamSideEffectPipe[I] { val pipe = seFunc } def apply[I](seFunc: SideEffectPipe[I]): StreamSideEffectPipe[I] = new StreamSideEffectPipe[I] { val pipe = seFunc.run _ } } object StreamFlatMapPipe { def apply[I, J](mapFunc: (I) => Stream[J]) = new StreamFlatMapPipe[I, J] { override val pipe = mapFunc } def apply[I, J](mapFunc: DataPipe[I, Stream[J]]) = new StreamFlatMapPipe[I, J] { override val pipe = mapFunc.run _ } } object StreamPartitionPipe { def apply[I](mapFunc: (I) => Boolean): StreamPartitionPipe[I] = new StreamPartitionPipe[I] { val pipe = mapFunc } def apply[I](mapFunc: DataPipe[I, Boolean]): StreamPartitionPipe[I] = new StreamPartitionPipe[I] { val pipe = mapFunc.run _ } }
Example 8
Source File: LinkedHashSetSequencing.scala From scredis with Apache License 2.0 | 5 votes |
package scredis.util import org.scalactic.Equality import org.scalatest.enablers.Sequencing import scala.collection.GenTraversable object LinkedHashSetSequencing { implicit def linkedHashSetSequencing[A](implicit equality: Equality[A]): Sequencing[LinkedHashSet[A]] = new Sequencing[LinkedHashSet[A]] { override def containsInOrder(sequence: LinkedHashSet[A], eles: collection.Seq[Any]): Boolean = { val it = eles.iterator for (e <- sequence) { it.takeWhile(_ != e) if (it.hasNext) it.next() else return false } true } override def containsInOrderOnly(sequence: LinkedHashSet[A], eles: collection.Seq[Any]): Boolean = { sequence.toList == eles.toList } override def containsTheSameElementsInOrderAs(leftSequence: LinkedHashSet[A], rightSequence: GenTraversable[Any]): Boolean = leftSequence.toList == rightSequence.toList } }
Example 9
Source File: LinkedHashSetSequencing.scala From scredis with Apache License 2.0 | 5 votes |
package scredis.util import org.scalactic.Equality import org.scalatest.enablers.Sequencing import scala.collection.GenTraversable object LinkedHashSetSequencing { implicit def linkedHashSetSequencing[A](implicit equality: Equality[A]): Sequencing[LinkedHashSet[A]] = new Sequencing[LinkedHashSet[A]] { override def containsInOrder(sequence: LinkedHashSet[A], eles: collection.Seq[Any]): Boolean = { val it = eles.iterator for (e <- sequence) { it.takeWhile(_ != e) if (it.hasNext) it.next() else return false } true } override def containsInOrderOnly(sequence: LinkedHashSet[A], eles: collection.Seq[Any]): Boolean = { sequence.toList == eles.toList } override def containsTheSameElementsInOrderAs(leftSequence: LinkedHashSet[A], rightSequence: GenTraversable[Any]): Boolean = leftSequence.toList == rightSequence.toList } }
Example 10
Source File: ReprFormat.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.data import scodec.bits.{BitVector, ByteVector} import shapeless.{Generic, HList, HNil, Lazy, ::} import scala.collection.GenTraversable trait ReprFormat[T] extends (T => ByteVector) object ReprFormat extends ReprFormat0 { final case class instance[T](fn: T => ByteVector) extends ReprFormat[T] { def apply(t: T): ByteVector = fn(t) } private val one = ByteVector.fromByte(1) private val zero = ByteVector.fromByte(0) implicit val byteFormat: ReprFormat[Byte] = instance(ByteVector.fromByte) implicit val boolFormat: ReprFormat[Boolean] = instance(b => if (b) one else zero) implicit val shortFormat: ReprFormat[Short] = instance(ByteVector.fromShort(_)) implicit val intFormat: ReprFormat[Int] = instance(ByteVector.fromInt(_)) implicit val longFormat: ReprFormat[Long] = instance(ByteVector.fromLong(_)) implicit val floatFormat: ReprFormat[Float] = instance(f => ByteVector.fromInt(java.lang.Float.floatToIntBits(f))) implicit val doubleFormat: ReprFormat[Double] = instance(d => ByteVector.fromLong(java.lang.Double.doubleToLongBits(d))) implicit val stringFormat: ReprFormat[String] = instance(str => ByteVector.encodeUtf8(str).fold(throw _, identity)) implicit val byteArrayFormat: ReprFormat[Array[Byte]] = instance(ByteVector(_)) implicit val byteVectorFormat: ReprFormat[ByteVector] = instance(identity) implicit val bitVectorFormat: ReprFormat[BitVector] = instance(_.toByteVector) implicit def arrayFormat[A](implicit elFormat: ReprFormat[A]): ReprFormat[Array[A]] = instance { arr => arr.foldLeft(ByteVector.fromInt(arr.length)) { (accum: ByteVector, next: A) => accum ++ elFormat(next) } } implicit def traversableFormat[F[X] <: GenTraversable[X], A](implicit elFormat: ReprFormat[A]): ReprFormat[F[A]] = instance { arr => arr.foldLeft(ByteVector.fromInt(arr.size)) { (accum, next) => accum ++ elFormat(next) } } implicit def optionalFormat[A](implicit elFormat: ReprFormat[A]): ReprFormat[Option[A]] = instance { case Some(el) => one ++ elFormat(el) case None => zero } implicit val hnilFormat: ReprFormat[HNil] = instance(_ => ByteVector.empty) implicit def hlistFormat[H, T <: HList](implicit formatH: ReprFormat[H], formatT: ReprFormat[T]): ReprFormat[H :: T] = instance { case h :: t => formatH(h) ++ formatT(t) } } private[data] trait ReprFormat0 { self: ReprFormat.type => implicit def struct[A, L <: HList](implicit gen: Generic.Aux[A, L], formatL: Lazy[ReprFormat[L]]): ReprFormat[A] = instance(a => formatL.value(gen.to(a))) }