Skip to content

Instantly share code, notes, and snippets.

View anoopelias's full-sized avatar

Anoop Elias anoopelias

View GitHub Profile
package jperftest;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.ArrayList;
import java.util.List;
class TestClass {
private static final int[] PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };
public static void main(String args[]) throws Exception {
goodVsBad(23, 46, 22);
}
function substring(txt, str) {
var dfa = compile(str);
var curr = 0;
for(var i=0; i<txt.length; i++) {
curr = next(dfa, curr, txt.charAt(i));
if(curr === str.length)
return true;
}
@anoopelias
anoopelias / Inheritance.js
Created August 11, 2012 13:40
A snippet to show the idea of prototype object and inheritance in JavaScript
ObjMaker = function() {
this.a = "first";
};
ObjMaker.prototype.b = "second";
var objMaker = new ObjMaker();
ObjMaker.prototype.c = "third";
console.log("objMaker.a " + objMaker.a); // Prints 'first'
console.log("objMaker.b " + objMaker.b); // Prints 'second'
@anoopelias
anoopelias / MemoryLeakTest.java
Created September 3, 2012 21:16
Test memory leak in java code
import java.util.WeakHashMap;
import junit.framework.Assert;
import org.junit.Test;
public class MemoryLeakTest {
@Test
public void test_stack_memory_leak() {
@anoopelias
anoopelias / Implicit.scala
Created December 10, 2012 16:05
Implicit variables in scala
package concept
object Impicit {
implicit val i : Int = 1 //> i : Int = 1
def add(a:Int)(implicit b:Int) = a + b //> add: (a: Int)(implicit b: Int)Int
add(10)(15) //> res0: Int = 25
add(10) //> res1: Int = 11
}
@anoopelias
anoopelias / Closure.markdown
Created December 10, 2012 16:03
Closure..

Closure..

Couple of months back, when I started looking at Scala as a programming language, a key computer programming concept came up - Closure. I couldn't find a page which explains what it is and how to use it, so here it goes,

Definition

A closure is a first-class function with free variables that are bound in the lexical environment. (Source wikipedia)

First-class functions

First-class functions are those that can be assigned to a variable to be carried around and be executed at a later point of time. Subsequently this will enable those functions being passed as arguments, as well as returned back. An example in Scala is shown below.

@anoopelias
anoopelias / ImpicitConversion.scala
Created December 10, 2012 16:27
Implicit conversion in scala
package concept
object ImpicitConversion {
class Val {
def value = 10
}
/* Implicit Conversion begins */
/**
* __main__
*/
var bot;
var botId;
var lenColumns;
var lenRows;
@anoopelias
anoopelias / Assignment.scala
Created December 28, 2012 14:37
Is there a name for this kind of assignment? See how 'n' is assigned below.
scala> case class Name(name:String)
defined class Name
scala> val a = Name("Anoop")
a: Name = Name(Anoop)
scala> val Name(n) = a
n: String = Anoop
scala>