View TryFlatten.scala
import scala.util.{Success, Failure}
val seq=Seq(Success(1), Failure(new Exception("bang")), Success(2))
// all emit List(1, 2)
seq.map(_.toOption).flatten
seq.flatMap(_.toOption)
seq.filter(_.isSuccess).map(_.get)
seq.collect{case Success(x) => x}
View .block
license: gpl-3.0
height: 600
View InfiniteStreamReceiver.scala
/**
* Allows you to receive scala.collection.immutable.Stream
*/
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.receiver.Receiver
class InfiniteStreamReceiver[T](stream: Stream[T], delay:Int=0, storageLevel: StorageLevel) extends Receiver[T](storageLevel) {
receiver=>
View ES.scala
// https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_maven_repository.html
// Play 2.5 is not compatible with ES 5.x Java TCP, as ES demands Netty 4.1 vs Play's 4.0
// https://github.com/sksamuel/elastic4s/issues/672
import org.apache.http.HttpHost
import org.apache.http.entity.ContentType
import org.apache.http.nio.entity.NStringEntity
import org.apache.http.util.EntityUtils
import org.elasticsearch.client.RestClient
import play.api.libs.json.{Format, Json, Writes}
View Module.scala
import actors.ActorBrowserActor
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
class Module extends AbstractModule with AkkaGuiceSupport{
def configure(): Unit = {
bindActor[ActorBrowserActor](ActorBrowserActor.NAME, _=>ActorBrowserActor.props)
}
}
View Module.scala
import actors.StartActorBrowser
import com.google.inject.AbstractModule
class Module extends AbstractModule {
def configure(): Unit = {
bind(classOf[StartActorBrowser]).asEagerSingleton()
}
}
View twirl.scala.html
@for((name, actor, status) <- actorsInfo) {
@{
val camelCase=actorNameToCamelCase(name)
<tr>
<td>{name}</td>
<td>{status}</td>
<td><a href={s"/actors/$camelCase"}>{camelCase}</a></td>
</tr>
}
}
View MyIterable.scala
class MyIterable extends Iterable[Int]{
def iterator: Iterator[Int]={
Iterator(1,2,3)
}
}
val my=new MyIterable
(my ++ my).foreach{println}
View MyTraverable.scala
class MyTraverable extends Traversable[Int]{
def foreach[U](f: Int => U)={
f(1)
f(2)
f(3)
}
}
val my=new MyTraverable
View Assignment.scala
case class Thingy(name:String, age:Int){
def +(x:Int)=copy(age=age+x)
def unary_- =copy(age = -age)
}
var dino=Thingy("Dino",49)
println(dino) // Thingy(Dino,49)
// Note, Thingy is immutable, but we reassign the var