View JavaNullHandler
object play {
println("Welcome to the Scala worksheet")
case class User(id:Int, name:String)
def getUser:User = null
def processUser(u:User):String= u.name
Option(getUser)
Option(getUser).fold("Sky")(u=>processUser(u))
View AdvancedOptions
object OptionPlay {
println("Welcome to the Scala worksheet")
def someCrappyJavaService = null
val result: Option[String] = Some("Hello")
def someProcessingLogic(x: String) = println(s"I would execute when there is some value like $x")
def opSomeProcessingLogic(x: String): Option[String] = { println(s"I would execute when there is some value like $x"); Some("Some String") }
//Standard Way
if (result != None) someProcessingLogic(result.get)
if (result.isDefined) someProcessingLogic(result.get)
View Scala Idiomatic Error Handling
def sayHello(any: Any): Try[String] = {
Try {
any match {
case x: String => "Hello"
case _ => throw new Exception("Huh!")
}
}
} //> sayHello: (any: Any)scala.util.Try[String]
def letMeSayHello = {
View gist:5411842
/room/classroom),,true,false), , GetRequest, Empty)
java.lang.NullPointerException: null
at net.liftweb.mongodb.record.field.JsonObjectField.asJValue(JsonObjectField.scala:45) ~[lift-mongodb-record_2.9.0-1-2.4.jar:2.4]
at net.liftweb.record.MetaRecord$$anonfun$asJValue$1.apply(MetaRecord.scala:274) ~[lift-record_2.9.0-1-2.4.jar:2.4]
at net.liftweb.record.MetaRecord$$anonfun$asJValue$1.apply(MetaRecord.scala:274) ~[lift-record_2.9.0-1-2.4.jar:2.4]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:na]
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:na]
at scala.collection.immutable.List.foreach(List.scala:45) ~[scala-library.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:194) ~[scala-library.jar:na]
View GoogleDispatcher
package login
import net.liftweb.http.LiftRules
import net.liftweb.http.Req
import net.liftweb.http.GetRequest
import net.liftweb.http.S
import net.liftweb.common.Box
import net.liftweb.http.LiftResponse
import net.liftweb.util.Props
import net.liftweb.common.Full
import net.liftweb.common.Empty
View TwitterDispatcher
package login
import net.liftweb.http.LiftRules
import net.liftweb.http.Req
import net.liftweb.http.GetRequest
import twitter4j.TwitterException
import twitter4j.TwitterFactory
import net.liftweb.http.S
import net.liftweb.common.Box
import net.liftweb.http.LiftResponse
import net.liftweb.util.Props
View SalatCustomContext
import org.scalatest.FunSuite
import com.novus.salat.annotations.Key
import org.bson.types.ObjectId
import org.specs2.specification.BeforeAfter
import org.scalatest.BeforeAndAfter
import com.novus.salat.dao.SalatDAO
import com.mongodb.casbah.MongoConnection
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
//import com.novus.salat.global._
View gist:2487883
import org.scalatest.FunSuite
import com.novus.salat.annotations.raw.Key
import org.bson.types.ObjectId
import org.specs2.specification.BeforeAfter
import org.scalatest.BeforeAndAfter
import com.novus.salat.dao.SalatDAO
import com.mongodb.casbah.MongoConnection
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import com.novus.salat.global._
View PullApplicationActorLess.scala
object PullApplicationActorLess extends App {
var context: ZMQ.Context = null
var pullSocket: ZMQ.Socket = null
context = ZMQ.context(5)
pullSocket = context.socket(ZMQ.PULL)
pullSocket.connect("tcp://127.0.0.1:5555")
println("Starting consumer ...")
View PullApplicationActorLess.scala
object PullApplicationActorLess extends App {
val TOTAL_MESSAGES = 1000000
val diagnostics = actorOf(new Diagnostics.DiagnosticsActor).start
var context: ZMQ.Context = null
var pullSocket: ZMQ.Socket = null
context = ZMQ.context(1)
pullSocket = context.socket(ZMQ.PULL)
pullSocket.connect("tcp://127.0.0.1:5555")