Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
val root = <numbers>
<number>1</number>
<number>2</number>
<number>3</number>
</numbers>
val root2= <numbers>
{for (x<- 1 to 3) yield <number>{x}</number>}
</numbers>
@fancellu
fancellu / MyQuote.scala
Last active June 8, 2016 22:11
Very simple example of Scala "string" Interpolation (more than just strings though)
//http://docs.scala-lang.org/overviews/core/string-interpolation.html
implicit class MyQuote(ctx:StringContext){
def x(args:String*):String=ctx.parts.zip(args).map{case (p,a)=>s"$p$a"}.mkString("")
}
val string="STRINGY"
val string2="STRINGY2"
println(x"Part1 $string Part2 $string2 Part3 $string")
@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._
@fancellu
fancellu / ShapelessCoproduct.scala
Last active February 20, 2023 10:02
Examples with Shapeless 2.0, easier to understand from examples. Taken from docs, more examples/comments added etc
// Coproduct is extension of Either concept, to N multually exlusive choices
type ISB = Int :+: String :+: Boolean :+: CNil
val isb = Coproduct[ISB]("foo") //> isb : qaaz.ISB = foo
isb.select[Int] //> res0: Option[Int] = None
isb.select[String] //> res1: Option[String] = Some(foo)
@fancellu
fancellu / ExceptionDig.scala
Last active December 13, 2023 06:07
Various ways to recurse down Exception causes in 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
}
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 / CaseClassMapping.scala
Created October 21, 2014 12:16
Slick 2.x examples with MariaDB
package slick.lifted
import scala.slick.driver.MySQLDriver.simple._
import slick._
object CaseClassMapping extends App {
// the base query for the Users table
val users = TableQuery[Users]