Skip to content

Instantly share code, notes, and snippets.

View abhin4v's full-sized avatar

Abhinav Sarkar abhin4v

View GitHub Profile
@abhin4v
abhin4v / cache_server.exs
Created March 18, 2018 06:36
Simple cache using GenServer in Elixir
defmodule CacheServer do
use GenServer
@name CS
## Client API
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts ++ [name: CS])
end
@abhin4v
abhin4v / onlinedrummer_downloader.py
Last active January 10, 2023 11:39
Python script to download all song drum sheets from onlinedrummer.com and save them in an organized structure in the current directory. Requires python-mechanize lib to be installed.
import sys
import os
import itertools
import re
import traceback
import time
import mechanize
from functools import wraps
from urllib import urlretrieve
@abhin4v
abhin4v / Calc.hs
Last active November 29, 2022 08:02
Simple Applicative Parser and Expression Calculator in Haskell
module Calc
( Expr(..)
, parse
, calculate
) where
import Control.Applicative
import Parser
data Expr = Add Expr Expr
@abhin4v
abhin4v / astar.hs
Last active November 24, 2022 06:36
A* (A star) search in haskell
import qualified Data.PQueue.Prio.Min as PQ
import qualified Data.HashSet as Set
import qualified Data.HashMap.Strict as Map
import Data.Hashable (Hashable)
import Data.List (foldl')
import Data.Maybe (fromJust)
astarSearch :: (Eq a, Hashable a) => a -> (a -> Bool) -> (a -> [(a, Int)]) -> (a -> Int) -> Maybe (Int, [a])
astarSearch startNode isGoalNode nextNodeFn heuristic =
astar (PQ.singleton (heuristic startNode) (startNode, 0))
@abhin4v
abhin4v / upsert.clj
Last active July 4, 2022 20:42
Postgres Upsert in Clojure using clojure.java.jdbc and honeysql.
(ns postgres.upsert
(:require [honeysql.core :as sql]
[clojure.java.jdbc :as jdbc]
[clojure.string :as str]
[clojure.set :as set]))
(defn- keyword->colname [kwd]
(-> kwd
name
(str/replace #"-" "_")))
@abhin4v
abhin4v / maximal_cliques.py
Last active October 7, 2021 16:46
Finds all maximal cliques in a graph using the Bron-Kerbosch algorithm
# Finds all maximal cliques in a graph using the Bron-Kerbosch algorithm. The input graph here is
# in the adjacency list format, a dict with vertexes as keys and lists of their neighbors as values.
# https://en.wikipedia.org/wiki/Bron-Kerbosch_algorithm
from collections import defaultdict
def find_cliques(graph):
p = set(graph.keys())
r = set()
x = set()
@abhin4v
abhin4v / Fibonacci.java
Created November 24, 2009 18:46
Python-style Generator in Java
package net.abhinavsarkar.util;
/**
* A (infinite) Fibonacci number generator.
*
* @author Abhinav Sarkar
*/
public class Fibonacci extends Generator<Integer> {
@abhin4v
abhin4v / dfs.hs
Last active December 22, 2020 12:16
Implementation of depth first search in Haskell
module DepthFirstSearch where
import Data.Foldable (asum)
import Data.List ((\\))
dfs :: (Eq a) => (a -> [a]) -> a -> a -> Maybe [a]
dfs next start goal = dfs' [] start
where dfs' path current
| current == goal = Just . reverse $ goal : path
| null nexts = Nothing
@abhin4v
abhin4v / SpringGuavaCache.java
Last active February 4, 2020 11:45
An implementation of the Spring Cache interface on top of Google Guava cache
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
@abhin4v
abhin4v / RingTopologySimulation.scala
Created July 12, 2010 19:27
Simulation of Ring network topology using Actors in Scala
import actors.Actor
import actors.Actor._
object RingTopologySimulation extends Application {
case class Message(count: Int)
class Node(id: Int, stops: Int) extends Actor {
var next: Node = null