Skip to content

Instantly share code, notes, and snippets.

View vigneshwaranr's full-sized avatar

Vigneshwaran Raveendran vigneshwaranr

View GitHub Profile
@vigneshwaranr
vigneshwaranr / migrator.sh
Created August 24, 2012 18:34
Script to convert SQLITE dumps into PostgreSQL compatible dumps
#! /bin/sh
usage_error () {
echo 'Usage: sh migrator.sh <path to sqlite_to_postgres.py> <path to sqlite db file> <an empty dir to output dump files>'
echo
echo 'Example:'
echo '>sh migrator.sh sqlite_to_postgres.py ~/reviewboard.db /tmp/dumps'
echo
echo 'Tested on:'
echo 'Python 2.7.3'
@vigneshwaranr
vigneshwaranr / blockingqueue.rb
Created August 22, 2015 14:47
This Class is just my exercise to try ConditionVariable in Ruby
require 'thread'
#This Class is just my exercise to try ConditionVariable
class BlockingQueue
def initialize
@queue = Queue.new
@semaphore = Mutex.new
@condition = ConditionVariable.new
end
@vigneshwaranr
vigneshwaranr / Main.java
Last active August 29, 2015 14:27
Just my exercise to try out wait/notify in Java
import java.util.Random;
public class Main {
private static final MyBlockingQueue<Integer> queue = new MyBlockingQueue<>();
static class Producer extends Thread {
Producer(int i) {
super("Producer-" + i);
}
@Override
@vigneshwaranr
vigneshwaranr / sumOfSquaresOfEvenElements.java
Created May 29, 2020 07:24
9e8a12f2112b / sumOfSquaresOfEvenElements
public int sumOfSquaresOfEvenElements(List<Integer> list) {
if (list == null) {
return 0;
}
int acc = 0;
for (int elem: list) {
if (elem % 2 == 0) {
acc += (elem * elem);
}
}
@vigneshwaranr
vigneshwaranr / MaybeDivideable.scala
Created May 29, 2020 07:28
9e8a12f2112b / MaybeDivideable
sealed trait MaybeDivideable {
def get: Int //Extraction
def flatMap(f: Int => MaybeDivideable): MaybeDivideable
}
object MaybeDivideable {
def apply(value: Int): MaybeDivideable =
if (value == 0) UnDivideable else Divideable(value) // Lifting
}
@vigneshwaranr
vigneshwaranr / divide1.scala
Created May 29, 2020 07:29
9e8a12f2112b / MaybeDivideable / divide1
def divide1(a: Int, b: Int): MaybeDivideable = {
MaybeDivideable(a) match {
case UnDivideable =>
UnDivideable
case Divideable(x) =>
MaybeDivideable(b) match {
case UnDivideable =>
UnDivideable
@vigneshwaranr
vigneshwaranr / divide2.scala
Created May 29, 2020 07:30
9e8a12f2112b / MaybeDivideable / divide2
def divide2(a: Int, b: Int): MaybeDivideable = {
val aMayBe = MaybeDivideable(a)
val bMayBe = MaybeDivideable(b)
aMayBe.flatMap { x =>
bMayBe.flatMap { y =>
MaybeDivideable(x / y)
}
}
}
@vigneshwaranr
vigneshwaranr / MaybeDivideable2.scala
Last active May 29, 2020 07:33
9e8a12f2112b / MaybeDivideable2
sealed trait MaybeDivideable {
def get: Int
def flatMap(f: Int => MaybeDivideable): MaybeDivideable
/* New map method */
def map(f: Int => Int): MaybeDivideable
}
case class Divideable(value: Int) extends MaybeDivideable {
require(value != 0, "value must not be 0")
@vigneshwaranr
vigneshwaranr / divide3.scala
Created May 29, 2020 07:32
9e8a12f2112b / MaybeDivideable2 / divide3
def divide3(a: Int, b: Int): MaybeDivideable = {
for {
x <- MaybeDivideable(a)
y <- MaybeDivideable(b)
} yield (x / y)
}
@vigneshwaranr
vigneshwaranr / bad-future.scala
Last active February 14, 2021 06:45
2021-02-14 Blog post Scala Future
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
val future = someFutureJob()
// NOO :(
val value = Await.result(future, Duration.Inf)
operate(value)