package nova.monadic_sfx.util final class BoundedStack private ( val underlying: List[Int], val maxSize: Int, val sp: Int ) { def push(v: Int): Either[String, BoundedStack] = if (sp < maxSize) Right(new BoundedStack(v :: underlying, maxSize, sp + 1)) else Left("overflow") def pushAll(v: List[Int]) = if (sp + v.length < maxSize) Right(new BoundedStack(v ::: underlying, maxSize, sp + v.length)) else Left("overflow") def pop = if (sp == 0) Left("Underflow") else Right( underlying(sp) -> new BoundedStack( underlying.splitAt(sp)._1, maxSize, sp - 1 ) ) } object BoundedStack { def apply( // defaultValues: List[Int] = List.empty, maxSize: Int = 10 // sp: Int = 0 ) = new BoundedStack(List.empty, maxSize, 0) }