本文整理汇总了Scala中scala.collection.generic.CanBuildFrom类的典型用法代码示例。如果您正苦于以下问题:Scala CanBuildFrom类的具体用法?Scala CanBuildFrom怎么用?Scala CanBuildFrom使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CanBuildFrom类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
}
开发者ID:hammerlab,项目名称:genomic-utils,代码行数:54,代码来源:Bases.scala
示例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)
}
}
}
开发者ID:j-keck,项目名称:sclib,代码行数:28,代码来源:option.scala
示例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)
}
开发者ID:xiaozai512,项目名称:marathon,代码行数:23,代码来源:RichTraversableLike.scala
示例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]
}
}
开发者ID:xiaozai512,项目名称:marathon,代码行数:52,代码来源:ConcurrentSet.scala
示例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)
}
开发者ID:stonexx,项目名称:utils,代码行数:37,代码来源:TraversableOps.scala
示例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()
}
}
开发者ID:sirthias,项目名称:swave,代码行数:52,代码来源:RichSeq.scala
示例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())
}
}
}
开发者ID:mosche,项目名称:sugarz,代码行数:39,代码来源:ToIterableSugar.scala
示例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]]
}
开发者ID:RomanIakovlev,项目名称:easycodecs,代码行数:19,代码来源:Readers.scala
示例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]
}
开发者ID:RomanIakovlev,项目名称:easycodecs,代码行数:19,代码来源:Writers.scala
示例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
}
}
开发者ID:informarte,项目名称:yuck,代码行数:24,代码来源:RandomGenerator.scala
示例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)
}
开发者ID:lambdista,项目名称:config,代码行数:24,代码来源:package.scala
示例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
开发者ID:redbubble,项目名称:finch-template,代码行数:41,代码来源:DecoderOps.scala
示例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])
开发者ID:Sun-shan,项目名称:Hail_V2,代码行数:47,代码来源:Arbitrary.scala
示例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]]
}
开发者ID:S-Mach,项目名称:effectful,代码行数:42,代码来源:Par.scala
示例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)
}
}
开发者ID:S-Mach,项目名称:effectful,代码行数:60,代码来源:package.scala
示例16: WriterTPar
//设置package包名称以及导入依赖的类
package effectful.examples.adapter.cats.writer
import cats.{Functor, Monoid}
import cats.data.WriterT
import effectful.augments.Par
import scala.collection.generic.CanBuildFrom
class WriterTPar[F[_],L](implicit
P:Par[F],
F:Functor[F],
L:Monoid[L]
) extends Par[({type W[AA] = WriterT[F,L,AA]})#W] {
import cats.syntax.functor._
def par[A, B](ea: => WriterT[F, L, A], eb: => WriterT[F, L, B]) =
WriterT {
P.par(ea.run,eb.run).map { case ((l1,a),(l2,b)) =>
(L.combine(l1,l2),(a,b))
}
}
def par[A, B, C](ea: => WriterT[F, L, A], eb: => WriterT[F, L, B], ec: => WriterT[F, L, C]) = ???
def par[A, B, C, D](ea: => WriterT[F, L, A], eb: => WriterT[F, L, B], ec: => WriterT[F, L, C], ed: => WriterT[F, L, D]) = ???
// todo:
def parMap[M[AA] <: Seq[AA], A, B](items: M[A])(f: (A) => WriterT[F, L, B])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]) = ???
def parFlatMap[M[AA] <: Seq[AA], A, B](items: M[A])(f: (A) => WriterT[F, L, Traversable[B]])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]) = ???
def parMapUnordered[M[AA] <: Traversable[AA], A, B](items: M[A])(f: (A) => WriterT[F, L, B])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]) = ???
def parFlatMapUnordered[M[AA] <: Traversable[AA], A, B](items: M[A])(f: (A) => WriterT[F, L, Traversable[B]])(implicit cbf: CanBuildFrom[Nothing, B, M[B]]) = ???
}
开发者ID:S-Mach,项目名称:effectful,代码行数:36,代码来源:WriterTPar.scala
示例17: Ok
//设置package包名称以及导入依赖的类
package faunadb.importer.lang
import scala.annotation.tailrec
import scala.collection.generic.CanBuildFrom
import scala.collection.{ IterableLike, mutable }
final case class Ok[A](get: A) extends Result[A] {
def error: String = throw new NoSuchElementException("Ok.error")
val isSuccess: Boolean = true
}
final case class Err[A](error: String) extends Result[A] {
def get: A = throw new NoSuchElementException(s"Err($error).get")
val isSuccess: Boolean = false
}
sealed trait Result[+A] {
def get: A
def error: String
val isSuccess: Boolean
@inline def map[B](f: A => B): Result[B] =
if (isSuccess) Ok(f(get)) else asInstanceOf[Result[B]]
@inline def flatMap[B](f: A => Result[B]): Result[B] =
if (isSuccess) f(get) else asInstanceOf[Result[B]]
@inline def fold[B](ifErr: String => B, ifSucc: A => B): B =
if (isSuccess) ifSucc(get) else ifErr(error)
}
object Result {
val unit: Result[Unit] = Ok(())
def apply[A](value: A): Result[A] = Ok(value)
}
trait ShortCircuit {
implicit class SeqResultSC[Repr, A](self: IterableLike[A, Repr]) {
def flatMapS[B, That](f: (A) => Result[B])(implicit cbf: CanBuildFrom[Repr, B, That]): Result[That] = {
@tailrec
def loop0(res: mutable.Builder[B, That], seq: Iterable[A]): Result[That] = {
if (seq.isEmpty) Ok(res.result()) else f(seq.head) match {
case Ok(mapped) => loop0(res += mapped, seq.tail)
case Err(msg) => Err(msg)
}
}
loop0(cbf(self.repr), self.toIterable)
}
def foldLeftS[Z, That](z: => Z)(f: (Z, A) => Result[Z]): Result[Z] = {
@tailrec
def loop0(acc: Z, seq: Iterable[A]): Result[Z] = {
if (seq.isEmpty) Ok(acc) else f(acc, seq.head) match {
case Ok(mapped) => loop0(mapped, seq.tail)
case Err(msg) => Err(msg)
}
}
loop0(z, self.toIterable)
}
}
}
开发者ID:fauna,项目名称:faunadb-importer,代码行数:62,代码来源:Result.scala
示例18: EitherSyntax
//设置package包名称以及导入依赖的类
package arguments
object EitherSyntax {
implicit def toEithersOps[A, B](eithers: Seq[Either[A, B]]): EithersOps[A, B] = new EithersOps(eithers)
implicit def toEitherOps[A, B](either: Either[A, B]): EitherOps[A, B] = new EitherOps(either)
}
class EitherOps[A, B](val either: Either[A, B]) extends AnyVal {
def map[C](f: B => C): Either[A, C] = either match {
case Right(right) => Right(f(right))
case Left(left) => Left(left)
}
def flatMap[C](f: B => Either[A, C]): Either[A, C] = either match {
case Right(right) => f(right)
case Left(left) => Left(left)
}
}
class EithersOps[A, B](val eithers: Seq[Either[A, B]]) extends AnyVal {
import scala.collection.generic.CanBuildFrom
def segregate(
implicit
cbfa: CanBuildFrom[Seq[_], A, Seq[A]],
cbfb: CanBuildFrom[Seq[_], B, Seq[B]]
): (Seq[A], Seq[B]) = {
val aBuilder = cbfa()
val bBuilder = cbfb()
eithers.foreach {
case [email protected](left) => aBuilder += left
case [email protected](right) => bBuilder += right
}
(aBuilder.result(), bBuilder.result())
}
}
开发者ID:esarbe,项目名称:scala-command-line-arguments-parser,代码行数:42,代码来源:EitherSyntax.scala
示例19: par
//设置package包名称以及导入依赖的类
package effectful.augments
import cats._
import scala.collection.generic.CanBuildFrom
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]]
}
object Par {
implicit def apply[F[_],G[_]](implicit
P:Par[F],
F:Monad[F],
G:Monad[G]
) : Par[({ type FG[A] = F[G[A]]})#FG] =
CompositePar[F,G]
}
开发者ID:lancegatlin,项目名称:effectful-demo,代码行数:52,代码来源:Par.scala
示例20: EffectfulOps
//设置package包名称以及导入依赖的类
package effectful.impl
import scala.collection.generic.CanBuildFrom
import cats._
object EffectfulOps {
def sequence[M[_],A,F[AA] <: Traversable[AA]](self: F[M[A]])(implicit
M: Monad[M],
cbf: CanBuildFrom[Nothing, A, F[A]]
) : M[F[A]] = {
import Monad.ops._
self.foldLeft(M.pure(cbf())) { (mBuilder,ea) =>
for {
builder <- mBuilder
a <- ea
} yield builder += a
}.map(_.result())
}
}
开发者ID:lancegatlin,项目名称:effectful-demo,代码行数:21,代码来源:EffectfulOps.scala
注:本文中的scala.collection.generic.CanBuildFrom类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论