本文整理汇总了Scala中scala.collection.generic.CanBuildFrom类的典型用法代码示例。如果您正苦于以下问题:Scala CanBuildFrom类的具体用法?Scala CanBuildFrom怎么用?Scala CanBuildFrom使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CanBuildFrom类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: apply
//设置package包名称以及导入依赖的类
package org.hammerlab.genomics.bases
import org.hammerlab.genomics.bases.Base._
import org.hammerlab.genomics.bases.Bases.Bytes
import scala.collection.generic.CanBuildFrom
import scala.collection.{ IndexedSeqLike, mutable }
def apply(basesStr: String): Bases = Bases(basesStr.iterator.map(_.toUpper.toByte: Base).toVector)
def iteratorOrdering[T](implicit ord: Ordering[T]) =
new Ordering[Iterator[T]] {
override def compare(x: Iterator[T], y: Iterator[T]): Int = {
(x.hasNext, y.hasNext) match {
case (false, false) ? 0
case (true, true) ?
ord.compare(x.next(), y.next()) match {
case 0 ? compare(x, y)
case v ? v
}
case (false, true) ? -1
case (true, false) ? 1
}
}
}
implicit object BasesOrdering extends Ordering[Bases] {
val ord = iteratorOrdering[Base]
override def compare(x: Bases, y: Bases): Int =
ord.compare(x.bytes.iterator, y.bytes.iterator)
}
def newBuilder: mutable.Builder[Base, Bases] = Vector.newBuilder[Base].mapResult(Bases(_))
implicit def canBuildFromBases: CanBuildFrom[Bases, Base, Bases] =
new CanBuildFrom[Bases, Base, Bases] {
override def apply(from: Bases): mutable.Builder[Base, Bases] = newBuilder
override def apply(): mutable.Builder[Base, Bases] = newBuilder
}
implicit val canBuildFromSeqs =
new CanBuildFrom[Iterable[_], Base, Bases] {
override def apply(from: Iterable[_]): mutable.Builder[Base, Bases] = Bases.newBuilder
override def apply(): mutable.Builder[Base, Bases] = Bases.newBuilder
}
implicit val canBuildFromArrays =
new CanBuildFrom[Array[_], Base, Bases] {
override def apply(from: Array[_]): mutable.Builder[Base, Bases] = Bases.newBuilder
override def apply(): mutable.Builder[Base, Bases] = Bases.newBuilder
}
}
示例2: option
//设置package包名称以及导入依赖的类
package sclib.ops
import scala.collection.generic.CanBuildFrom
object option extends option
def sequence(implicit cbf: CanBuildFrom[Nothing, A, CC[A]]): Option[CC[A]] = {
val b = {
val builder = collection.breakOut[CC[Option[A]], A, CC[A]]
builder(ts)
}
b.sizeHint(ts)
def go(xs: Traversable[Option[A]]): Option[CC[A]] = xs.headOption match {
case Some(None) => None
case Some(Some(x)) =>
b += x
go(xs.tail)
case None => b.result.some
}
go(ts)
}
}
}
示例3: RichTraversableLike
//设置package包名称以及导入依赖的类
package mesosphere.marathon
package stream
import scala.collection.TraversableLike
import scala.collection.generic.CanBuildFrom
class RichTraversableLike[+A, +Repr](to: TraversableLike[A, Repr]) {
private def filterAsImpl[B >: A, That](p: A => Boolean, isFlipped: Boolean)(implicit cbf: CanBuildFrom[Repr, B, That]): That = {
val b = cbf(to.repr)
to.foreach { x =>
if (p(x) != isFlipped) b += x
}
b.result
}
def filterAs[B >: A, That](p: A => Boolean)(implicit cbf: CanBuildFrom[Repr, B, That]): That =
filterAsImpl(p, isFlipped = false)
def filterNotAs[B >: A, That](p: A => Boolean)(implicit cbf: CanBuildFrom[Repr, B, That]): That =
filterAsImpl(p, isFlipped = true)
}
示例4: ConcurrentSet
//设置package包名称以及导入依赖的类
package mesosphere.util
import scala.collection.concurrent.TrieMap
import scala.collection.generic.{ CanBuildFrom, GenericSetTemplate, MutableSetFactory }
import scala.collection.mutable
final class ConcurrentSet[A](elems: A*)
extends mutable.Set[A]
with GenericSetTemplate[A, ConcurrentSet]
with mutable.SetLike[A, ConcurrentSet[A]]
with mutable.FlatHashTable[A]
with Serializable {
import ConcurrentSet._
private[this] val underlying = TrieMap[A, AnyRef](elems.map(_ -> Dummy): _*)
override def +=(elem: A): this.type = {
underlying.putIfAbsent(elem, Dummy)
this
}
override def -=(elem: A): this.type = {
underlying.remove(elem)
this
}
override def contains(elem: A): Boolean = underlying.contains(elem)
override def iterator: Iterator[A] = underlying.keysIterator
override def companion: MutableSetFactory[ConcurrentSet] = ConcurrentSet
}
object ConcurrentSet extends MutableSetFactory[ConcurrentSet] {
private[ConcurrentSet] val Dummy = new AnyRef
override def apply[A](elems: A*): ConcurrentSet[A] = new ConcurrentSet[A](elems: _*)
override def empty[A]: ConcurrentSet[A] = new ConcurrentSet[A]
override def newBuilder[A]: mutable.Builder[A, ConcurrentSet[A]] =
new mutable.SetBuilder[A, ConcurrentSet[A]](new ConcurrentSet[A]())
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, ConcurrentSet[A]] =
setCanBuildFrom[A]
override def setCanBuildFrom[A]: CanBuildFrom[ConcurrentSet[_], A, ConcurrentSet[A]] =
new CanBuildFrom[ConcurrentSet[_], A, ConcurrentSet[A]] {
override def apply(from: ConcurrentSet[_]): mutable.Builder[A, ConcurrentSet[A]] = newBuilder[A]
override def apply(): mutable.Builder[A, ConcurrentSet[A]] = newBuilder[A]
}
}
示例5: TraversableOps
//设置package包名称以及导入依赖的类
package com.github.stonexx.scala.util
import scala.collection.{GenTraversableLike, immutable, mutable}
import scala.collection.generic.CanBuildFrom
final class TraversableOps[A, Repr](val self: GenTraversableLike[A, Repr]) extends AnyVal {
def groupByOrdered[K](f: A => K)(implicit cbf: CanBuildFrom[Repr, A, Repr]): immutable.ListMap[K, Repr] = {
val m = mutable.LinkedHashMap.empty[K, mutable.Builder[A, Repr]]
for (elem <- self) m.getOrElseUpdate(f(elem), cbf()) += elem
val b = immutable.ListMap.newBuilder[K, Repr]
for ((k, v) <- m) b += k -> v.result()
b.result()
}
def groupConsecutiveKeys[K](f: A => K): Seq[(K, Seq[A])] = (self :\ List.empty[(K, List[A])]) {
(item: A, res: List[(K, List[A])]) =>
res match {
case Nil => List((f(item), List(item)))
case (k, klist) :: tail if k == f(item) => (k, item :: klist) :: tail
case _ => (f(item), List(item)) :: res
}
}
def toSampleString(besides: String = "...(", postfix: String = ")", default: String = ""): String = self.headOption match {
case Some(head) if self.size == 1 => head.toString
case Some(head) => s"$head$besides${self.size - 1}$postfix"
case None => default
}
@inline def toSampleString: String = toSampleString()
}
trait ToTraversableOps {
implicit def toTraversableOps[A, Repr, CC](x: CC)(implicit ev: CC => GenTraversableLike[A, Repr]): TraversableOps[A, Repr] = new TraversableOps(x)
}
示例6: RichSeq
//设置package包名称以及导入依赖的类
package swave.core.util
import scala.annotation.tailrec
import scala.collection.LinearSeq
import scala.collection.generic.CanBuildFrom
import swave.core.macros._
final class RichSeq[A](val underlying: Seq[A]) extends AnyVal {
def mapFind[B](f: A ? Option[B]): Option[B] = {
@tailrec def linearRec(seq: LinearSeq[A]): Option[B] =
if (seq.nonEmpty) {
val x = f(seq.head)
if (x.isEmpty) linearRec(seq.tail) else x
} else None
@tailrec def indexedRec(ix: Int): Option[B] =
if (ix < underlying.length) {
val x = f(underlying(ix))
if (x.isEmpty) indexedRec(ix + 1) else x
} else None
underlying match {
case x: LinearSeq[A] ? linearRec(x)
case _ ? indexedRec(0)
}
}
def foreachWithIndex(f: (A, Int) ? Unit): Unit = {
requireState(underlying.isInstanceOf[IndexedSeq[A]])
@tailrec def rec(ix: Int): Unit =
if (ix < underlying.size) {
f(underlying(ix), ix)
rec(ix + 1)
}
rec(0)
}
def mapWithIndex[B, That](f: (A, Int) ? B)(implicit bf: CanBuildFrom[Seq[A], B, That]): That = {
requireState(underlying.isInstanceOf[IndexedSeq[A]])
val b = bf(underlying)
b.sizeHint(underlying)
@tailrec def rec(ix: Int): Unit =
if (ix < underlying.size) {
b += f(underlying(ix), ix)
rec(ix + 1)
}
rec(0)
b.result()
}
}
示例7: toIterableSugar
//设置package包名称以及导入依赖的类
package com.github.mosche.sugarz.collection
import com.github.mosche.sugarz.collection.ToIterableSugar.IterableSugar
import scala.collection.IterableLike
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scalaz.Monad
trait ToIterableSugar {
implicit def toIterableSugar[A, Repr <: Iterable[A]](repr: IterableLike[A, Repr]): IterableSugar[A, Repr] = new IterableSugar(repr)
}
object ToIterableSugar extends ToIterableSugar {
class IterableSugar[A, ReprA <: Iterable[A]](val repr: IterableLike[A, ReprA]) extends AnyVal {
private def traverse[I, ReprI, B, ReprB, F[_]](it: IterableLike[I, ReprI], builder: Builder[B, ReprB], f: I ? F[B])(implicit F: Monad[F]): F[Builder[B, ReprB]] = {
it.foldLeft(F.pure(builder)) { (fBuilder, next) =>
F.bind(fBuilder)(builder => F.map(f(next))(builder += _))
}
}
def batchedTraverse[B, ReprB, F[_]](batchSize: Int)(f: A ? F[B])(implicit F: Monad[F], bf: CanBuildFrom[ReprA, B, ReprB]): F[ReprB] = {
val builder = bf()
builder.sizeHint(repr)
val batches = repr.grouped(batchSize)
val fBuilder = batches.foldLeft(F.pure(builder)) { (fBuilder, nextBatch) =>
F.bind(fBuilder)(builder => traverse(nextBatch.map(f), builder, identity[F[B]]))
}
F.map(fBuilder)(_.result())
}
}
}
示例8: readInt
//设置package包名称以及导入依赖的类
package net.iakovlev.easycodecs.decoder
import scala.collection.generic.CanBuildFrom
import scala.language.higherKinds
trait Readers[A] {
implicit def readInt: PrimitivesReader[A, Int]
implicit def readLong: PrimitivesReader[A, Long]
implicit def readFloat: PrimitivesReader[A, Float]
implicit def readDouble: PrimitivesReader[A, Double]
implicit def readBigDecimal: PrimitivesReader[A, BigDecimal]
implicit def readBoolean: PrimitivesReader[A, Boolean]
implicit def readString: PrimitivesReader[A, String]
implicit def readIterable[C[X] <: Iterable[X]](
implicit canBuildFrom: CanBuildFrom[C[A], A, C[A]])
: PrimitivesReader[A, C[A]]
implicit def readMap: PrimitivesReader[A, Map[String, A]]
}
示例9: writeInt
//设置package包名称以及导入依赖的类
package net.iakovlev.easycodecs.encoder
import scala.collection.generic.CanBuildFrom
import scala.language.higherKinds
trait Writers[A] {
implicit def writeInt: PrimitivesWriter[Int, A]
implicit def writeLong: PrimitivesWriter[Long, A]
implicit def writeFloat: PrimitivesWriter[Float, A]
implicit def writeDouble: PrimitivesWriter[Double, A]
implicit def writeBigDecimal: PrimitivesWriter[BigDecimal, A]
implicit def writeBoolean: PrimitivesWriter[Boolean, A]
implicit def writeString: PrimitivesWriter[String, A]
implicit def writeIterable[C[X] <: Iterable[X]](
implicit canBuildFrom: CanBuildFrom[C[A], A, C[A]])
: PrimitivesWriter[C[A], A]
implicit def writeMap: PrimitivesWriter[Map[String, A], A]
}
示例10: shuffle
//设置package包名称以及导入依赖的类
package yuck.core
import scala.language.higherKinds
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.ArrayBuffer
def shuffle
[T, From[X] <: TraversableOnce[X], To[X] <: Seq[X]]
(source: From[T])
(implicit cbf: CanBuildFrom[_, T, To[T]]): To[T] =
{
val buf = new ArrayBuffer[T] ++= source
for (i <- buf.size - 1 to 1 by -1) {
val j = nextInt(i + 1)
val tmp = buf(i)
buf(i) = buf(j)
buf(j) = tmp
}
(cbf() ++= buf).result
}
}
示例11: traverse
//设置package包名称以及导入依赖的类
package com.lambdista
import scala.collection.generic.CanBuildFrom
package object util {
def traverse[A, B, M[X] <: TraversableOnce[X]](
ms: M[Option[A]]
)(f: A => B)(implicit cbf: CanBuildFrom[M[A], B, M[B]]): Option[M[B]] = {
val builder = cbf()
builder.sizeHint(ms.size)
if (ms.exists(_.isEmpty)) None
else {
ms foreach (x => builder += f(x.get))
Some(builder.result)
}
}
def sequence[A, M[X] <: TraversableOnce[X]](ms: M[Option[A]])(
implicit cbf: CanBuildFrom[M[A], A, M[A]]
): Option[M[A]] = traverse(ms)(identity)
}
示例12: decodeUnsafe
//设置package包名称以及导入依赖的类
package com.redbubble.util.json
import java.net.{URI, URL}
import cats.syntax.either._
import io.circe.{Decoder, DecodingFailure}
import scala.collection.generic.CanBuildFrom
trait DecoderOps {
final val uriDecoder: Decoder[URI] = decodeUnsafe[String, URI](s => new URI(s))
final val urlDecoder: Decoder[URL] = decodeUnsafe[String, URL](s => new URL(s))
final val urlSeqDecoder: Decoder[Seq[URL]] = Decoder.instance { c =>
val decoder = failureTolerantContainerDecoder(urlDecoder, Seq.canBuildFrom[URL])
c.as[Seq[URL]](decoder)
}
final def decodeUnsafe[A: Decoder, B](unsafeF: A => B): Decoder[B] = decodeFocusEither[A, B](a => Either.catchNonFatal(unsafeF(a)))
final def decodeFocusEither[A: Decoder, B](f: A => Either[Throwable, B]): Decoder[B] = Decoder.instance { c =>
for {
a <- c.as[A]
b <- f(a).leftMap(e => DecodingFailure(e.getMessage, Nil))
} yield b
}
final def decodeFocusOption[A: Decoder, B](f: A => Option[B]): Decoder[B] = Decoder.instance { c =>
for {
a <- c.as[A]
b <- f(a).toRight(DecodingFailure("Option unexpectedly returned None", c.history))
} yield b
}
final def decodeContainerFocus[A, C[_]](implicit d: Decoder[A], cbf: CanBuildFrom[Nothing, A, C[A]]): Decoder[C[A]] =
Decoder.instance(c => c.as[C[A]](failureTolerantContainerDecoder(d, cbf)))
}
object DecoderOps extends DecoderOps
示例13: Arbitrary
//设置package包名称以及导入依赖的类
package org.broadinstitute.hail.check
import scala.collection.generic.CanBuildFrom
import scala.math.Numeric.Implicits._
import scala.language.higherKinds
object Arbitrary {
def apply[T](arbitrary: Gen[T]): Arbitrary[T] =
new Arbitrary(arbitrary)
implicit def arbBoolean: Arbitrary[Boolean] = new Arbitrary(
Gen.oneOf(true, false))
implicit def arbByte: Arbitrary[Byte] = new Arbitrary(Gen.oneOfGen(Gen.oneOf(Byte.MinValue, -1, 0, 1, Byte.MaxValue),
Gen { p => p.rng.getRandomGenerator.nextInt().toByte }))
implicit def arbInt: Arbitrary[Int] = new Arbitrary(
Gen.oneOfGen(Gen.oneOf(Int.MinValue, -1, 0, 1, Int.MaxValue),
Gen.choose(-100, 100),
Gen { p => p.rng.getRandomGenerator.nextInt() }))
implicit def arbLong: Arbitrary[Long] = new Arbitrary(
Gen.oneOfGen(Gen.oneOf(Long.MinValue, -1L, 0L, 1L, Long.MaxValue),
Gen.choose(-100, 100),
Gen { p => p.rng.getRandomGenerator.nextLong() }))
implicit def arbDouble: Arbitrary[Double] = new Arbitrary(
Gen.oneOfGen(Gen.oneOf(Double.MinValue, -1.0, 0.0, Double.MinPositiveValue, 1.0, Double.MaxValue),
Gen.choose(-100.0, 100.0),
Gen { p => p.rng.nextUniform(Double.MinValue, Double.MaxValue, true) }))
implicit def arbString: Arbitrary[String] = new Arbitrary(Gen.frequency((1, Gen.const("")), (10, Gen { (p: Parameters) =>
val s = p.rng.getRandomGenerator.nextInt(12)
val b = new StringBuilder()
for (i <- 0 until s)
b += Gen.randomOneOf(p.rng, Gen.printableChars)
b.result()
})))
implicit def arbBuildableOf[C[_], T](implicit a: Arbitrary[T], cbf: CanBuildFrom[Nothing, T, C[T]]): Arbitrary[C[T]] =
Arbitrary(Gen.buildableOf(a.arbitrary))
def arbitrary[T](implicit arb: Arbitrary[T]): Gen[T] = arb.arbitrary
}
class Arbitrary[T](val arbitrary: Gen[T])
示例14: par
//设置package包名称以及导入依赖的类
package effectful.augments
import scala.collection.generic.CanBuildFrom
// todo: docs
trait Par[E[_]] {
def par[A,B](ea: =>E[A],eb: =>E[B]) : E[(A,B)]
def par[A,B,C](ea: =>E[A],eb: =>E[B],ec: =>E[C]) : E[(A,B,C)]
def par[A,B,C,D](ea: =>E[A],eb: =>E[B],ec: =>E[C],ed: =>E[D]) : E[(A,B,C,D)]
// todo: gen more
// todo: b/c of free monad interpreter this will prob have to be concrete Seq
def parMap[M[AA] <: Seq[AA],A,B](
items: M[A]
)(
f: A => E[B]
)(implicit
cbf: CanBuildFrom[Nothing,B,M[B]]
) : E[M[B]]
def parFlatMap[M[AA] <: Seq[AA],A,B](
items: M[A]
)(
f: A => E[Traversable[B]]
)(implicit cbf: CanBuildFrom[Nothing,B,M[B]]) : E[M[B]]
def parMapUnordered[M[AA] <: Traversable[AA],A,B](
items: M[A]
)(
f: A => E[B]
)(implicit
cbf: CanBuildFrom[Nothing,B,M[B]]
) : E[M[B]]
def parFlatMapUnordered[M[AA] <: Traversable[AA],A,B](
items: M[A]
)(
f: A => E[Traversable[B]]
)(implicit cbf: CanBuildFrom[Nothing,B,M[B]]) : E[M[B]]
}
示例15: capture_Id
//设置package包名称以及导入依赖的类
import scala.collection.generic.CanBuildFrom
import cats._
import effectful.augments._
package object effectful { //extends CaptureTransform.Ops with Capture.Ops {
// todo: figure out how this sugar is declared in emm
// type |:[F[_],G[_]] = F[G[_]]
// val |: = Nested
implicit object capture_Id extends Capture[Id] {
def capture[A](a: => A) = a
}
implicit def captureTransform_Id[G[_]](implicit
G:Capture[G]
) = new CaptureTransform[Id,G] {
def apply[A](f: => Id[A]) =
G.capture(f)
}
implicit object par_Id extends Par[Id] {
override def par[A, B](ea: => Id[A], eb: => Id[B]): (A, B) =
(ea,eb)
override def par[A, B, C](ea: => Id[A], eb: => Id[B], ec: => Id[C]): (A, B, C) =
(ea,eb,ec)
override def par[A, B, C, D](ea: => Id[A], eb: => Id[B], ec: => Id[C], ed: => Id[D]): (A, B, C, D) =
(ea,eb,ec,ed)
override def parMap[M[AA] <: Seq[AA], A, B](items: M[A])(f: (A) => Id[B])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]): Id[M[B]] =
items.map(f)(scala.collection.breakOut)
override def parFlatMap[M[AA] <: Seq[AA], A, B](items: M[A])(f: (A) => Id[Traversable[B]])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]): Id[M[B]] =
items.flatMap(f)(scala.collection.breakOut)
override def parMapUnordered[M[AA] <: Traversable[AA], A, B](items: M[A])(f: (A) => Id[B])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]): Id[M[B]] =
items.map(f)(scala.collection.breakOut)
override def parFlatMapUnordered[M[AA] <: Traversable[AA], A, B](items: M[A])(f: (A) => Id[Traversable[B]])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]): Id[M[B]] =
items.flatMap(f)(scala.collection.breakOut)
}
implicit object exceptions_Id extends impl.NoCaptureExceptions[Id] {
implicit val E = implicitly[Monad[Id]]
}
implicit object delay_Id extends impl.BlockingDelay[Id] {
override implicit val E = capture_Id
}
implicit class ServicePML[S[_[_]],F[_]](val self: S[F]) extends AnyVal {
def liftService[G[_]](implicit
X:CaptureTransform[F,G],
liftService:LiftService[S]
) : S[G] = liftService(self)
}
}