Skip to content

Instantly share code, notes, and snippets.

View rahilb's full-sized avatar

Rahil Bohra rahilb

View GitHub Profile
@rahilb
rahilb / Cache.scala
Created March 16, 2015 16:21
In memory ttl cache
class InMemoryTTLCache[T <: AnyRef](settings: TTLCacheSettings, zeroFun: String => T) extends Cache[T] {
val underlying = CacheBuilder.newBuilder()
.initialCapacity(30)
.expireAfterWrite(settings.maxAgeInSeconds, TimeUnit.SECONDS)
.build[String, T](new CacheLoader[String, T] {
override def load(key: String): T = zeroFun(key)
})
override def get(cacheKey: String): Option[T] = Option(underlying.get(cacheKey))
override def set(cacheKey: String, value: T): Unit = underlying.put(cacheKey, value)
override def delete(cacheKeys: Seq[String]): Unit = cacheKeys.foreach(underlying.invalidate)
@rahilb
rahilb / authenticator.lua
Created March 18, 2015 14:41
lua token authenticator for nginx
-- an openresty/nginx authenticator that checks bearer tokens with
-- an ID service for use with `access_by_lua_file` nginx directive
local http = require "resty.http"
local hc = http:new()
function abandon_request(status_code, response_body)
ngx.header["WWW-Authenticate"] = "Bearer"
ngx.status = status_code
ngx.say(response_body)
@rahilb
rahilb / Foo.scala
Created October 29, 2015 15:54
overwrite object values
import sun.misc.Unsafe
object Foo {
val len: String => Int = {s => s.length}
def bar(s: String) = s.length
}
Foo.len("foo") // 3
val unsafe = {
object test extends App {
object Bar extends Dynamic {
def applyDynamic(name: String)(args: Any*) = {
???
}
}
val b = Bar.foo("any")
@rahilb
rahilb / Scratch.scala
Created November 25, 2015 16:26
Functor Abstraction Pattern
import cats.Functor
trait ImplicitInstances {
implicit val OptionInstance: Functor[Option] = new Functor[Option] {
override def map[A, B](fa: Option[A])(f: (A) => B): Option[B] = fa.map(f)
}
}
final class FuncAbs[M[_]] {
def double(s: M[String])(implicit functor : Functor[M]) = functor.map(s)(str => str + str)
@rahilb
rahilb / Awesomez.scala
Last active December 8, 2015 09:57
Scalaz solutions for scala warts
import scalaz._
import Scalaz._
// Option#getOrElse is ugly
val two = None | 2
// if else & bool match are ugly
val three = true.fold(t = 3, f = 0)
// nested method calls can become ugly & hard to read
@rahilb
rahilb / semantic-versioner.groovy
Created January 15, 2016 10:24
Semantic Versioning
/**
* Semantic Versioning Script for Jenkins
*
* For this to work commit messages must contain one of the characters [!, +, =]
*
* ! is a breaking change and will increment the major version number.
* e.g. 1.1.256 will become 2.0.0
*
* + is a backward compatible new feature and will increment the minor version number.
* e.g. 1.2.24 will become 1.3.0
java -ea \
$SBT_OPTS \
$JAVA_OPTS \
-Djava.net.preferIPv4Stack=true \
-XX:+AggressiveOpts \
-XX:+UseParNewGC \
-XX:+UseConcMarkSweepGC \
-XX:+CMSParallelRemarkEnabled \
-XX:+CMSClassUnloadingEnabled \
-XX:ReservedCodeCacheSize=128m \
import eu.fakod.neo4jscala.{TypedTraverser, SingletonEmbeddedGraphDatabaseServiceProvider, Neo4jWrapper}
import sys.ShutdownHookThread
trait ProgramBase {
val name: String
}
case class Series(name: String) extends ProgramBase
case class Episode(name: String) extends ProgramBase
@rahilb
rahilb / comments.md
Last active November 7, 2016 15:35
Onzo

Implemented using typeclasses.

Implementation:

  1. Define the problem with types :
  • Encoder[A] & Decoder[A]. Encoder takes an A, and produces an AttributeValue. Decoder does the opposite.
  • KeyLike[A] coproduct for things that can be keys.
  • NamedKeyLike[A] a key that has a name, an encoder, and a decoder.
  • PrimaryKey - I suppose it's the Primary Key of the table