Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Mortimerp9 / gist:6952000
Created October 12, 2013 16:33
An alternative Factory pattern in Scala to compare with the one presented in [Design Patterns in Scala](http://pavelfatin.com/design-patterns-in-scala/). This one involves a bit more boilerplate, but will not compile if you ask for an unknown type and is easier to extend.
trait Animal
private class Dog extends Animal
private class Cat extends Animal
trait Wanna
case object WannaCat extends Wanna
case object WannaDog extends Wanna
trait AnimalFactory[Wanna] {
def create(): Animal
@Mortimerp9
Mortimerp9 / Retry.scala
Last active July 3, 2022 22:35
A retry implementation for Scala, a bit of explanations here: http://pierreandrews.net/posts/retry-fail-scala.html
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.blocking
import scala.concurrent.duration.Deadline
import scala.concurrent.duration.Duration
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.DurationLong
import scala.concurrent.future
import scala.concurrent.promise
@Mortimerp9
Mortimerp9 / readerwithtooling.scala
Created April 14, 2013 22:14
An implementation of the Reader Monad in scala, with correct type variance and some implicit utils to simplify the daily use of Readers, In particular with Future.
/**
* A monad to abstract dependencies in the code, see https://coderwall.com/p/kh_z5g
*/
object Reader {
/**
* an implicit to convert a function A => B in a Reader[A, B]
*/
implicit def reader[C, R](block: C => R): Reader[C, R] = Reader(block)
@Mortimerp9
Mortimerp9 / check_kafkalag.sh
Last active December 28, 2021 15:14
Check kafka lag and queue growth with nagios/nrpe and KafkaOffsetMonitor
#!/bin/bash
#inspired by https://github.com/vide/nagios-scripts/blob/master/check_kafka_lag.sh
function printHelp() {
cat >&2 <<EOF
$@
--warning|-w Warning threshold
--critical|-c Critical threshold
@Mortimerp9
Mortimerp9 / dabblet.css
Created October 18, 2012 01:24
CSS bar graph with Positive and Negative values
/**
* CSS bar graph with Positive and Negative values
*/
.graph {
width: 100px;
border: 1px solid #aeaeae;
background-color: #eaeaea;
}
/* Start of "Micro clearfix" */
@Mortimerp9
Mortimerp9 / gist:5649109
Created May 25, 2013 13:50
"cheap" implementation of an immutable IndexedSeq backed by an array. `b` looks like an Array according to the types, but can't be modified nor cast back to a mutable Array.
import scala.reflect.ClassTag
import scala.collection.mutable.WrappedArray
import scala.collection.mutable.ArrayLike
def ASeq[T](elt: T*)(implicit ct: ClassTag[T]): IndexedSeq[T] = {
val a = elt.toArray.clone
a.deep.asInstanceOf[IndexedSeq[T]]
}
val a = Array(1,2,3) //> a : Array[Int] = Array(1, 2, 3)
@Mortimerp9
Mortimerp9 / ReaderCovariance.scala
Last active October 13, 2017 16:12
Sample code for the following post https://coderwall.com/p/pdrz7q
object test {
/**
* The companion object
*/
object Reader {
/**
* automatically wrap a function in a reader
*/
@Mortimerp9
Mortimerp9 / RefreshEvery.scala
Created May 2, 2014 21:07
Sort of a lazy val, but that refreshes itselfs every so often; or a temporary cache for a value T, will be refreshed at a minimun at the specified interval.
import com.twitter.util.{Duration, Time}
/**
* Sort of a lazy val, but that refreshes itselfs every so often; or a temporary cache for a value T,
* will be refreshed at a minimun at the specified interval.
* In practice, the refresh is done only when the value is accessed, so there are no guarantees
* to when it will actually be refreshed. You can just be sure that it won't be refreshed if two calls
* are made within `every`.
*/
class RefreshEvery[T](every: Duration)(refresh: => T) {
@Mortimerp9
Mortimerp9 / ScalaJson.scala
Created January 11, 2014 14:38
Jackson, only extract a single field from a json inputstream/string in scala, without parsing everything. I often deserialize whole objects with jackson to only access a field out of the json. This doesn't seem very efficient, Jackson provides a streaming API that can be used to make it a bit faster.
import com.fasterxml.jackson.core._
object ScalaJson {
lazy val factory = new JsonFactory()
implicit class JsonString(json: String) {
def jsonField(name: String) = ScalaJson.getFieldOnly(name, json)
}
class MyActor extends Actor {
var driver: ActorRef = null
val identifyId = //get a unique identifyId for that driverUrl
override def preStart() {
val driverSel = context.actorSelection(driverUrl)
driverSel ! Identify(identifyId)
}