Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / hash_hmac.scala
Last active August 10, 2016 12:04
PHP hash_hmac in 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
}
@fancellu
fancellu / numberFun.scala
Created September 17, 2016 21:48
Places the numbers 1 to 9 inclusive, once each, in these gaps to make this sum correct
object numberFun {
/* Places the numbers 1 to 9 inclusive, once each, in these gaps to make this sum correct
() () () ()
x 3
() () () () ()
*/
@fancellu
fancellu / Assignment.scala
Last active October 14, 2016 12:39
Assignment operator example, plus unary
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
@fancellu
fancellu / MyTraverable.scala
Created October 25, 2016 13:29
Trivial example of custom Traversable
class MyTraverable extends Traversable[Int]{
def foreach[U](f: Int => U)={
f(1)
f(2)
f(3)
}
}
val my=new MyTraverable
@fancellu
fancellu / MyIterable.scala
Created October 25, 2016 13:36
Trivial example of custom Iterable
class MyIterable extends Iterable[Int]{
def iterator: Iterator[Int]={
Iterator(1,2,3)
}
}
val my=new MyIterable
(my ++ my).foreach{println}
@fancellu
fancellu / twirl.scala.html
Created November 3, 2016 22:29
Example of inline Play twirl variable and scala escaping
@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>
}
}
@fancellu
fancellu / Module.scala
Created November 18, 2016 14:54
Play 2.5/Akka/Guice, starting an actor before anything else
import actors.StartActorBrowser
import com.google.inject.AbstractModule
class Module extends AbstractModule {
def configure(): Unit = {
bind(classOf[StartActorBrowser]).asEagerSingleton()
}
}
@fancellu
fancellu / Module.scala
Created November 18, 2016 15:05
Using bindActor: Play 2.5/Akka/Guice, starting an actor before anything else,
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)
}
}
@fancellu
fancellu / ES.scala
Last active November 24, 2016 17:55
ElasticSearch 5.x Java client REST example with Play JSON (to get around Netty issue)
// 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}
@fancellu
fancellu / InfiniteStreamReceiver.scala
Created January 23, 2017 17:26
Spark streaming custom receiver example, consumes scala Streams
/**
* 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=>