scala.collection.Factory Scala Examples

The following examples show how to use scala.collection.Factory. 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: Scala213Specific.scala    From nyaya   with GNU Lesser General Public License v2.1 5 votes vote down vote up
package nyaya.gen

import scala.collection.immutable.ArraySeq
import scala.collection.Factory
import scala.reflect.ClassTag

object ScalaVerSpecific {

  trait BaggyImplicits {

    implicit object LazyListBaggy extends Baggy[LazyList] {
      override def empty   [A]                                 = LazyList.empty
      override def contains[A](h: LazyList[A], a: A)           = h contains a
      override def add     [A](h: LazyList[A], a: A)           = a #:: h
      override def append  [A](h: LazyList[A], i: LazyList[A]) = i #::: h
    }

    implicit object ArraySeqBaggy extends Baggy[ArraySeq] {
      override def empty   [A]                                 = ArraySeq.empty
      override def contains[A](h: ArraySeq[A], a: A)           = h contains a
      override def add     [A](h: ArraySeq[A], a: A)           = h :+ a
      override def append  [A](h: ArraySeq[A], i: ArraySeq[A]) = h ++ i
    }
  }

  trait GenClassExt[+A] extends Any { self: Gen[A] =>

    final def to[B](f: Factory[A, B])(implicit ss: SizeSpec): Gen[B] =
      fillSS(ss)(f)

    final def arraySeq[B >: A](implicit ct: ClassTag[B], ss: SizeSpec): Gen[ArraySeq[B]] =
      to(ArraySeq)

    final def arraySeq[B >: A](ss: SizeSpec)(implicit ct: ClassTag[B]): Gen[ArraySeq[B]] =
      to[ArraySeq[B]](ArraySeq)(ss)

    // --------------------------------------------------------------------------------------------

    final def to1[B](f: Factory[A, B])(implicit ss: SizeSpec): Gen[B] =
      fillSS1(ss)(f)

    final def arraySeq1[B >: A](implicit ct: ClassTag[B], ss: SizeSpec): Gen[ArraySeq[B]] =
      to1(ArraySeq)

    final def arraySeq1[B >: A](ss: SizeSpec)(implicit ct: ClassTag[B]): Gen[ArraySeq[B]] =
      to1[ArraySeq[B]](ArraySeq)(ss)

  }
} 
Example 2
Source File: package.scala    From magnolify   with Apache License 2.0 5 votes vote down vote up
package magnolify

import scala.collection.{mutable, Factory}
import scala.util.hashing.MurmurHash3

package object shims {
  trait Monadic[F[_]] extends mercator.Monadic[F] {
    def flatMapS[A, B](from: F[A])(fn: A => F[B]): F[B]
    def mapS[A, B](from: F[A])(fn: A => B): F[B]

    override def flatMap[A, B](from: F[A])(fn: A => F[B]): F[B] = flatMapS(from)(fn)
    override def map[A, B](from: F[A])(fn: A => B): F[B] = mapS(from)(fn)
  }

  trait FactoryCompat[-A, +C] extends Serializable {
    def newBuilder: mutable.Builder[A, C]
    def build(xs: IterableOnce[A]): C = newBuilder.addAll(xs).result()
  }

  object FactoryCompat {
    implicit def fromFactory[A, C](implicit f: Factory[A, C]): FactoryCompat[A, C] =
      new FactoryCompat[A, C] {
        override def newBuilder: mutable.Builder[A, C] = f.newBuilder
      }
  }

  object SerializableCanBuildFroms

  val JavaConverters = scala.jdk.CollectionConverters

  object MurmurHash3Compat {
    def seed(data: Int): Int = MurmurHash3.mix(MurmurHash3.productSeed, data)
  }
} 
Example 3
Source File: compat.scala    From mercator   with Apache License 2.0 5 votes vote down vote up
package mercator

import scala.collection.Factory
import language.higherKinds

final class CollOps[M[_], Coll[T] <: IterableOnce[T], A](val value: Coll[M[A]]) extends AnyVal {
  @inline def sequence(implicit monadic: Monadic[M], factory: Factory[A, Coll[A]]): M[Coll[A]] =
    value.iterator.foldLeft(monadic.point(List[A]()): M[List[A]]) { (acc, next) =>
      acc.flatMap { xs => next.map(_ :: xs) }
    }.map { xs => factory.fromSpecific(xs.reverseIterator) }
}

final class TraversableOps[Coll[T] <: IterableOnce[T], A](val value: Coll[A]) extends AnyVal {
  @inline def traverse[B, M[_]](fn: A => M[B])(implicit monadic: Monadic[M], factory: Factory[B, Coll[B]]): M[Coll[B]] =
    value.iterator.foldLeft(monadic.point(List[B]())) { (acc, next) =>
      acc.flatMap { xs => fn(next).map(_ :: xs) }
    }.map { xs => factory.fromSpecific(xs.reverseIterator) }
} 
Example 4
Source File: package.scala    From shapeless-datatype   with Apache License 2.0 5 votes vote down vote up
package shapeless.datatype

