This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET pljava.libjvm_location TO '/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server/libjvm.so'; | |
CREATE EXTENSION pljava; | |
GRANT USAGE ON LANGUAGE java TO postgres; | |
ALTER DATABASE postgres SET pljava.libjvm_location FROM CURRENT; | |
SELECT sqlj.install_jar( 'file:///tmp/simple-java-function/target/simple-java-function.jar','jfunctions', true ); | |
SELECT sqlj.set_classpath( 'public', 'jfunctions' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A monad to abstract dependencies in the code, see https://coderwall.com/p/kh_z5g | |
*/ | |
object Reader { | |
/** | |
* an implicit to convert a function A => B in a Reader[A, B] | |
*/ | |
implicit def reader[C, R](block: C => R): Reader[C, R] = Reader(block) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection._ | |
import scala.collection.generic._ | |
import scala.concurrent.{ Future, ExecutionContext } | |
/** | |
* a Typeclass representing a class that can map and flatMap (collections, Option, Future..). | |
* effectively, it's a Monad without enforcing the axioms of a Monad. | |
*/ | |
trait CanMap[A, B, M[_]] { | |
def map(l: M[A])(f: A => B): M[B] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MonoidRange { | |
case class MonoidRangeBuilder[T](from: T, to: T, incl: Boolean)(implicit mo: Monoid[T], ordering: Ordering[T]) { | |
def by(step: T) = Stream.iterate(from) { previous: T => | |
previous + step | |
}.takeWhile { x => | |
if (incl) { | |
ordering.lteq(x, to) | |
} else { | |
ordering.lt(x, to) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twitter.util.{Duration, Time} | |
/** | |
* Sort of a lazy val, but that refreshes itselfs every so often; or a temporary cache for a value T, | |
* will be refreshed at a minimun at the specified interval. | |
* In practice, the refresh is done only when the value is accessed, so there are no guarantees | |
* to when it will actually be refreshed. You can just be sure that it won't be refreshed if two calls | |
* are made within `every`. | |
*/ | |
class RefreshEvery[T](every: Duration)(refresh: => T) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.Await | |
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
import scala.concurrent.blocking | |
import scala.concurrent.duration.Deadline | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.duration.DurationInt | |
import scala.concurrent.duration.DurationLong | |
import scala.concurrent.future | |
import scala.concurrent.promise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.ClassTag | |
import scala.collection.mutable.WrappedArray | |
import scala.collection.mutable.ArrayLike | |
def ASeq[T](elt: T*)(implicit ct: ClassTag[T]): IndexedSeq[T] = { | |
val a = elt.toArray.clone | |
a.deep.asInstanceOf[IndexedSeq[T]] | |
} | |
val a = Array(1,2,3) //> a : Array[Int] = Array(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.duration.FiniteDuration | |
/** | |
* A map-like object (with only the most basic functions implemented) that is thread safe but doesn't is not locking. | |
* The size of the map is limited to a maximum bound and the "oldest" entries are thrown out. | |
*/ | |
class LockFreeBoundedCache[K, E](maxSize: Int) { | |
import java.util.concurrent.atomic.AtomicReference | |
import scala.collection.mutable.LinkedHashMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.duration.{Duration => ScalaDuration} | |
import scala.concurrent.{ExecutionContext => ScalaExecutionContext, Future => ScalaFuture, Promise => ScalaPromise} | |
import scala.util.{Success, Failure} | |
import akka.dispatch.{ExecutionContext => AkkaExecutionContext, Future => AkkaFuture, Promise => AkkaPromise} | |
import akka.util.{Duration => AkkaDuration} | |
/** | |
* Some compatibility implicit conversions to deal with akka as if it might already be a newer version | |
* (using scala interfaces instead of akka). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.{Failure, Success, Try} | |
import scala.concurrent.future | |
implicit def tryToFuture[T](t: Try[T]): Future[T] = t match { | |
case Success(s) => Future.successful(s) | |
case Failure(t) => Future.failed(t) | |
} |