Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / RowParser.scala
Last active Mar 4, 2019
Little Typeclass example
View RowParser.scala
case class Row(rowstring: String)
trait RowParse[A]{
def parse(row:Row):A
}
case class TwoPart(v1: String, v2: String)
case class ThreePart(v1: String, v2: String, v3: String)
@fancellu
fancellu / Censor.scala
Created Dec 4, 2018
Scala uJson code to censor certain field names
View Censor.scala
import ujson.{ Obj, Value }
object Censor{
def updateAll(root: Value, target: (String => Boolean), newValue: Value): Value = {
def update(value: Value, parent: Value = null, valueName: String = null): Unit = {
value match {
case Obj(values) =>
values.map { case (name, v) => update(v, value, name) }
@fancellu
fancellu / FoldingValidationExamples.scala
Created Nov 13, 2018
Simple chaining validation example with pure Scala
View FoldingValidationExamples.scala
import scala.util.{Failure, Try}
val li=List(1,2,3,4)
type Transform[F[_],A]=F[A] => F[A]
def filterSmall: Transform[Try,Int] ={ ti=>
ti.flatMap(i=>if (i>1) Try(i) else new Failure(new Exception(s"$i is too small")))
}
View keybase.md

Keybase proof

I hereby claim:

  • I am fancellu on github.
  • I am fancellu (https://keybase.io/fancellu) on keybase.
  • I have a public key ASAIwD1-6lC-FAdHSUyefT5Xul7zEulj3OlQnMZ7uGYBtAo

To claim this, I am signing this object:

@fancellu
fancellu / TryFlatten.scala
Created Apr 11, 2017
A few ways to flatten down a Seq[Try] to only Success values
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}
@fancellu
fancellu / .block
Last active Mar 8, 2019
Force directed graph for D3.js v4 with labelled edges and arrows
View .block
license: gpl-3.0
height: 600
@fancellu
fancellu / InfiniteStreamReceiver.scala
Created Jan 23, 2017
Spark streaming custom receiver example, consumes scala Streams
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=>
@fancellu
fancellu / ES.scala
Last active Nov 24, 2016
ElasticSearch 5.x Java client REST example with Play JSON (to get around Netty issue)
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}
@fancellu
fancellu / Module.scala
Created Nov 18, 2016
Using bindActor: Play 2.5/Akka/Guice, starting an actor before anything else,
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)
}
}
@fancellu
fancellu / Module.scala
Created Nov 18, 2016
Play 2.5/Akka/Guice, starting an actor before anything else
View Module.scala
import actors.StartActorBrowser
import com.google.inject.AbstractModule
class Module extends AbstractModule {
def configure(): Unit = {
bind(classOf[StartActorBrowser]).asEagerSingleton()
}
}
You can’t perform that action at this time.