Skip to content

Instantly share code, notes, and snippets.

View abhandaru's full-sized avatar

Adu Bhandaru abhandaru

View GitHub Profile
@abhandaru
abhandaru / Main.scala
Last active August 31, 2020 19:27
First draft of a functional-style RetryPolicy implementation.
import scala.concurrent._
import scala.concurrent.duration._
import java.net.SocketTimeoutException
object Postgres {
// Example for postgres connection timeout retry policy
case class PgConnectionPolicy(n: Int) extends RetryPolicy {
override def attempt(rpc: => Future[T]) = (PgConnectionPolicy(n - 1), rpc)
override def retryable[E <: Throwable] = {
case _: SocketTimeoutException => n > 0
trait Foo { def i: Int }
trait Foo2[T] { def sq(t: T): Int }
trait Bar[T] {
def cube(t: T): Int
}
object Bar {
implicit def foo[T <: Foo: Foo2]: Bar[T] = {
val ctx = implicitly[Foo2[T]]
import language.experimental.macros
import scala.reflect.macros.whitebox.Context
// Macro for iterating sealed trait enums
// https://stackoverflow.com/questions/13671734/iteration-over-a-sealed-trait-in-scala
object SealedEnum {
def values[A]: Set[A] = macro values_impl[A]
def values_impl[A: c.WeakTypeTag](c: Context) = {
import c.universe._
// Consider pulling in Algebird. See full implementation:
// https://github.com/twitter/bijection/blob/0.8.0/bijection-core/src/main/scala/com/twitter/bijection/Bijection.scala
@implicitNotFound(msg = "Could not find a BytesBijection for ${T}")
trait Bijection[A, B] extends (A => B) {
def invert(b: B): A
def andThen[C](g: Bijection[B, C]): Bijection[A, C] = {
val self = this
new Bijection[A, C] {
override def apply(a: A) = g(self.apply(a))
@abhandaru
abhandaru / Line.scala
Created August 15, 2019 21:23
Line with intersection method
case class Vec(i: Double, j: Double) {
def unary_- = Vec(-i, -j)
def +(v: Vec): Vec = Vec(i + v.i, j + v.j)
def -(v: Vec): Vec = Vec(i - v.i, j - v.j)
def *(s: Double): Vec = Vec(i * s, j * s)
def cross(v: Vec): Int = i * v.j - j * v.i
}
We can't make this file beautiful and searchable because it's too large.
UNITID,OPEID,OPEID6,INSTNM,CITY,STABBR,INSTURL,NPCURL,HCM2,PREDDEG,HIGHDEG,CONTROL,LOCALE,HBCU,PBI,ANNHI,TRIBAL,AANAPII,HSI,NANTI,MENONLY,WOMENONLY,RELAFFIL,SATVR25,SATVR75,SATMT25,SATMT75,SATWR25,SATWR75,SATVRMID,SATMTMID,SATWRMID,ACTCM25,ACTCM75,ACTEN25,ACTEN75,ACTMT25,ACTMT75,ACTWR25,ACTWR75,ACTCMMID,ACTENMID,ACTMTMID,ACTWRMID,SAT_AVG,SAT_AVG_ALL,PCIP01,PCIP03,PCIP04,PCIP05,PCIP09,PCIP10,PCIP11,PCIP12,PCIP13,PCIP14,PCIP15,PCIP16,PCIP19,PCIP22,PCIP23,PCIP24,PCIP25,PCIP26,PCIP27,PCIP29,PCIP30,PCIP31,PCIP38,PCIP39,PCIP40,PCIP41,PCIP42,PCIP43,PCIP44,PCIP45,PCIP46,PCIP47,PCIP48,PCIP49,PCIP50,PCIP51,PCIP52,PCIP54,DISTANCEONLY,UGDS,UGDS_WHITE,UGDS_BLACK,UGDS_HISP,UGDS_ASIAN,UGDS_AIAN,UGDS_NHPI,UGDS_2MOR,UGDS_NRA,UGDS_UNKN,PPTUG_EF,CURROPER,NPT4_PUB,NPT4_PRIV,NPT41_PUB,NPT42_PUB,NPT43_PUB,NPT44_PUB,NPT45_PUB,NPT41_PRIV,NPT42_PRIV,NPT43_PRIV,NPT44_PRIV,NPT45_PRIV,PCTPELL,RET_FT4_POOLED_SUPP,RET_FTL4_POOLED_SUPP,RET_PT4_POOLED_SUPP,RET_PTL4_POOLED_SUPP,PCTFLOAN,UG25ABV,MD_EARN_WNE_P10,GT_25K_P6,GRAD_DEBT_MDN_SUPP,GR
return {
["Abilene Christian Wildcats"] = {"4E2683", "FFFFFF", "C5C6C8", name1="Purple", name2="White", cite="{{cite web |url=http://www.acu.edu/campusoffices/university-marketing/brand-resources/logo/colors.html |title=Colors of Master Logo - Office of University Marketing |accessdate=April 1, 2016 |archive-url=https://web.archive.org/web/20170309073121/http://www.acu.edu/campusoffices/university-marketing/brand-resources/logo/colors.html |archive-date=March 9, 2017}}"},
["Academy of Art Urban Knights"] = {"CC0000", "FFFFFF", "000000", name1="Red", name3="Black", cite="{{cite manual |url=http://www.thepacwest.com/documents/2015/6/22//pacWest_colorways_spring2015.pdf?id=2516 |title=Pacific West Conference Visual Identity Standards |accessdate=March 23, 2017}}"},
["Adams State Grizzlies"] = {"154734", "FFFFFF", "231F20", name1="Green", name2="White", cite="{{cite manual |url=https://www.adams.edu/administration/comm/athletics-style-guide.pdf |title=Adams State College Athletics Style Guide |accessdate=Septemb
@abhandaru
abhandaru / colleges.csv
Last active September 20, 2018 18:34
Colleges with their name, campus name, state, and email domain information
Name Campus State Email domain
Abilene Christian University Abilene TX acu.edu
Adelphi University Garden City NY adelphi.edu
Agnes Scott College Atlanta/Decatur GA scottlan.edu
Alabama State University Montgomery AL alasu.edu
Albion College Albion MI albion.edu
Alfred University Alfred NY alfred.edu
Allegheny College Meadville PA alleg.edu
Alma College Alma MI alma.edu
Alverno College Milwaukee WI alverno.edu
// Using Rediscala library here.
import redis.RedisClient
import scala.concurrent.Future
import java.util.UUID
case class RedisLockedException(key: String) extends Exception(s"Resource '${key}' is in use.")
// This is based on the following Redis locking scheme (single instance):
// https://redis.io/topics/distlock
object RedisLock {
@abhandaru
abhandaru / Mustache.scala
Created March 28, 2018 00:51
Minimal mustache-scala string template example
import collection.JavaConverters
import com.github.mustachejava.reflect.ReflectionObjectHandler
import com.github.mustachejava.{DefaultMustacheFactory, Iteration, MustacheFactory}
import java.io.{StringReader, StringWriter, Writer}
import java.lang.reflect.{Field, Method}
import java.util.{List => JList}
import runtime.BoxedUnit
import scala.reflect.ClassTag
object Mustache {