Skip to content

Instantly share code, notes, and snippets.

View rbuckland's full-sized avatar

Ramon Buckland rbuckland

  • Inosion
  • Sydney Australia
View GitHub Profile
def doWork[A: Numeric, B: Numeric](values: Seq[Option[A]], op: String): Option[B] = {
import spire.implicits._
val v = values.flatten
if (v.isEmpty) {
None
} else {
op match {
case "sum" => Some(v.qsum) // returns A
case "avg" | "avg" => Some(v.qsum / v.length) // returns Double
case "max" => Some(v.qmin) // A
scala> val x:Double = Long.MaxValue // x: Double = 9.223372036854776E18 (9223372036854775807)
scala> x == Long.MaxValue // Boolean = true
scala> x.longValue == Long.MaxValue // Boolean = true
scala> val y = 1234567890123456789L // y: Long = 1234567890123456789 // less than Long.MaxValue
scala> y < x // Boolean = true
scala> val z:Double = y // z: Double = 1.23456789012345677E18
scala> z == y // Boolean = true
scala> z.longValue == y // Boolean = false // why ? MaxValue fits in the Double
@rbuckland
rbuckland / spire-maths-gist.scala
Created February 24, 2015 23:28
compiler error with generics
def safeSum[A: Numeric](as: Seq[A]):Option[A] =
if (as.isEmpty) None else Some(as.qsum)
def safeDiv[A: Numeric](a:Option[A], b:Option[A]):Option[Double] = (a,b) match {
case (None,_) | (_,Some(0)) | (_,None) => None
case (Some(x),Some(y)) => Some((x / y).toDouble())
}
/**
@rbuckland
rbuckland / AddNewRegistrationSpec.scala
Created April 23, 2014 20:25
JacksonUnmarshallingSpec for Spray. Just making sure that it is all still working.
package com.soqqo.luap.web.marshalling
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import io.straight.fw.spray.marshalling.JacksonUnmarshaller
import com.soqqo.luap.messages.AddNewAccountTenantRegistration
import spray.httpx.unmarshalling._
import spray.util._
import spray.http._
import spray.http.MediaTypes.`application/json`
@rbuckland
rbuckland / sealed-abstract-case-class-copy for Command messages.scala
Last active September 30, 2021 10:36
Sometimes you just want to call the copy method for the same "parameters", but not care about what underlying type it is.Because case class .copy() methods are auto generated per case class, you can't call copy on the abstract class, so this approach uses some scala foo to generate a scala Function that generates the correct magic for you.
package com.soqqo.luap.messages
import com.soqqo.luap.messages.ContextMetaData
import com.soqqo.luap.messages.NoContextMetaData
import com.soqqo.luap.messages.RegisterNewPerson
import io.straight.fw.model.Uuid
import scala.reflect.api.JavaUniverse
import scala.reflect.api
import io.straight.fw.jackson.JacksonBindingSupport._
import scala.Some
@rbuckland
rbuckland / CampusMessages.scala
Created April 22, 2014 20:49
Sample to show / explain CommandMessage Enrichment of MetatData
import org.joda.time.DateTime
import com.soqqo.luap.model.common.Address
import io.straight.fw.model.Uuid
import java.util.Date
import akka.actor.ActorRef
/*
* file: CampusMessages.scala
*/
@rbuckland
rbuckland / H2DatabaseGenerator.scala
Created April 1, 2014 19:24
Creates a ZIP file from an XLS Spreadsheet uploaded to the client.Uses some magic foo and a spinkling of fun - see this at http://ete.straight.io
/**
* @author rbuckland
*/
trait H2DatabaseGenerator extends CommonHttpService {
val generateH2DbRoute = pathPrefix("h2generator") {
// headerValueByName("Upload-File-Name") { filename =>
// headerValueByName("DatabaseName") { dbname =>
@rbuckland
rbuckland / SbtConfigurationforHeroku.scala
Last active March 12, 2016 11:39
This is some configuration I have used to run a a Scala base SBT webapp on Heroku. It mirrors that Java Maven way of using jetty-runner. The Heroku suggestion of using "sbt-start-script" is just bad.
/*
This is some configuration I have used to run a a Scala base SBT webapp on Heroku.
Heroku suggest using startscript. But this doesn;t work on Mac OSX *readline, it also uses an embedded jetty verison, and it uses the jetty distribution.
Instead, Heroku suggest using jetty-runner with Java Maven projects (and I liked that a lot) so I am replicating that setup here.
In short:
1. Ensure you have the xsbt-web-plugin