Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / XMLCR.scala
Created Feb 28, 2014
Fix for better control over XML output. Hope this little tip helps.
View XMLCR.scala
val that= <that>x</that>
val thatcr= Seq(that,Text("\n"))
val xml= <root>
{ for (x<-1 to 4) yield that }
{ for (x<-1 to 4) yield
@fancellu
fancellu / Debbie_Harry.xquery
Created Mar 14, 2014
Example of MarkLogic 7 Semantic triple query, emitting as custom XML
View Debbie_Harry.xquery
xquery version "1.0-ml";
let $out:=sem:sparql('
SELECT *
WHERE { <http://dbpedia.org/resource/Debbie_Harry> ?p ?o }
')
for $i in $out
let $p:=map:get($i,"p")
let $o:=map:get($i,"o")
@fancellu
fancellu / Rational.scala
Last active Aug 29, 2015
Nice OO Scala example for a talk for Harvey Nash
View Rational.scala
object Rational extends App {
case class Rational(n: Int, d: Int=1) extends Ordered[Rational]{
require(d != 0)
private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
private val g = gcd(n.abs, d.abs)
val numer = n / g
val denom = d / g
def + (that: Rational) = Rational( numer * that.denom + that.numer * denom, denom * that.denom)
@fancellu
fancellu / ThrottledProcessor.scala
Created Apr 1, 2014
ThrottledProcessor.scala (little example for an intro to Scala talk)
View ThrottledProcessor.scala
import akka.actor.ActorSystem
import scala.concurrent.duration._
import akka.actor.ActorDSL._
object ThrottledProcessor extends App {
implicit val system = ActorSystem("ThrottledProcessor")
import system.dispatcher
case object Dump
case object Process
@fancellu
fancellu / TimestampMacro.scala
Created Jul 22, 2014
Simple Macro example, allows you to have a string which shows when last compiled. Remember that your main has to be in a different project to the macro (for the moment)
View TimestampMacro.scala
package com.felstar.macros.timestamp
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
object TimestampMacro {
def timestampString: String = macro timestampMacro
def timestampMacro(c: Context): c.Tree = {
import c.universe._
@fancellu
fancellu / ExceptionDig.scala
Last active Aug 29, 2015
Various ways to recurse down Exception causes in Scala
View ExceptionDig.scala
// Using vars, lots of local mutation
def getThemVar(th:Throwable)={
var now=th
var li=List[Throwable](now)
var cause=now.getCause
while (cause!=null){
li=cause::li
now=cause
cause=now.getCause
}
View CaseMapApp.scala
package playpen
object CaseMapApp extends App {
import caseMapper.CaseMapper._
case class Address(firstLine:String, postcode:String, country:String)
case class Person(name: String, age: Int, address:Address)
val here=Address("26 Duncoding","KT17 4LX","UK")
@fancellu
fancellu / e4s1.scala
Last active Aug 29, 2015
Example of using ES elastic4s with CaseMapper macros
View e4s1.scala
package es
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._
object e4s1 extends App {
View ThrottledProcessor2.scala
import akka.actor.ActorSystem
import scala.concurrent.duration._
import akka.actor.ActorDSL._
// Better than ThrottledProcessor as it fires immediately if idle, and doesn't send Process messages repeatedly
object ThrottledProcessor2 extends App {
implicit val system = ActorSystem("ThrottledProcessor")
@fancellu
fancellu / C1.scala
Created Oct 3, 2014
Tiny Chronicle Map + Scala example
View C1.scala
package chroniclemap
import net.openhft.chronicle.map._
import java.io.File
object C1 extends App {
val tmp = System.getProperty("java.io.tmpdir");
val pathname = tmp + "/mychronicle.dat";
You can’t perform that action at this time.