scala.collection.LinearSeq Scala Examples

The following examples show how to use scala.collection.LinearSeq. 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: Document.scala    From CSYE7200_Old   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200

import scala.collection.LinearSeq

trait Document[-K, +V] extends (LinearSeq[K] => V) {
  def get(x: LinearSeq[K]): Option[V]

  def get(x: String)(implicit conv: String => K): Option[V] = get(x.split("""\.""").toList map conv)

  def apply(x: LinearSeq[K]): V = get(x).getOrElse(default())

  def default() = throw new NoSuchElementException

  //  def asMap[L <: K](implicit l: L): Map[L,Document[L,V]]
  def add[L <: K, W >: V](l: L, d: Document[L, W]): Document[L, W]

  def pushDown[L <: K](ls: LinearSeq[L]): Map[L, Document[L, V]]
}

case class Leaf[V](value: V) extends Document[Any, V] {
  def get(x: LinearSeq[Any]): Option[V] = x match {
    case Nil => Some(value)
    case _ => None
  }

  def asMap[L <: Any](implicit l: L): Map[L, Document[L, V]] = Map(l -> this)

  def pushDown[L <: Any](ls: LinearSeq[L]): Map[L, Document[L, V]] = ls match {
    case Nil => throw new UnsupportedOperationException("cannot pushDown with empty list")
    case h :: Nil => Map(h -> this)
    case h :: t => Map(h -> Clade(pushDown(t)))
  }

  def add[L <: Any, W >: V](l: L, d: Document[L, W]): Document[L, W] = d match {
    case Clade(x) => val z = pushDown(LinearSeq(l)); val y = z.asInstanceOf[Clade[L, W]].branches; val q = x.asInstanceOf[Map[L, Document[L, W]]]; Clade[L, W](q ++ y)
    case Leaf(_) => throw new UnsupportedOperationException("++ is not supported for two Leaf objects")
  }

  override def toString: String = value.toString
}

case class Clade[K, V](branches: Map[K, Document[K, V]]) extends Document[K, V] {
  def get(x: LinearSeq[K]): Option[V] = x match {
    case h :: t => branches.get(h) flatMap (_.get(t))
    case Nil => None
  }

  //  def asMap[L <: Any](implicit l: L): Map[L,Document[L,V]] = branches.asInstanceOf[Map[L,Document[L,V]]
  def pushDown[L <: K](ls: LinearSeq[L]): Map[L, Document[L, V]] = ls match {
    case Nil => branches.asInstanceOf[Map[L, Document[L, V]]]
    case h :: t => Map(h -> Clade(pushDown(t)))
  }

  def add[L <: K, W >: V](l: L, d: Document[L, W]): Document[L, W] = d match {
    case Clade(x) => val z = pushDown(LinearSeq(l)); val y = z.asInstanceOf[Clade[L, W]].branches; val q = x.asInstanceOf[Map[L, Document[L, W]]]; Clade[L, W](y ++ q)
    case Leaf(_) => d add(l, this.asInstanceOf[Document[L, W]])
  }

  override def toString: String = branches.toString
}

object Document {
  def apply[V](v: V): Document[Any, V] = Leaf(v)

  def apply[K, V](map: Map[K, V]): Document[K, V] = Clade(for ((k, v) <- map) yield k -> apply(v))
} 
Example 2
Source File: Document.scala    From CSYE7200   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200

import scala.collection.LinearSeq

trait Document[-K, +V] extends (LinearSeq[K] => V) {
  def get(x: LinearSeq[K]): Option[V]

  def get(x: String)(implicit conv: String => K): Option[V] = get(x.split("""\.""").toList map conv)

  def apply(x: LinearSeq[K]): V = get(x).getOrElse(default())

  def default() = throw new NoSuchElementException

  //  def asMap[L <: K](implicit l: L): Map[L,Document[L,V]]
  def add[L <: K, W >: V](l: L, d: Document[L, W]): Document[L, W]

  def pushDown[L <: K](ls: LinearSeq[L]): Map[L, Document[L, V]]
}

case class Leaf[V](value: V) extends Document[Any, V] {
  def get(x: LinearSeq[Any]): Option[V] = x match {
    case Nil => Some(value)
    case _ => None
  }

  def asMap[L <: Any](implicit l: L): Map[L, Document[L, V]] = Map(l -> this)

  def pushDown[L <: Any](ls: LinearSeq[L]): Map[L, Document[L, V]] = ls match {
    case Nil => throw new UnsupportedOperationException("cannot pushDown with empty list")
    case h :: Nil => Map(h -> this)
    case h :: t => Map(h -> Clade(pushDown(t)))
  }

  def add[L <: Any, W >: V](l: L, d: Document[L, W]): Document[L, W] = d match {
    case Clade(x) => val z = pushDown(LinearSeq(l)); val y = z.asInstanceOf[Clade[L, W]].branches; val q = x.asInstanceOf[Map[L, Document[L, W]]]; Clade[L, W](q ++ y)
    case Leaf(_) => throw new UnsupportedOperationException("++ is not supported for two Leaf objects")
  }

  override def toString: String = value.toString
}

