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
View numberFun.scala
object numberFun {
/* Places the numbers 1 to 9 inclusive, once each, in these gaps to make this sum correct
() () () ()
x 3
() () () () ()
*/
View hash_hmac.scala
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
def hash_hmac(s: String, secret:String): String = {
val sha256_HMAC = Mac.getInstance("HmacSHA256")
sha256_HMAC.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"))
sha256_HMAC.doFinal(s.getBytes("UTF-8")).map(char=>f"$char%02x").mkString
}