scala.collection.mutable.Stack Scala Examples

The following examples show how to use scala.collection.mutable.Stack. 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: RunInOutSpec.scala    From make-your-programs-free   with GNU General Public License v3.0 5 votes vote down vote up
package free

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

import scalaz._, Scalaz._

class RunInOutSpec extends FreeSpec with Matchers {

  def interpreter(input: Stack[String], output: ListBuffer[String]) = new (InOut ~> Id) {

    def apply[A](inout: InOut[A]): Id[A] = inout match {
      case PrintLine(line) =>
        output += line
        ()
      case GetLine =>
        input.pop
    }
  }

  "A program" - {
    "should ask for a name and greet the user" in {
      // given
      implicit val ops = new InOut.Ops[InOut]
      val input = Stack.empty[String]
      val output = ListBuffer.empty[String]
      input.push("Pawel")
      // when
      OurFirstProgram.program.foldMap(interpreter(input, output))
      // then
      input.size should be(0)
      output should equal(ListBuffer("What is your name", "Nice to meet you Pawel"))
    }
  }
} 
Example 2
Source File: StackTest.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo


object StackTest extends  App{
  import scala.collection.mutable.Stack
  val stack = new Stack[Int]
  stack.push(1)//在最顶层加入数据
  stack.push(2)//在最顶层加入数据
 stack.top //返回并移除最顶层的数据
  //res8: Int = 2
 stack.pop //返回最顶层数据的值,但不移除它
 // res10: Int = 2
  println(stack)
//res11: scala.collection.mutable.Stack[Int] = Stack(1)
} 
Example 3
Source File: DoubleMill.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
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 MillNumeric(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)
} 
Example 4
Source File: SpireMill.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package models

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


object SpireMill {

  val conv: String=>Try[Real] = SpireMill.valueOf _
  val lookup: String=>Option[Real] = SpireMill.constants.get _
  implicit val store = Map[String,Real]()
  implicit val parser = new ExpressionParser[Real](conv,lookup)
  def apply(): Mill[Real] = new MillSpire(Stack[Real]()) {
    def apply(s: String): Try[Real] = SpireMill.valueOf(s)    
  }
  def valueOf(s: String): Try[Real] = Try(Real(s))
 val constants = Map("e"->Real.e, "pi"->Real.pi)
} 
Example 5
Source File: MillNumeric.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package models

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


abstract class MillNumeric[A : Numeric](stack: Stack[A])(implicit store: Map[String,A]) extends Mill[A](stack)(store) { self =>

  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")
  }
  
} 
Example 6
Source File: RationalMill.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
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 MillNumeric(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)))
} 
Example 7
Source File: Mill.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package models

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


abstract class Mill[A](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
  
  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"))
    }
} 
Example 8
Source File: MillSpire.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package models

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


abstract class MillSpire[A : Numeric](stack: Stack[A])(implicit store: Map[String,A]) extends Mill[A](stack)(store) { self =>
  
  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")
  }
  
}