Skip to content

Instantly share code, notes, and snippets.

View AlexRogalskiy's full-sized avatar
🛰️
Work on stuff that matters

Alexander AlexRogalskiy

🛰️
Work on stuff that matters
View GitHub Profile
@AlexRogalskiy
AlexRogalskiy / java_extension.sql
Created December 19, 2020 20:12
Java extension for Postgres
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' );
@AlexRogalskiy
AlexRogalskiy / reader_monad.scala
Created December 19, 2020 20:15
Scala reader monad
/**
* 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)
@AlexRogalskiy
AlexRogalskiy / reader_mapping_monad.scala
Created December 19, 2020 20:20
Scala reader monad mapping
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]
@AlexRogalskiy
AlexRogalskiy / range_monoid.scala
Last active December 19, 2020 20:26
Scala monoid range
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)
@AlexRogalskiy
AlexRogalskiy / refreshing_cache.scala
Created December 19, 2020 20:28
Scala refreshing cache
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) {
@AlexRogalskiy
AlexRogalskiy / retry.scala
Last active December 19, 2020 20:29
Scala retry
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
@AlexRogalskiy
AlexRogalskiy / sequence.scala
Created December 19, 2020 20:30
Scala sequence
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)
@AlexRogalskiy
AlexRogalskiy / memoizer.scala
Created December 19, 2020 20:30
Scala memoizer
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
@AlexRogalskiy
AlexRogalskiy / akka_converter.scala
Created December 19, 2020 20:31
Scala akka converter
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).
@AlexRogalskiy
AlexRogalskiy / try_future_mapping.scala
Created December 19, 2020 20:33
Try to Future mapping
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)
}