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


Scala Stack类代码示例

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


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

示例1: DatabaseSpec

//设置package包名称以及导入依赖的类
import java.sql.{Driver, DriverManager}

import org.scalatest.BeforeAndAfter
import org.scalatestplus.play.PlaySpec
import play.api.Logger
import scalikejdbc._
import scalikejdbc.config.DBs

import scala.collection.mutable.Stack

class DatabaseSpec extends PlaySpec with BeforeAndAfter {
  implicit var session = AutoSession

  before {
    DBs.setupAll()
  }

  after {
    DBs.closeAll()
  }

  "Database" must {
    "List of database drivers" in {
      val drivers = DriverManager.getDrivers

      while (drivers.hasMoreElements) {
        val driver:Driver = drivers.nextElement()
        Logger.info(driver.toString)
      }
    }
    "Fetch users" in {
      val sql: SQL[Nothing, NoExtractor] = sql"select * from users"
      val map: SQL[Map[String, Any], HasExtractor] = sql.toMap
      val list: SQLToList[Map[String, Any], HasExtractor] = map.list
      val users: List[Map[String, Any]] = list.apply()
    }
  }

  "Slick" must {
    "How to use Scala test" in {
      // TODO
    }

    "throw NoSuchElementException if an empty stack is popped" in {
      val emptyStack = new Stack[Int]
      a [NoSuchElementException] must be thrownBy {
        emptyStack.pop()
      }
    }
  }
} 
开发者ID:esperia,项目名称:play-scala-sample,代码行数:52,代码来源:DatabaseSpec.scala

示例2: ExampleSpec

//设置package包名称以及导入依赖的类
package com.github.uryyyyyyy.samples.scalatest

import scala.collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

	"A Stack" should "pop values in last-in-first-out order" in {
		val stack = new Stack[Int]
		stack.push(1)
		stack.push(2)
		stack.pop() should be (2)
		stack.pop() should be (1)
	}

	it should "throw NoSuchElementException if an empty stack is popped" in {
		val emptyStack = new Stack[Int]
		a [NoSuchElementException] should be thrownBy {
			emptyStack.pop()
		}
	}
} 
开发者ID:uryyyyyyy,项目名称:scalaTests,代码行数:23,代码来源:ExampleSpec.scala

示例3: RationalMill

//设置package包名称以及导入依赖的类
package models

import scala.collection.mutable.{Stack,Map}
import scala.util._
import edu.neu.coe.scala.numerics.Rational


object RationalMill {

  val conv: String=>Try[Rational] = RationalMill.valueOf _
  val lookup: String=>Option[Rational] = RationalMill.constants.get _
  implicit val store = Map[String,Rational]()
  implicit val parser = new ExpressionParser[Rational](conv,lookup)
  def apply(): Mill[Rational] = new Mill(Stack[Rational]()) {
    def apply(s: String): Try[Rational] = RationalMill.valueOf(s)    
  }
  def valueOf(s: String): Try[Rational] = Try(Rational(s))
 val constants = Map("e"->Rational(BigDecimal(math.E)), "pi"->Rational(BigDecimal(math.Pi)))
} 
开发者ID:menezesl,项目名称:Scala-repo,代码行数:20,代码来源:RationalMill.scala

示例4: Mill

//设置package包名称以及导入依赖的类
package models

import scala.collection.mutable.{Stack,Map}
import scala.util._


abstract class Mill[A : Numeric](stack: Stack[A])(implicit store: Map[String,A]) extends Function1[Valuable[A],Try[A]] { self =>
  
  var debugMill = false;
  def value = if (stack.size>0) Success(stack.top) else Failure(new IllegalArgumentException("stack is empty"))
  def toSeq = stack.toSeq
  def show = println(stack)
  def push(x: A) = { if (debugMill) println(s"push $x");stack.push(x)}
  def pop = {val x = stack.pop; if (debugMill) println(s"popped $x"); x}
  def setDebug(b: Boolean) { debugMill = b }
  def has(n: Int) = assert(stack.size>=n,s"operation requires $n element(s) on stack")
 
  def apply(v: Valuable[A]) = v match {
    case n @ Number(x) => n.apply match {case Success(x) => push(x); case Failure(e) => throw e}; value
    case k @ Constant(x) => k.apply match {case Success(x) => push(x); case Failure(e) => throw e}; value
    case Operator(s) => operate(s); value
    case MemInst(s,n) => memInst(s,n); value
  }
  