import scala.collection.{mutable, Factory}

package object shims {
  // From https://github.com/pureconfig/pureconfig
  trait FactoryCompat[-A, +C] extends Serializable {
    def newBuilder: mutable.Builder[A, C]
    def build(xs: IterableOnce[A]): C = newBuilder.addAll(xs).result()
  }

  object FactoryCompat {
    implicit def fromFactory[A, C](implicit f: Factory[A, C]): FactoryCompat[A, C] =
      new FactoryCompat[A, C] {
        override def newBuilder: mutable.Builder[A, C] = f.newBuilder
      }
  }

  val JavaConverters = scala.jdk.CollectionConverters
} 
Example 5
Source File: package.scala    From diffx   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill
import acyclic.skipped

import scala.annotation.compileTimeOnly
import scala.collection.Factory
import com.softwaremill.diffx.DiffxSupport._

package object diffx extends DiffxSupport {
  implicit def traversableDiffxFunctor[F[_], A](implicit
      fac: Factory[A, F[A]],
      ev: F[A] => Iterable[A]
  ): DiffxFunctor[F, A] =
    new DiffxFunctor[F, A] {}

  implicit class DiffxEachMap[F[_, _], K, T](t: F[K, T])(implicit fac: Factory[(K, T), F[K, T]]) {
    @compileTimeOnly(canOnlyBeUsedInsideIgnore("each"))
    def each: T = sys.error("")
  }
} 
Example 6
Source File: XCompat.scala    From boopickle   with Apache License 2.0 5 votes vote down vote up
package boopickle

import boopickle.Constants.NullRef
import scala.collection.Factory
import scala.language.higherKinds

trait XCompatImplicitPicklers {
  this: PicklerHelper =>

  implicit def mapPickler[T: P, S: P, V[_, _] <: scala.collection.Map[_, _]](
      implicit cbf: Factory[(T, S), V[T, S]]): P[V[T, S]] = BasicPicklers.MapPickler[T, S, V]
  implicit def iterablePickler[T: P, V[_] <: Iterable[_]](implicit cbf: Factory[T, V[T]]): P[V[T]] =
    BasicPicklers.IterablePickler[T, V]
}

trait XCompatPicklers {
  this: PicklerHelper =>

  
  def MapPickler[T: P, S: P, V[_, _] <: scala.collection.Map[_, _]](implicit cbf: Factory[(T, S), V[T, S]]): P[V[T, S]] =
    new P[V[T, S]] {
      override def pickle(map: V[T, S])(implicit state: PickleState): Unit = {
        if (map == null) {
          state.enc.writeInt(NullRef)
        } else {
          // encode length
          state.enc.writeInt(map.size)
          // encode contents as a sequence
          val kPickler = implicitly[P[T]]
          val vPickler = implicitly[P[S]]
          map.asInstanceOf[scala.collection.Map[T, S]].foreach { kv =>
            kPickler.pickle(kv._1)(state)
            vPickler.pickle(kv._2)(state)
          }
        }
      }

      override def unpickle(implicit state: UnpickleState): V[T, S] = {
        state.dec.readInt match {
          case NullRef =>
            null.asInstanceOf[V[T, S]]
          case 0 =>
            // empty map
            val res = cbf.newBuilder.result
            res
          case idx if idx < 0 =>
            state.identityFor[V[T, S]](-idx)
          case len =>
            val b = cbf.newBuilder
            b.sizeHint(len)
            val kPickler = implicitly[P[T]]
            val vPickler = implicitly[P[S]]
            var i        = 0
            while (i < len) {
              b += kPickler.unpickle(state) -> vPickler.unpickle(state)
              i += 1
            }
            val res = b.result
            res
        }
      }
    }
} 
Example 7
Source File: ModifyMacroSupport.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.internal

import scala.annotation.compileTimeOnly
import scala.collection.Factory

trait ModifyMacroSupport extends ModifyMacroFunctorSupport {
  implicit def traversableModifyFunctor[F[_], A](
                                                  implicit fac: Factory[A, F[A]],
                                                  ev: F[A] => Iterable[A]
                                                ): ModifyFunctor[F, A] =
    new ModifyFunctor[F, A] {}

  implicit class ModifyEachMap[F[_, _], K, T](t: F[K, T])(implicit fac: Factory[(K, T), F[K, T]]) {
    @compileTimeOnly(canOnlyBeUsedInsideModify("each"))
    def each: T = sys.error("")
  }
}