本文整理汇总了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()
}
}
}
}
示例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()
}
}
}
示例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)))
}
示例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"))
}
}
示例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)
}
示例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)
}
}
示例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()
}
}
}
示例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
}
}
示例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)
}
}
示例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()
}
}
}
示例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)
}
示例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()
}
}
}
示例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")
}
}
}
示例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"))
}
}
示例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)
}
}