Skip to content

Instantly share code, notes, and snippets.

View nicholasren's full-sized avatar

Xiaojun Ren nicholasren

View GitHub Profile
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 "+" =>
@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]())
@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 / 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-composition.scala
Last active August 29, 2015 14:01
scala training example- function composition
val userFetcher: String => User = {
//...
}
val commentFetcher: User => List[Comment] = {
//...
}
val userCommentFetcher : String => List[Comment] = userFetcher andThen commentFetcher
@nicholasren
nicholasren / pattern-matching.scala
Created May 19, 2014 15:32
scala training example - pattern matching
case class Option[T]
case class Some(t: T) extends Option[T]
case class None extends Option[T]
val possibleNumber = Some(1)
val numbers = possibleNumber match {
case Some(x) => x
case None => 0
}
@nicholasren
nicholasren / biz-logic-in.java
Last active August 29, 2015 14:01
scala training example - monad
//in post controller
User user = null;
try {
user = userService.findById(userId)
} catch (UserNotFoundException e) {
throw //handling exception
}
@nicholasren
nicholasren / duck-typeing.scala
Created May 20, 2014 01:08
scala training example - structural types
trait Quack {
this: { def quack(): Unit } =>
}
class Duck extends Quack {
def quack() = {
println("Quack, Quack...")
}
}
@nicholasren
nicholasren / realtime-searching.scala
Created October 13, 2014 03:18
a small example of real time indexing and searching engine
package actors
import akka.actor._
import scala.concurrent.{Future, ExecutionContext}
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import java.io.{PrintWriter, FileOutputStream}
import rx.lang.scala.Observable
import rx.lang.scala.Observable._
@nicholasren
nicholasren / push_daemon_v1.java
Last active August 29, 2015 14:20
push_daemon
package com.thoughtworks;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.lang.String;