A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That // ... } def zipWithIndexIf[T](list: List[T])(pred: T => Boolean): List[(T, Option[Int])] = { type R = List[(T, Option[Int])] def doZip(zipped: R, left: List[T], index: Int): R = left match { case x :: xs if pred(x) => doZip((x, Some(index)) :: zipped, xs, index + 1) case x :: xs => doZip((x, None) :: zipped, xs, index) case Nil => zipped } doZip(Nil, list, 0).reverse }
A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That // ... } def zipWithIndexIf[T](list: List[T])(pred: T => Boolean): List[(T, Option[Int])] = { type R = List[(T, Option[Int])] def doZip(zipped: R, left: List[T], index: Int): R = left match { case x :: xs if pred(x) => doZip((x, Some(index)) :: zipped, xs, index + 1) case x :: xs => doZip((x, None) :: zipped, xs, index) case Nil => zipped } doZip(Nil, list, 0).reverse }
} trait T2 { def bar = "bar" } class A extends T1 with T2 val a = new A a.foo // "foo" a.bar // "bar" module T1 def foo "foo" end end module T2 def bar "bar" end end class A include T1 include T2 end a = A.new a.foo # => "foo" a.bar # => "bar"
A { def foo "singleton" } new A().foo // "instance" A.foo // "singleton" class A def foo "instance" end class << self def foo "singleton" end end end A.new.foo # => "instance" A.foo # => "singleton"
Dove def quack def walk end class Cat end def quack_and_walk(animal) animal.quack animal.walk end quack_and_walk Duck.new quack_and_walk Dove.new quack_and_walk Cat.new # NoMethodError
class Dove { def quack def walk } class Cat def quackAndWalk(a: { def quack; def walk }) = { a.quack a.walk } quackAndWalk(new Duck) quackAndWalk(new Dove) quackAndWalk(new Cat) // Compile error class Duck def quack def walk end class Dove def quack def walk end class Cat end def quack_and_walk(animal) animal.quack animal.walk end quack_and_walk Duck.new quack_and_walk Dove.new quack_and_walk Cat.new # NoMethodError
should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should equal (2) stack.pop() should equal (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[String] evaluating { emptyStack.pop() } should produce [NoSuchElementException] } } http://scalatest.org