Skip to content

Instantly share code, notes, and snippets.

@pathikrit
pathikrit / ProducerConsumer.scala
Last active August 19, 2025 23:29
Single synchronous Producer/Consumer in Scala
import java.util.concurrent.ArrayBlockingQueue
import scala.concurrent.{ExecutionContext, Future}
/**
* Rick's implementation of ghetto back-pressure algo
* Implement this trait and pass it off to ProducerConsumer.Runner to run it
*
* @tparam R Type of result to be crunched
* @tparam S State to iterate on
@pathikrit
pathikrit / GroupNamedRegex.scala
Last active September 21, 2017 01:37
Group named Regex
import java.util.regex.{MatchResult, Pattern}
import scala.collection.mutable
/**
* Supports named group finding
*
* @see http://stackoverflow.com/questions/39754604/
*/
class GroupNamedRegex(pattern: Pattern, namedGroups: Set[String]) {
def this(regex: String) = this(Pattern.compile(regex), GroupNamedRegex.namePattern.findAllMatchIn(regex).map(_.group(1)).toSet)
@pathikrit
pathikrit / DbBatchedJob.scala
Created September 14, 2016 17:57
Batched prepared statements
package com.coatue.datascience.util
import java.sql.{Connection, PreparedStatement}
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import slick.driver.PostgresDriver.api._
class DbBatchedJob(db: Database, sql: String, batchSize: Int) extends AutoCloseable {
@pathikrit
pathikrit / README.md
Last active April 20, 2026 05:58
My highly opinionated list of things needed to build an app in Scala
@pathikrit
pathikrit / MortgageMath.md
Created August 16, 2016 19:35
Mortgage Math

Input

p := Original Principal amount
apr := Annual Percentage Rate
t := Number of years of loan

Calcuation:

r := apr/100/12                    # monthly interest rate
@pathikrit
pathikrit / bulk_edit_commit_msgs.sh
Created July 5, 2016 13:24
Script to replace a substring in all new commit messages in your feature branch
#!/usr/bin/env bash
substring=$1
replace=$2
current_branch=$(git name-rev --name-only HEAD)
echo "Replacing ${substring} with ${replace} in all new commits in ${current_branch}"
cd $(git root)
git filter-branch --msg-filter "'sed ""s/${substring}/${replace}/g""'" master..${current_branch}
git push -f
@pathikrit
pathikrit / ValueSortedMap.scala
Last active June 3, 2019 14:36
A sorted map that sorts keys by value
import scala.collection.mutable
type PQ[K, V] = mutable.SortedMap[K, V]
object PQ {
def apply[K, V: Ordering](elems: Seq[(K, V)]): PQ[K, V] =
elems.foldLeft(PQ.empty[K, V])(_ += _)
/**
* A SortedMap which sorts keys by the value
/**
* Solves the n-Queen puzzle in O(n!)
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row)
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until n)
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks)
* @return returns a Iterator of solutions
* Each solution is an array p of length n such that p[i] is the column of the queen on the ith row
*/
def nQueens(n: Int): Iterator[Seq[Int]] =
(0 until n)
@pathikrit
pathikrit / Not.scala
Last active August 3, 2016 16:17
Simple Negation Types in Scala
trait NotSubTypeOf[A, B] // encoding to capture A is not a subtype of B
// Note: We can use infix notation to write `A NotSubTypeOf B` instead of `NotSubTypeOf[A, B]`
// evidence for any two arbitrary types A and B, A is not a subtype of B
implicit def isSub[A, B]: A NotSubTypeOf B = null
// define ambigous implicits to trigger compile error in case A is a subtype of B (or A =:= B)
implicit def iSubAmbig1[A, B >: A]: A NotSubTypeOf B = null
implicit def iSubAmbig2[A, B >: A]: A NotSubTypeOf B = null
@pathikrit
pathikrit / AutoSuggest.scala
Last active March 5, 2016 06:25
Auto suggester
class AutoSuggest(corpus: String, alphabet: Seq[Char] = 'a' to 'z', depth: Int = 2) {
val words = s"[${alphabet.head}-${alphabet.last}]+".r
.findAllIn(corpus.toLowerCase).toSeq
.groupBy(_.toSeq).mapValues(_.size)
.par withDefaultValue 0
def editDistance(a: Seq[Char], b: Seq[Char]) = {
lazy val d: Stream[Stream[Int]] = Stream.tabulate(a.length + 1, b.length + 1) {
case (i, j) if (i - j).abs > depth => Int.MaxValue
case (i, 0) => i