Skip to content

Instantly share code, notes, and snippets.

# Prints the Total heap usage of Java process
jdk1.7.0/bin/jstat -gccapacity 9043 | tail -n 1 | awk '{ print $4, $5, $6, $10 }' | python -c "import sys; nums = [x.split(' ') for x in sys.stdin.readlines()]; print(str(sum([float(x) for x in nums[0]]) / 1024.0) + ' mb');"
@purijatin
purijatin / CaptureType.java
Last active August 29, 2015 14:16
Capture Type information
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* CaptureType is useful to capture the generic type of a parametrized class. For example:
* <pre>
public static <T> CompletableFuture<List<T>> sequence2(List<CompletableFuture<T>> com, ExecutorService exec) {
if(com.isEmpty()){
throw new IllegalArgumentException();
}
Stream<? extends CompletableFuture<T>> stream = com.stream();
CompletableFuture<List<T>> init = CompletableFuture.completedFuture(new ArrayList<T>());
return stream.reduce(init, (ls, fut) -> ls.thenComposeAsync(x -> fut.thenApplyAsync(y -> {
x.add(y);
return x;
},exec),exec), (a, b) -> a.thenCombineAsync(b,(ls1,ls2)-> {
@purijatin
purijatin / jargon.md
Last active August 29, 2015 14:26 — forked from cb372/jargon.md
Category theory jargon cheat sheet

Category theory jargon cheat sheet

A primer/refresher on the category theory concepts that most commonly crop up in conversations about Scala or FP. (Because it's embarassing when I forget this stuff!)

I'll be assuming Scalaz imports in code samples, and some of the code may be pseudo-Scala.

Functor

A functor is something that supports map.

@purijatin
purijatin / YouAreQuiteGoodAtJava.java
Last active September 3, 2015 15:53 — forked from anonymous/isuckatjava.java
some java code
package com.jp;
import java.io.BufferedReader;
import java.io.FileReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.IntStream;
@purijatin
purijatin / DefaultType.scala
Last active November 5, 2015 16:45
This is damn good
//source: http://www.cakesolutions.net/teamblogs/default-type-parameters-with-implicits-in-scala
object Prac {
trait DefaultsTo[Type, Default]
object DefaultsTo {
implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null
implicit def fallback[T, D]: DefaultsTo[T, D] = null
}
type Def[Default] = {
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
import static java.util.Objects.isNull;
public class User {
@purijatin
purijatin / gist:5721126
Last active December 18, 2015 03:49
Consolidate from multiple Future's
type T = //someType
val list:[Future[T]] = //list of different Future[T]
@purijatin
purijatin / DifferentTypes.scala
Last active September 23, 2016 06:51
New.scala
To write a method which takes two type parameters and both should not be the same:
trait =!=[A,B]
implicit def neq[A,B]: =!=[A,B] = null
implicit def eqAmb[A]: =!=[A,A] = null
implicit def eqAmbig2[A]: =!=[A,A] = null
case class Foo2[A,B]( a: A, b: B )( implicit ev: A =!= B )
@purijatin
purijatin / ZooKeeperLock.scala
Last active October 3, 2016 11:22
Distributed Lock based on ZooKeeper
package zookeeper
import java.util.concurrent.{ExecutorService, Executors}
import org.apache.zookeeper._
import scala.collection.JavaConverters._
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.control._