Last active
August 29, 2015 14:02
-
-
Save funatsufumiya/be06b36ea3ad03718b0c to your computer and use it in GitHub Desktop.
Java vs Scala vs Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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