  def dyadic(f: (A,A)=>A) = { has(2); push(f(pop,pop)) }
  def monoadic(f: (A)=>A) = { has(1); push(f(pop)) }
  def monoadic2(f: (A,A)=>A)(a: A) = { has(1); push(f(a,pop)) }
  
  def operate(s: String): Unit = s match {
    case "+" => operate("plus")
    case "plus" => dyadic(implicitly[Numeric[A]].plus)
    case "-" => operate("chs"); operate("plus")
    case "chs" => monoadic(implicitly[Numeric[A]].negate)
    case "*" => operate("times")
    case "times" => dyadic(implicitly[Numeric[A]].times)
    case "div" => operate("/")
    case "/" => operate("inv"); operate("times")
    case "inv" => val i = implicitly[Numeric[A]]; if (i.isInstanceOf[Fractional[A]]) monoadic2(i.asInstanceOf[Fractional[A]].div _)(i.one)
    case "swap" => has(2); val (top,next) = (pop,pop); push(top); push(next)
    case "del" => has(1); pop
    case "clr" => stack.clear
    case x => throw new IllegalArgumentException(s"operator $x is not supported")
  }
  
  def memInst(s: String, k: String) = s.toLowerCase match {
    case "sto" => value match {case Success(x) => store.put(k,x); case Failure(e) => throw e}
    case "rcl" => store.get(k) match {case Some(x) => push(x); case None => throw new IllegalArgumentException(s"no value at memory location $k")}
  }
  
  def parse(s: String)(implicit parser: ExpressionParser[A]): Try[A] = 
    parser.parseAll(parser.expr,s) match {
      case parser.Success(ws,_) => try {
          (for (w <- ws) yield apply(w)).reverse.head
        } catch {
        case t: Throwable => Failure(t)
      }
      case parser.Failure(e,_) => Failure(new IllegalArgumentException(s"parseResult error: $e"))
      case r @ _ => Failure(new IllegalArgumentException(s"logic error: parseResult is $r"))
    }
} 
开发者ID:menezesl,项目名称:Scala-repo,代码行数:61,代码来源:MillNumeric.scala

示例5: DoubleMill

//设置package包名称以及导入依赖的类
package models

import scala.collection.mutable.{Stack,Map}
import scala.util._
//

object DoubleMill {
  val conv: String=>Try[Double] = DoubleMill.valueOf _
  val lookup: String=>Option[Double] = DoubleMill.constants.get _
  implicit val store = Map[String,Double]()
  implicit val parser = new ExpressionParser[Double](conv,lookup)
  def apply(): Mill[Double] = new Mill(Stack[Double]()) {
    def apply(s: String): Try[Double] = DoubleMill.valueOf(s)    
  }
  def valueOf(s: String): Try[Double] = Try(s.toDouble)
  val constants = Map("e"->math.E, "pi"->math.Pi)
} 
开发者ID:menezesl,项目名称:Scala-repo,代码行数:18,代码来源:DoubleMill.scala

示例6: ExampleSuite

//设置package包名称以及导入依赖的类
package pl.writeonly.babel.example

import org.scalatest.FunSuite
import scala.collection.mutable.Stack

class ExampleSuite extends FunSuite {

  test("pop is invoked on a non-empty stack") {

    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    val oldSize = stack.size
    val result = stack.pop()
    assert(result === 2)
    assert(stack.size === oldSize - 1)
  }

  test("pop is invoked on an empty stack") {

    val emptyStack = new Stack[Int]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
    assert(emptyStack.isEmpty)
  }
} 
开发者ID:writeonly,项目名称:babel,代码行数:28,代码来源:ExampleSuite.scala

示例7: ExampleSpec

//设置package包名称以及导入依赖的类
package com.github.interaction.algorithms_of_the_intelligent_web

import org.scalatest._

import scala.collection.mutable.Stack

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
} 
开发者ID:inter-action,项目名称:algorithms_of_the_intelligent_web,代码行数:24,代码来源:AppSpec.scala

示例8: CommandManager

//设置package包名称以及导入依赖的类
package de.htwg.se.pixels.controller

