Skip to content

Instantly share code, notes, and snippets.

View nicholasren's full-sized avatar

Xiaojun Ren nicholasren

View GitHub Profile
@nicholasren
nicholasren / high-order-function.scala
Last active August 29, 2015 14:01
scala the language matters training
//-------------------------------------//
// readFile is a high order function //
// which can take another function //
// as parameter //
//-------------------------------------//
def using[T](f: File)(handler: FileInputStream => T): T = {
val resource = new java.io.FileInputStream(f)
try {
handler(resource)
@nicholasren
nicholasren / function-as-first-class-citizen.java
Created May 19, 2014 14:51
scala the language matters training
//-------------------------------------------//
// function as parameter java implementation //
//-------------------------------------------//
class UerFetcher implement Callable<User> {
private String userId;
@Autowired
private UserService userService;
@nicholasren
nicholasren / blocking-with-future.scala
Created May 11, 2014 10:19
solving nested future problem
val fa = callToRemoteServiceA();
val fb = callToRemoteServiceB();
val fc = callToRemoteServiceC(fa.get());
val fd = callToRemoteServiceD(fb.get());
val fe = callToRemoteServiceE(fb.get());
val executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue[Runnable]())
def solveRPN(expression: String):String = {
expression.split(" ").foldLeft(List[String]()) {
(stack:List[String], e:String) =>
e match {
case "*" =>
stack match {
case List(z) => List(z)
case x :: y :: ys => (y.toInt * x.toInt).toString :: ys
}
case "+" =>