Skip to content

Instantly share code, notes, and snippets.

@funatsufumiya
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funatsufumiya/be06b36ea3ad03718b0c to your computer and use it in GitHub Desktop.
Save funatsufumiya/be06b36ea3ad03718b0c to your computer and use it in GitHub Desktop.
Java vs Scala vs Clojure
(ns demo-clj.demo)
(defn -main [] (
(def years
["2009-11", "2009-01",
"2010-01", "2010-12",
"2010-01", "2010-04",
"2010-01", "2010-12",
"2010-12", "2010-04"])
(def arr (ref []))
(doseq [s years]
(let [i (.indexOf @arr s) v (inc i)]
(if (== i -1)
(dosync (alter arr conj s 1))
(dosync (alter arr assoc v (inc (@arr v)))))))
(println (apply array-map @arr))))
import java.util.Map;
import java.util.HashMap;
public class Demo {
public static void main(String[] args) {
String years[] = {
"2009-11", "2009-01",
"2010-01", "2010-12",
"2010-01", "2010-04",
"2010-01", "2010-12",
"2010-12", "2010-04"
};
Map<String,Integer> map = new HashMap<String,Integer>();
for(String s : years){
int count = map.containsKey(s) ? map.get(s) : 0;
map.put(s,count+1);
}
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
}
}
import java.util.Map
import java.util.HashMap
import scala.collection.JavaConversions._
object DemoS {
def main(arg:Array[String]) = {
val years = Array(
"2009-11", "2009-01",
"2010-01", "2010-12",
"2010-01", "2010-04",
"2010-01", "2010-12",
"2010-12", "2010-04"
)
val map = new HashMap[String,Int]()
years.foreach{(s:String) =>
val count = if(map.containsKey(s)) map.get(s) else 0
map.put(s,count+1)
}
map.foreach{case(key,value) => println(key + ", " + value)}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment