Skip to content

Instantly share code, notes, and snippets.

@fairjm
fairjm / 1
Last active August 29, 2015 14:04
package laozhao
import java.util.concurrent.Semaphore
trait IBuffer {
def flush: Unit
}
class Item {
val semaphore = new Semaphore(0)
@fairjm
fairjm / EventSourcing.scala
Last active August 29, 2015 14:05
a simple event sourcing example to help myself comprehend the idea.
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.HashMap
case class Account(val user:String,val money:Double)
sealed abstract class Op
case class Despoit(val money:Double) extends Op
@fairjm
fairjm / JDBCTestSpec2.scala
Created August 31, 2014 14:04
slick2_mysql_for_update
package jdbctest
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import model.Tables
import model.Tables.UserRow
import scala.concurrent.Future
import scala.concurrent.Await
class JDBCTestSpec2 extends FlatSpec with Matchers {
@fairjm
fairjm / P5.scala
Last active August 29, 2015 14:06
projecteuler_problem
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.HashMap
import scala.annotation.tailrec
/*
** author fairjm
** https://projecteuler.net/problem=5
*/
object P5 {
def getPrimesLEQ(num:Int):List[Int] = {
@fairjm
fairjm / WordCount.ex
Created November 27, 2014 12:43
a simple word count model
defmodule Wordcount do
@moduledoc "a simple wordcount program by fairjm"
@nodecount 3
def start(file) do
s = self
spawn(fn -> count(file,s) end)
end
defp count(file,caller) do
@fairjm
fairjm / Download.scala
Created December 5, 2014 17:27
record some scala code
// a simple script
// batch download from http://www.andrew.cmu.edu/course/15-749/READINGS/optional/
//extract file name
def getAllFiles(url:String):List[String] = {
import scala.io._
val regex = """<a href="(.+?\.pdf)">""".r
val content = Source.fromURL(url).mkString
regex.findAllMatchIn(content).map(_.group(1)).toList
}
@fairjm
fairjm / NMI.scala
Created May 6, 2015 08:28
NMI(Normalized Mutual Information) scala implementation
package com.cc.graph.algorithm
import com.cc.graph.base.Graph
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.HashMap
object NMI {
/**
* NMI(A,B)=2*H(A)/(H(A)+H(B))
public class StringCompressUtil {
private static final String DEFAULT_ENCODING = "UTF-8";
public static String compress(final String str) throws IOException {
if (StringUtils.isEmpty(str)) {
return str;
}
String result;
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
open System.Text
open System.IO
open System.IO.Compression
let iso8859_1 = Encoding.GetEncoding("ISO-8859-1");
let compress (s:string) =
let sBytes = Encoding.UTF8.GetBytes(s)
use original = new MemoryStream(sBytes)
let oBytes = using(new MemoryStream())(fun out ->
@fairjm
fairjm / ScanRequestMappingTool.java
Last active September 6, 2018 12:55
used to scan RequestMapping(SpringMVC)
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;