Last active
August 29, 2015 14:14
-
-
Save sshark/03e11ab8a116f1b8669e to your computer and use it in GitHub Desktop.
Java vs Scala code length and style comparison
This file contains hidden or 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.List; | |
| import java.util.ArrayList; | |
| import java.util.Map; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| public class RunMe { | |
| public static void main(String[] args) { | |
| List<String> sentences = new ArrayList<String>(); | |
| sentences.add("This is the first sentence."); | |
| sentences.add("This is the second sentence."); | |
| sentences.add("This is the third sentence."); | |
| Map<String, List<String>> wordMap = makeWordMap(sentences); | |
| Iterator<Map.Entry<String, List<String>>> iter = wordMap.entrySet().iterator(); | |
| StringBuffer sb = new StringBuffer(); | |
| while (iter.hasNext()) { | |
| Map.Entry<String, List<String>> entry = iter.next(); | |
| sb.append(entry.getKey()) | |
| .append(" -> ") | |
| .append(entry.getValue().size()) | |
| .append('\n'); | |
| } | |
| System.out.println(sb.toString()); | |
| } | |
| static String[] words(String sentence) { | |
| return sentence.split(" "); | |
| } | |
| static Map<String, List<String>> makeWordMap(List<String> sentences) { | |
| Map<String, List<String>> result = new HashMap<>(); | |
| for (String sentence: sentences) { | |
| for (String word: words(sentence)) { | |
| List<String> sentencesForWord = result.get(word); | |
| if (sentencesForWord == null) { | |
| sentencesForWord = new ArrayList<String>(); | |
| result.put(word, sentencesForWord); | |
| } | |
| sentencesForWord.add(sentence); | |
| } | |
| } | |
| return result; | |
| } | |
| } |
This file contains hidden or 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
| val sentences = List("This is the first sentence.", "This is the second sentence.", "This is the third sentence.") | |
| val wordsWarp = sentences.foldLeft(Map[String, List[String]]()){ | |
| case (m, s) => s.split(" ").foldLeft(m){ | |
| case (m2, word) => m2 + (word -> (s :: (m2.getOrElse(word, Nil))))}} | |
| wordsWarp.foreach{case (k,v) => println(s"$k -> ${v.size}")} | |
| // removed intermediate val | |
| List("This is the first sentence.", | |
| "This is the second sentence.", | |
| "This is the third sentence.").foldLeft(Map[String, List[String]]()){ | |
| case (m, s) => s.split(" ").foldLeft(m){ | |
| case (m2, word) => m2 + (word -> (s :: (m2.getOrElse(word, Nil))))}}.foreach{ | |
| case (k,v) => println(s"$k -> ${v.size}")} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment