Skip to content

Instantly share code, notes, and snippets.

@sshark
sshark / AreYouDone.java
Last active August 4, 2016 04:06
What is wrong with this class?
public class AreYouDone {
private boolean done = false;
public void done() {
done = true;
}
public boolean isDone() {
return done;
}
@sshark
sshark / fib.scala
Last active October 13, 2015 01:38
Simple maths functions e.g. Fibonacci and factorial sequences
// better use of lazy eval with Stream.
// please refer to http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream for details
lazy val fib: Stream[Int] = Stream.cons(0, Stream.cons(1, fib.zip(fib.tail).map(p => p._1 + p._2)))
implicit def iterableWithSum(it: Iterable[Int]) = new { def sum = it.foldLeft(0)(_ + _) }
fib.filter( _ % 2 == 0).takeWhile(_ < 4000000).sum
/*
The following implementation provides a more "cost effective" implementation due to the fact that
@sshark
sshark / MapReduce.scala
Created January 30, 2013 02:50
Curry and functions flexibilities
/**
*
* @author Lim, Teck Hooi
*
* <br/><br/>30/01/2013 10:19 AM
*
*/
object MapReduce {
def mapReduce(f : Int => Int, combine : (Int, Int) => Int, zero : Int)(a:Int, b: Int) : Int =
@sshark
sshark / minmax
Last active December 13, 2015 22:58
def minmax(values : Array[Int]) : (Int, Int) = {
def minmax_(values : Array[Int], n : (Int, Int)) : (Int, Int)= {
if (values.isEmpty) n
else {
values.head match {
case x if x < n._1 => minmax_(values.tail, (x, n._2))
case x if x > n._2 => minmax_(values.tail, (n._1, x))
case _ => minmax_(values.tail, n)
}
}
@sshark
sshark / FactorialWithBigDecimal.java
Last active December 19, 2015 08:49
Factorial using BigDecimal
package org.thlim;
import java.math.BigDecimal;
/**
*
*
* @author Lim, Teck Hooi
*
*/
@sshark
sshark / FuturesAndAwaitExample.scala
Created August 18, 2013 10:40
Mulitple actors example using Future-s and Await.
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.forkjoin.ForkJoinPool
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.util.Random
/**
*
@sshark
sshark / Factorial.java
Created August 20, 2013 09:14
Factorial with BigInteger
package com.hp.hpls.monsoon.servicecat.service.impl;
import java.math.BigInteger;
/**
*
* @author Lim, Teck Hooi
*
*
*/
@sshark
sshark / Permutations.java
Last active April 20, 2016 06:46
Search all the permutations that uniquely filled the blanks (0-slot) with the numbers from 1 to 9.
package org.teckhooi;
import java.util.*;
/**
* @author Lim, Teck Hooi
*/
public class Permutations {
public static void main(String[] args) {
List<Integer> series = Arrays.asList(new Integer[] {1, 0, 0, 5, 2, 0, 3, 0, 0});
@sshark
sshark / GroceryStore.java
Last active July 20, 2017 11:03
A short Java code example to show case Java Generics with wildcard. This code highlights the uses of wildcard "extends" and "super".
package org.teckhooi.generics;
import java.util.ArrayList;
import java.util.List;
/**
* java -ea to enable assert otherwise AssertionError will not be thrown with
* false assertion.
*
* To compile,
@sshark
sshark / RunMeFuture.java
Last active October 6, 2017 12:47
Java Future implementation example
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
*