Skip to content

Instantly share code, notes, and snippets.

@pathikrit
pathikrit / GitPunchCard.scala
Last active April 15, 2018 11:17
Scala Script to print Git PunchCard
/**
* Quick and dirty Scala app to print git commit punch-card e.g.
*
* ┃08┃09┃10┃11┃12┃13┃14┃15┃16┃17┃18┃19┃20┃21┃22┃23┃00┃01┃02┃03┃04┃05┃06┃07┃
* Sun┃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Mon┃▁▁▁▄▄▄▅▅▅▅▅▅▄▄▄▆▆▆▇▇▇▇▇▇███▆▆▆▅▅▅▄▄▄▃▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Tue┃▁▁▁▃▃▃▆▆▆▅▅▅▅▅▅▅▅▅▆▆▆▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Wed┃▁▁▁▄▄▄▅▅▅▇▇▇▅▅▅▅▅▅███▇▇▇▅▅▅▆▆▆▇▇▇▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Thu┃▁▁▁▂▂▂▄▄▄▆▆▆▅▅▅▆▆▆▇▇▇▇▇▇▆▆▆▇▇▇▅▅▅▄▄▄▃▃▃▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Fri┃▁▁▁▂▂▂▄▄▄▅▅▅▅▅▅▄▄▄▄▄▄▅▅▅▅▅▅▃▃▃▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
@pathikrit
pathikrit / local.sbt
Created August 30, 2017 15:03
Local sbt
triggeredMessage := Watched.clearWhenTriggered
libraryDependencies += "com.lihaoyi" % "ammonite" % "latest.release" % "test" cross CrossVersion.full
initialCommands in (Test, console) := """ammonite.Main().run()"""
watchSources ++= (
(baseDirectory.value * "*.sbt").get
++ (baseDirectory.value / "project" * "*.scala").get
++ (baseDirectory.value / "project" * "*.sbt").get
@pathikrit
pathikrit / MajorityElement.scala
Last active June 12, 2017 15:49
Boyer–Moore majority vote algorithm
import scala.collection.generic.Growable
/**
* Boyer–Moore majority vote algorithm (https://en.wikipedia.org/wiki/Boyer–Moore_majority_vote_algorithm)
* A Data structure that supports O(1) tracking of the majority element in streaming data
* (i.e. something that occurs strictly > 50% of the time)
*/
class MajorityElement[A] extends Growable[A] {
private[this] var majorityElement = Option.empty[A]
private[this] var count = 0
@pathikrit
pathikrit / README.md
Last active November 2, 2019 22:20
Contravariance vs. Covariance
  • Let C<A> be a higher-kinded type e.g. in List<Animal>, List is C and Animal is A.
  • Let S be a subtype of T e.g. in class Cat extends Animal, Cat is S and Animal is T
  • If C<S> is a subtype of C<T>, then C is covaraint on T e.g. List<Cat> is a subtype of List<Animal>
  • If C<T> is a subtype of C<S>, then C is contravariant on T e.g. Predicate<Animal> is a subtype of Predicate<Cat>
  • If neither C<T> and nor C<S> are subtypes of the other, thenC is invariant on T
  • If both C<T> and C<S> are subtypes of each other, then C is phantom variant on T. This is possible in languages which support phantom types like Haskell

In Scala:

@pathikrit
pathikrit / PalindromicSubstrings.scala
Last active April 15, 2018 11:18
Count number of palindromic substrings (non-empty) of a string
/**
* Dumb O(n^3) version: Check if all n^2 substrings are palindromes
*/
def countPalindromes3(s: String): Int =
(1 to s.length).flatMap(s.sliding).count(t => t.reverse == t)
/***********************************************************************/
/**
* O(n^2) solution - count how many (odd and even) palindromes are centered at index i by expanding left and right
*/
@pathikrit
pathikrit / Config.scala
Last active March 5, 2020 14:22
Better Config
import scala.util.control.NonFatal
import better.files.Scanner.Read
/**
* Extend this trait to create your application config
*
* Pros of this approach:
* 1) Library free approach - only 15 lines of dependency free "library" (four one-line defs for you to override)
* 2) Failures happen when the Config object is loaded instead of when a config value is accessed
* 3) Strongly typed
@pathikrit
pathikrit / KShingling.scala
Created March 2, 2017 15:46
Thread-safe k-shingling in Scala
import scala.collection.mutable
/**
* A thread-safe Scala port of the KShingling implementation of https://github.com/tdebatty/java-string-similarity
*/
class KShingling(k: Int = 3) {self =>
private[this] val shingles = mutable.Map.empty[String, Int]
def profile(s: String) = {
@pathikrit
pathikrit / CircularBuffer.java
Last active October 28, 2018 05:02
A circular (or ring) buffer: O(1) random-indexing (get/set), append, prepend, dropFirst, dropLast, clear
class CircularBuffer<T> {
private T[] array = (T[]) new Object[1<<4];
private int start = 0, end = 0;
public T get(int i) {
assert(0 <= i && i < size());
return array[mod(start + i)];
}
public void set(int i, T elem) {
@pathikrit
pathikrit / deremote.sh
Created January 4, 2017 16:44
Delete all local branches whose remote is gone
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
@pathikrit
pathikrit / ProducerConsumer.scala
Last active October 28, 2018 05:06
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