import de.htwg.se.pixels.util.ICommand

import scala.collection.mutable.{ListBuffer, Stack}


class CommandManager {
  var commandStack = new ListBuffer[ICommand]
  var anzahl = 0

  def undo: Unit = {
    if(!commandStack.isEmpty) {
        val cmd = commandStack.remove(anzahl-1)
        anzahl = anzahl -1
        cmd.undo
    }
  }

  def executeCommand(cmd:ICommand): Unit = {
    cmd.execute
    commandStack += cmd
    anzahl= anzahl + 1
  }
} 
开发者ID:JessicaKaechele,项目名称:Pixels,代码行数:26,代码来源:CommandManager.scala

示例9: DemoApplicationTest

//设置package包名称以及导入依赖的类
package io.skysail.app.demo

import org.scalatest.FunSuite

import scala.collection.mutable.Stack
import spray.json._
import DefaultJsonProtocol._
import akka.http.scaladsl.marshallers.sprayjson._
import io.skysail.core.akka.Item
import spray.json.DefaultJsonProtocol

class DemoApplicationTest extends FunSuite {

  object MyJsonProtocol extends DefaultJsonProtocol {
    implicit val appFormat = jsonFormat1(Contact)
  }

  test("pop is invoked on a non-empty stack") {

    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    val oldSize = stack.size
    val result = stack.pop()
    assert(result === 2)
    assert(stack.size === oldSize - 1)
  }

  test("pop is invoked on an empty stack") {

    val emptyStack = new Stack[Int]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
    assert(emptyStack.isEmpty)
  }

  test("a") {
    import MyJsonProtocol._
    //implicit val itemFormat = jsonFormat1(Application)
    val app1 = new Contact("hier")
    val app2 = new Contact("dort")
    val l = List(app1, app2)
    val str = l.toJson.toString()
    println(str)
  }
} 
开发者ID:evandor,项目名称:skysail-core,代码行数:48,代码来源:DemoApplicationTest.scala

示例10: StackSpecShortcut

//设置package包名称以及导入依赖的类
import org.scalatest.FlatSpec
import scala.collection.mutable.Stack

class StackSpecShortcut extends FlatSpec {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    assert(stack.pop() === 2)
    assert(stack.pop() === 1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[String]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
  }



} 
开发者ID:davidheryanto,项目名称:scalatest-example,代码行数:24,代码来源:StackSpecShortcut.scala

示例11: StackSpec

//设置package包名称以及导入依赖的类
import org.scalatest.FlatSpec
import scala.collection.mutable.Stack

class StackSpec extends FlatSpec {

  behavior of "A Stack"

  it should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    assert(stack.pop() === 2)
    assert(stack.pop() === 1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[String]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
  }

  ignore should "Ignore a test" in {
    val emptyStack = new Stack[String]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
  }

  it must "Pending a test" is (pending)

} 
开发者ID:davidheryanto,项目名称:scalatest-example,代码行数:33,代码来源:StackSpec.scala

示例12: StackSpec

//设置package包名称以及导入依赖的类
package com.realtimecep.scalatest

import org.scalatest._

import scala.collection.mutable.Stack

class StackSpec extends FlatSpec {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    assert(stack.pop() === 2)
    assert(stack.pop() === 1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[String]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
  }
} 
开发者ID:tedwon,项目名称:app-exception-analyzer,代码行数:24,代码来源:StackSpec.scala

示例13: ReversePolishEvaluator

//设置package包名称以及导入依赖的类
package com.nakoradio.scalc.core.parser

import scala.collection.mutable.Stack

class ReversePolishEvaluator extends ExpressionEvaluator {

  def apply(input: String): EvaluatorResult = {
    val parser = new ShuntingYardParser()
    val result = new Stack[BigDecimal]

    try {
      parser(input).reverse.foreach { token =>
        token match {
          case n: NumberTerm => result.push(n.value)
          case o: Operator   => result.push(o.eval(result.pop, result.pop))
        }
      }
      if (result.size != 1) {
        EvaluatorFailure("Malformed input")
      } else {
        EvaluatorSuccess(result.pop)
      }
    } catch {
      case ex: ShuntException => EvaluatorFailure(ex.message)
      case e: Exception       => EvaluatorFailure("Unexpected error")
    }

  }

} 
开发者ID:Arch-vile,项目名称:sCalc,代码行数:31,代码来源:ReversePolishEvaluator.scala

