Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / XMLCR.scala
Created February 28, 2014 17:48
Fix for better control over XML output. Hope this little tip helps.
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 March 14, 2014 15:28
Example of MarkLogic 7 Semantic triple query, emitting as custom XML
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 August 29, 2015 13:57
Nice OO Scala example for a talk for Harvey Nash
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 April 1, 2014 18:34
ThrottledProcessor.scala (little example for an intro to Scala talk)
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 July 22, 2014 09:43
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)
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._
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 August 29, 2015 14:05
Example of using ES elastic4s with CaseMapper macros
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 {
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 October 3, 2014 19:56
Tiny Chronicle Map + Scala example
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";
@fancellu
fancellu / combine.scala
Last active August 29, 2015 14:17
Combines 2 maps of sequences. Have seen this asked a few times. This is a fairly concise solution.
def combine[K,V](map1:Map[K,Seq[V]], map2:Map[K,Seq[V]]) =
(map1.toList ++ map2.toList).groupBy(_._1).mapValues(_.map(_._2).flatten).map(identity)