Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / ClassTagExample.scala
Created February 18, 2016 10:54
Example use of ClassTag to get around type erasure, filtering by type
val listOfAnys = List("x", 33, 21, "y", 55, 2.0, "z")
//> listOfAnys : List[Any] = List(x, 33, 21, y, 55, 2.0, z)
// 2 hard wired ways to filter by type
listOfAnys.collect{ case s:String=>s} //> res0: List[String] = List(x, y, z)
listOfAnys.filter{_.isInstanceOf[String]} //> res1: List[Any] = List(x, y, z)
// more generic manner
@fancellu
fancellu / IteratorExists.scala
Last active February 25, 2016 13:45
Iterator/exist example of short circuiting without using return. Also what happens when you use toStream and view
val li=List(1,2,3,4,5,6,7,8)
val nums=for {
i<-li.toIterator
_=println(" "+i)
} yield i
// note how we bail early
println(nums.exists{_>3})
// nums is now an iterator, so is mutable, we continue from where we left off
import scala.collection.generic.FilterMonadic
object stuff {
def functor[X, Y,F[X] <: Traversable[X]](f: X => Y) =
(arg: F[X]) => arg.map(f) //> functor: [X, Y, F[X] <: Traversable[X]](f: X => Y)F[X] => Traversable[Y]
val f=(x:Int)=>x*2 //> f : Int => Int = <function1>
val f2=functor{ f } //> f2 : Traversable[Int] => Traversable[Int] = <function1>
object stuff {
println("xx") //> xx
trait Plus[A] {
def plus(a1: A, a2: A): A
}
trait PlusOp[A] {
val F: Plus[A]
@fancellu
fancellu / Fut3.scala
Created March 16, 2016 11:11
Futures and comprehensions
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object Fut3 extends App {
def logF(st: String) = Future {
Thread.sleep(500)
@fancellu
fancellu / Mustache.scala
Last active December 30, 2016 21:03
Running JS from JDK1.8 (mustache.js example and raw example with binding)
package nashorn
import javax.script.{Invocable, ScriptEngineManager, SimpleBindings}
import java.io.InputStreamReader
import scala.collection.JavaConverters._
object Mustache {
private val engineManager = new ScriptEngineManager()
val engine = engineManager.getEngineByName("nashorn")
val invocable = engine.asInstanceOf[Invocable]
@fancellu
fancellu / GetStarted.scala
Last active April 27, 2016 13:11
Some simple Kamon code
package kamonstuff
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import kamon.Kamon
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot
package akkastream
import akka.stream._
import akka.stream.scaladsl._
import akka.NotUsed
import akka.actor._
import scala.concurrent._
import scala.concurrent.duration._
import akka.util.ByteString
@fancellu
fancellu / HttpServerMetrics.java
Created May 10, 2016 15:12
HTTP server monitoring for Kamon, wraps a Flow on HttpRequest
public class HttpServerMetrics extends GenericEntityRecorder {
public static final Logger log = LoggerFactory.getLogger(HttpServerMetrics.class);
public static final EntityRecorderFactory<HttpServerMetrics> FACTORY = EntityRecorderFactory$.MODULE$.create("akka-http-server", HttpServerMetrics::new);
private final Counter statusInformational;
private final Counter statusSuccess;
private final Counter statusRedirection;
private final Counter statusClientError;
private final Counter statusServerError;
@fancellu
fancellu / Result.scala
Created May 22, 2016 17:39
Simple example of adding map to a type, so it acts as a functor, as opposed to using Cats Functors with annoying invariance
sealed trait Result[+A] {
def map[B](f: A => B): Result[B] =
this match {
case success@Success(value) => Success(f(value))
case warning@Warning(value, message) => warning.copy(value=f(value))
case failure@Failure(message) => failure
}
}
final case class Success[A](value: A) extends Result[A]