当前位置: 首页>>代码示例>>Scala>>正文


Scala Builder类代码示例

本文整理汇总了Scala中scala.collection.mutable.Builder的典型用法代码示例。如果您正苦于以下问题:Scala Builder类的具体用法?Scala Builder怎么用?Scala Builder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Builder类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: 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

示例2: Histogram

//设置package包名称以及导入依赖的类
package com.olegych.scastie
package balancer
package utils

import System.{lineSeparator => nl}

import scala.collection.generic._
import scala.collection.mutable.Builder

object Histogram {
  implicit def canBuildFrom[A]: CanBuildFrom[Seq[A], A, Histogram[A]] = {
    def builder = Seq.newBuilder[A].mapResult(seq => Histogram.fromSeq(seq))
    new CanBuildFrom[Seq[A], A, Histogram[A]] {
      def apply(): Builder[A, Histogram[A]] = builder
      def apply(from: Seq[A]) = builder
    }
  }
  def empty[T]: Histogram[T] = new Histogram[T](Map())
  def fromSeq[T](xs: Seq[T]): Histogram[T] = {
    val data =
      xs.groupBy(x => x).mapValues(v => (v.size.toDouble / xs.size.toDouble))
    new Histogram(data)
  }
  def apply[T](xs: T*): Histogram[T] = fromSeq(xs)
}

class Histogram[T](protected val data: Map[T, Double]) {
  def distance(other: Histogram[T]): Double = {
    val m1 = data
    val m2 = other.data

    val km1 = m1.keySet
    val km2 = m2.keySet

    val missingM1 = (km2 -- km1).map(k => m2(k)).sum
    val missingM2 = (km1 -- km2).map(k => m1(k)).sum
    val intersect = (km1.intersect(km2)).map(k => Math.abs(m1(k) - m2(k))).sum

    missingM1 + missingM2 + intersect
  }
} 
开发者ID:scalacenter,项目名称:scastie,代码行数:42,代码来源:Histogram.scala

示例3: IncrementalSeq

//设置package包名称以及导入依赖的类
package inox.utils

import scala.collection.mutable.Builder
import scala.collection.mutable.ArrayBuffer
import scala.collection.{Iterable, IterableLike}

class IncrementalSeq[A] extends IncrementalState
                        with Iterable[A]
                        with IterableLike[A, Seq[A]]
                        with Builder[A, IncrementalSeq[A]] {

  private[this] var stack: List[ArrayBuffer[A]] = List(new ArrayBuffer())

  def clear() : Unit = {
    stack = List(new ArrayBuffer())
  }

  def reset(): Unit = {
    clear()
  }

  def push(): Unit = {
    stack ::= stack.head.clone
  }

  def pop(): Unit = {
    stack = stack.tail
  }

  def iterator = stack.head.toList.iterator
  def +=(e: A) = { stack.head += e; this }
  def -=(e: A) = { stack.head -= e; this }

  override def newBuilder = new scala.collection.mutable.ArrayBuffer()
  def result = this
} 
开发者ID:epfl-lara,项目名称:inox,代码行数:37,代码来源:IncrementalSeq.scala


注:本文中的scala.collection.mutable.Builder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。