Skip to content

Instantly share code, notes, and snippets.

@caiorss
caiorss / detect-jdbc-driver-name.scala
Created October 25, 2016 01:53 — forked from takezoux2/detect-jdbc-driver-name.scala
Detect jdbc driver name from jdbc url.
def detectDriverNameFromUrl(url : String) : Option[String] = {
val splits = url.split(":")
if(splits.length < 2){
return None
}
if(splits(0) != "jdbc"){
return None
}
@caiorss
caiorss / gist:3db6d420423170476ae6b1e0559ca5aa
Created October 25, 2016 01:47 — forked from denen99/gist:6765084
Coursera week 2 scala tests
package funsets
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
/**
* This class is a test suite for the methods in object FunSets. To run
* the test suite, you can either:
@caiorss
caiorss / GADTs.scala
Created October 25, 2016 01:42 — forked from jroesch/GADTs.scala
Scala GADTs
/* http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#gadt
Generalized Algebraic Datatypes in Scala */
trait Term[A]
case class Lit(x: Int) extends Term[Int]
case class Succ(x: Term[Int]) extends Term[Int]
case class IsZero(x: Term[Int]) extends Term[Boolean]
case class If[A](guard: Term[Boolean], t: Term[A], y: Term[A]) extends Term[A]
case class Pair[A, B](x: Term[A], y: Term[B]) extends Term[(A,B)]
case class Circle(radius: Double) {
def area = radius * radius * Math.PI
}
case class Rectangle(side: Double) {
def area = side * side
}
case class Triangle(base: Double, height: Double) {
def area = 0.5d * base * height
// And by the way, please feel free to contribute, correct errors, etc!
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
object Functor {
val ListFunctor: Functor[List] =
new Functor[List] {
def fmap[A, B](f: A => B) =
@caiorss
caiorss / PaypalTransaction.scala
Created October 25, 2016 01:31 — forked from mslinn/PaypalTransaction.scala
Attempt to persist entities with >22 fields via Slick. Cannot find documentation on how nested tuples are supposed to work; tried creating two helper classes (CustomerAddress and PaymentDetail). Compiler error is: scala: overloaded method value <> with alternatives: [R(in method <>)(in method <>)(in method <>)(in method <>)(in method <>)(in meth…
package com.micronautics.paypal
import java.util.{ List, Map }
import collection.JavaConversions._
import collection.immutable.HashMap
import controllers.Email
import org.joda.time.DateTime
import slick.driver.MySQLDriver.simple._
import java.util
/*
* The trait Nat is provided by the text.
*/
trait Nat {
def isZero: Boolean
def predecessor: Nat
def successor: Nat
def +(that: Nat): Nat
def -(that: Nat): Nat
@caiorss
caiorss / MOption.scala
Created October 25, 2016 01:29 — forked from ElectricCoffee/MOption.scala
Adds map and flatMap as extension methods to Scala, for those who don't want/need scalaz for their project, is fully compatible with for-comprehensions
package extension.monad
trait Monad[A, M[_]] {
// >>= :: Monad m => m a -> (a -> m b) -> m b
def flatMap[B](input: A => M[B]): M[B] // AKA "bind"
}
trait Functor[A, F[_]] {
// fmap :: Functor f => (a -> b) -> f a -> f b
def map[B](input: A => B): F[B] // AKA "fmap"
@caiorss
caiorss / SystemProperties.scala
Last active October 25, 2016 01:37
Scala - Java interoperability and integration exploration.
// Print Systme properties
//
//
scala> System.getProperty("path.separator")
res29: String = :
scala> System.getProperty("os.name")
res30: String = Linux
scala> System.getProperty("os.arch")
// AGDT - Algebraic Data type
// List implementation
//
sealed trait AList[+A]
case object ANil extends AList[Nothing]
case class Cons[A](head: A, tail: AList[A]) extends AList[A]
scala> Cons(1, Cons(10, Cons(30, ANil)))
res74: Cons[Int] = Cons(1,Cons(10,Cons(30,ANil)))