I use scala ArrayLists and java.util.ArrayList a lot. Here's an easy way to not get confused:
import java.util.{ArrayList => JArrayList}
Learning Scala. The hard way.
import java.util.{ArrayList => JArrayList}
final class AnEmptyStackSpeccedUsingScala {
@Stub var element: Int = 1
@Specification {
val expectedException = classOf[RuntimeException],
val withMessage = "Cannot pop an empty stack"}
def failsWhenPopped {
EmptyStack.pop
}
@Specification {
val expectedException = classOf[RuntimeException],
val withMessage = "Nothing to see"}
def failsWhenPeeked {
EmptyStack.peek
}
@Specification
def returnsNoneWhenSafelyPopped {
expect.that(EmptyStack.safePop).isEqualTo(None)
}
@Specification
def isNoLongerEmptyAfterPush {
val stack = EmptyStack.push(element)
expect.that(stack.peek).isEqualTo(element)
}
}
sealed trait Stack[+A] {
def push[B >: A](element: B): Stack[B]
def pop: Stack[A]
def safePop: Option[Stack[A]]
def peek: A
def safePeek: Option[A]
}
final case object EmptyStack extends Stack[Nothing] {
override def push[B](element: B) =
NonEmptyStack(element, this)
override def pop = error("Cannot pop an empty stack")
override def safePop = None
override def peek = error("Nothing to see")
override def safePeek = None
}
final case class NonEmptyStack[+A](
element: A, elements: Stack[A])
extends Stack[A] {
override def push[B >: A](element: B) =
NonEmptyStack(element, this)
override def pop = elements
override def safePop = Some(elements)
override def peek = element
override def safePeek = Some(element)
}
exit
method on Actor
is for use only within the actor, not externally. That is, a unit test cleanup script can't just call
myActor.exit
case Stop => exit
net.liftweb.util
, the ?~
operator is equivalent to asking "is this Can empty?", but has the added bonus of returning a Failure object if it is.
log.ifDebug("got " + getCount + " of them")
log.ifDebug(new Function0[String] { def apply = { "got " + getCount + " of them" } })
new
", which would happen for almost any logging.message
" from the method name:
def ifDebug(message: => AnyRef)
message
, and therefore evaluate the function. But I still think it would be nice if I could somehow sweet-talk the compiler into giving me the function object. Call-by-name is mostly for the syntactical-sugar convenience of the caller.
public String[] getPrefixes(String prefix) {
Vector<string> v = new Vector<string>();
for(String s : map.keySet())
{
if(s.startsWith(prefix) && !s.equals(prefix))
{
v.add(s);
}
}
if(v.size() > 0)
{
return (String[])v.toArray(new String[0]);
}
else
{
return null;
}
}
def getPrefixes(prefix: String): Array[String] = {
val keyList = (for (val key <- map.keys if key.startsWith(prefix) && (key != prefix)) yield key).toList
if (keyList.size > 0) keyList.toArray else null
}
keyList.toArray unless keyList.empty?
" but scala uses inline if/else for the "a ? b : c
" operator so it looks weird. Second try, let's use matching instead:
def getPrefixes(prefix: String): Array[String] = {
(for (val key <- map.keys if key.startsWith(prefix) && (key != prefix)) yield key).toList match {
case Nil => null
case list => list.toArray
}
}
(...).toList
" construct still bothers me a bit though. Is there some better way to do that?
val lst = List(1,2,3,4)
lst match {
case 1 :: _ => // match anything with the first element as 1
}
lst match {
case _ :: 2 :: _ => // match anything with the second element as 2
}
lst match {
case first :: 2 :: _ => // matches if the second item in the list is 2 and assigns the first element to "first"
}
::
operator is more or less equivalent to cons in Lisp, ML, and similar languages. When you're pattern matching, it extracts. When you're declaring or assigning variables (particularly Lists), it concatenates.
lazy val
.object MySingletonObject {
def singletonMethod() = println("What a fancy method, and so singular!")
}
So now if you want that supremely valuable console message, just call MySingletonObject.singletonMethod()
.class MyClass
def say_hi
print "hi"
end
def self.say_yo_from_the_class
print "yo"
end
end
>> m = MyClass.new
=> #
>> m.say_hi
hi=> nil
>> MyClass.say_yo_from_the_class
yo=> nil
You've got MyClass defined in your Scala code, but how to get that badass say_yo_from_the_class
class method? Like this, kid:object MyClass {
def sayYoFromTheClass() = println("yo")
}
The above singleton is now the companion object of MyClass
. Companion objects have to be defined the same source file as the class they accompany. Other than that, pretty intuitive.