Skip to content

Instantly share code, notes, and snippets.

View hipjim's full-sized avatar
🌴
On vacation

Cristian Popovici hipjim

🌴
On vacation
  • Timisoara
View GitHub Profile
@hipjim
hipjim / gist:8027043
Created December 18, 2013 18:11
basic cake scala
/**
* Created by cristipopovici on 12/18/13.
*/
trait LifeCycle {
def startup(): Unit
def shutdown(): Unit
}
@hipjim
hipjim / scala type class
Last active January 4, 2016 11:28
scala type class
trait M[T] {
def plus(x:T,y:T): T
def zero: T
}
implicit object IntM extends M[Int] {
def plus(x:Int, y:Int) = x + y
def zero = 0;
}
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.json._
// you need this import to have combinators
import play.api.libs.functional.syntax._
object Application extends Controller {
@hipjim
hipjim / gist:8899607
Last active August 29, 2015 13:56
OpenTsdb utils
OpenTsDb Usefull queries
* show all metrics
GET /suggest?type=metrics
* put datapoint
PUT /api/put
{
"timestamp" : 1391952958,
@hipjim
hipjim / gist:10262758
Last active August 29, 2015 13:58
recursive js
var power = function (base, exponent) {
if (exponent === 1) {
return base;
}
return base * power(base, exponent - 1);
};
var fibb = function (n) {
if (n === 0 || n === 1) {
return 1;
@hipjim
hipjim / gist:6803401b23aa0a9b7444
Created June 19, 2014 20:01
play framework basic auth filter
import play.api.mvc.{Results, SimpleResult, RequestHeader, Filter}
import scala.concurrent.Future
case class BasicAuthFilter() extends Filter {
private[this] val username = "joe"
private[this] val password = "secret"
override def apply(f: (RequestHeader) => Future[SimpleResult])(rh: RequestHeader): Future[SimpleResult] = {
rh.headers.get("Authorization").map { basicAuth =>
val (user, pass) = decodeBasicAuth(basicAuth)
type BuildingStringMapper = Building => String
val buildingCode = (b: Building) => b.getBuildingCode
// works OK
val buildingHeaders = Map[String, BuildingStringMapper](
"Building Code" -> buildingCode
)
// does not work. Can I define function literals as inline map values??
val buildingHeaders = Map[String, BuildingStringMapper](
@hipjim
hipjim / quicksort.scala
Created March 25, 2016 15:31
scala quicksort
def quickSort(l:List[Int]):List[Int] = l match {
case Nil => l
case head :: tail =>
val (l,r) = tail.partition(_ < head)
quickSort(l) ::: head :: quickSort(r)
}
@hipjim
hipjim / list.scala
Last active October 3, 2016 14:03
scala list impl
import scala.annotation.tailrec
/**
* Created by CristianPopovici on 9/29/2016.
*/
sealed trait List[+T] {
@tailrec
final def forEach(f:T => Unit): Unit =
this match {
@hipjim
hipjim / pdfReplacePage.scala
Last active October 4, 2016 16:02
PdfReplacePage.scala
object PdfReplace extends App {
val pageNumberToReplace = 22
val sourcePathStr = "C:\\..."
val destPathStr = "C:\\..."
val pageToReplacePath = "C:\\"
val sourceInput = new FileInputStream(new File(sourcePathStr))
val pageToReplaceInput = new FileInputStream(new File(pageToReplacePath))