case class Clade[K, V](branches: Map[K, Document[K, V]]) extends Document[K, V] {
  def get(x: LinearSeq[K]): Option[V] = x match {
    case h :: t => branches.get(h) flatMap (_.get(t))
    case Nil => None
  }

  //  def asMap[L <: Any](implicit l: L): Map[L,Document[L,V]] = branches.asInstanceOf[Map[L,Document[L,V]]
  def pushDown[L <: K](ls: LinearSeq[L]): Map[L, Document[L, V]] = ls match {
    case Nil => branches.asInstanceOf[Map[L, Document[L, V]]]
    case h :: t => Map(h -> Clade(pushDown(t)))
  }

  def add[L <: K, W >: V](l: L, d: Document[L, W]): Document[L, W] = d match {
    case Clade(x) => val z = pushDown(LinearSeq(l)); val y = z.asInstanceOf[Clade[L, W]].branches; val q = x.asInstanceOf[Map[L, Document[L, W]]]; Clade[L, W](y ++ q)
    case Leaf(_) => d add(l, this.asInstanceOf[Document[L, W]])
  }

  override def toString: String = branches.toString
}

object Document {
  def apply[V](v: V): Document[Any, V] = Leaf(v)

  def apply[K, V](map: Map[K, V]): Document[K, V] = Clade(for ((k, v) <- map) yield k -> apply(v))
} 
Example 3
Source File: RichSeq.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
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()
  }
} 
Example 4
Source File: client.scala    From laserdisc   with MIT License 5 votes vote down vote up
package laserdisc

import shapeless.ops.hlist.ToSized
import shapeless.ops.sized.ToHList
import shapeless._

import scala.annotation.implicitNotFound
import scala.collection.LinearSeq
import scala.concurrent.duration._

trait ClientBase[F[_], Env] {
  def defaultTimeout: FiniteDuration = 20.seconds

  def send[In <: HList, Out <: HList](in: In, timeout: FiniteDuration)(implicit handler: Handler.Aux[F, Env, In, Out]): F[Out]
  final def send[In <: HList, Out <: HList](in: In)(implicit ev: Handler.Aux[F, Env, In, Out]): F[Out] = send(in, defaultTimeout)

  final def send[A1](protocolA1: Protocol.Aux[A1], timeout: FiniteDuration)(
      implicit F: Functor[F],
      ev: Handler.Aux[F, Env, Protocol.Aux[A1] :: HNil, Maybe[A1] :: HNil]
  ): F[Maybe[A1]] = F.map(send(protocolA1 :: HNil, timeout))(_.head)
  final def send[A1](protocolA1: Protocol.Aux[A1])(
      implicit F: Functor[F],
      ev: Handler.Aux[F, Env, Protocol.Aux[A1] :: HNil, Maybe[A1] :: HNil]
  ): F[Maybe[A1]] = send(protocolA1, defaultTimeout)

  final def send[CC[x] <: LinearSeq[x], A, N <: Nat, In <: HList, Out <: HList](
      sizedSeq: Sized[CC[Protocol.Aux[A]], N],
      timeout: FiniteDuration
  )(
      implicit F: Functor[F],
      toHList: ToHList.Aux[CC[Protocol.Aux[A]], N, In],
      ev0: Handler.Aux[F, Env, In, Out],
      ev1: ToSized.Aux[Out, CC, Maybe[A], N]
  ): F[Sized[CC[Maybe[A]], N]] = F.map(send(toHList(sizedSeq), timeout))(_.toSized)
  final def send[CC[x] <: LinearSeq[x], A, N <: Nat, In <: HList, Out <: HList](sizedSeq: Sized[CC[Protocol.Aux[A]], N])(
      implicit F: Functor[F],
      toHList: ToHList.Aux[CC[Protocol.Aux[A]], N, In],
      ev0: Handler.Aux[F, Env, In, Out],
      ev1: ToSized.Aux[Out, CC, Maybe[A], N]
  ): F[Sized[CC[Maybe[A]], N]] = send(sizedSeq, defaultTimeout)
}

trait Client[F[_], Env] extends ClientBase[F, Env] with ClientExt[F, Env]

trait Handler[F[_], Env, In <: HList] extends DepFn2[Env, In] {
  override final type Out = F[LOut]
  type LOut <: HList
}

object Handler {
  @implicitNotFound(
    """Cannot derive Handler[${F}, ${Env}, ${In}] { type Out = ${LOut0} }

This could depend on many things but most likely:
  - ${In} is not an HList of only laserdisc.Protocol types
  - deriving this Handler requires other type classes to be available in implicit scope

Try running scalac with -Xlog-implicits (or https://github.com/tek/splain)
"""
  ) type Aux[F[_], Env, In <: HList, LOut0 <: HList] = Handler[F, Env, In] { type LOut = LOut0 }
}