示例14: PostfixExpressionEvaluator

//设置package包名称以及导入依赖的类
package main.scala.calculator

import scala.collection.mutable.Stack

object PostfixExpressionEvaluator {
  val constants = Map(
    "pi" -> Math.PI, "e" -> Math.E
  )

  def evaluate(postfixTokens: List[Token]): Either[Double, ExpressionError] = {
    val operandsStack = Stack[Double]()
    postfixTokens.foreach(token => {
      token match {
        case NumberToken(_, _) =>
          val value = token.text.toDouble
          operandsStack.push(value)
        case VariableToken(_, _) =>
          val value = if (token.text.head == '-') {
            val constant = token.text.tail.toLowerCase
            if (constants.contains(constant)) {
              -constants(constant)
            } else {
              return Right(new ExpressionError(s"Unknown $token variable"))
            }
          }
          else {
            val constant = token.text.toLowerCase
            if (constants.contains(constant)) {
              constants(constant)
            } else {
              return Right(new ExpressionError(s"Unknown $token variable"))
            }
          }
          operandsStack.push(value)
        case OperatorToken(_, _) => {
          if (operandsStack.length < 2) {
            return Right(new ExpressionError(s"Insufficient number of operands for ${token} operator"))
          }
          val right = operandsStack.pop()
          val left = operandsStack.pop()
          val result: Double = token.text match {
            case "+" => left + right
            case "-" => left - right
            case "*" => left * right
            case "/" => left / right
            case "^" => Math.pow(left, right)
            case "%" => left % right
            case _ => return Right(new ExpressionError(s"Failed to parse ${token} token"))
          }
          operandsStack.push(result)
        }
      }
    })
    if (operandsStack.isEmpty) {
      return Left(0.0)
    } else if (operandsStack.length == 1) {
      return Left(operandsStack.pop())
    }
    return Right(new ExpressionError(s"Invalid expression"))
  }
} 
开发者ID:sirIrishman,项目名称:calculator,代码行数:62,代码来源:PostfixExpressionEvaluator.scala

示例15: NotationConverter

//设置package包名称以及导入依赖的类
package main.scala.calculator

import scala.collection.mutable.{Stack, ListBuffer}

object NotationConverter {
  // https://en.wikipedia.org/wiki/Order_of_operations#Programming_languages
  val precedence = Map(
    "(" -> 1, ")" -> 1,
    "*" -> 3, "/" -> 3, "%" -> 3, "^" -> 3,
    "+" -> 4, "-" -> 4
  )
  val operators = precedence.keys.toList

  def fromInfixToPostfix(infixTokens: List[Token]): Either[List[Token], ExpressionError] = {
    val postfixTokens = ListBuffer[Token]()
    val operatorsStack = Stack[Token]()
    infixTokens.foreach(token => {
      token match {
        case NumberToken(_, _) |
             VariableToken(_, _) =>
          postfixTokens += token
        case OperatorToken("(", _) =>
          operatorsStack.push(token)
        case OperatorToken(")", _) => {
          while (!operatorsStack.isEmpty && operatorsStack.head.text != "(") {
            postfixTokens += operatorsStack.pop()
          }
          if (operatorsStack.isEmpty) {
            return Right(new ExpressionError(s"Parentheses are not balanced"))
          }
          operatorsStack.pop()
        }
        case OperatorToken(operator, _) if operators contains operator => {
          while (!operatorsStack.isEmpty && operatorsStack.head.text != "(" && precedence(operator) >= precedence(operatorsStack.head.text)) {
            postfixTokens += operatorsStack.pop()
          }
          operatorsStack.push(token)
        }
        case _ =>
          return Right(new ExpressionError(s"Failed to parse ${token} token"))
      }
    })
    if (operatorsStack.exists(token => token.text == "(")) {
      return Right(new ExpressionError(s"Parentheses are not balanced"))
    }
    postfixTokens ++= operatorsStack.toList
    Left(postfixTokens.toList)
  }
} 
开发者ID:sirIrishman,项目名称:calculator,代码行数:50,代码来源:NotationConverter.scala


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