Skip to content

Instantly share code, notes, and snippets.

val s = Seq("apple", "oranges", "apple", "banana", "apple", "oranges", "oranges")
s.foldLeft(Map.empty[String, Int]) { (m, x) => m + ((x, m.getOrElse(x, 0) + 1)) }
// output: scala.collection.immutable.Map[String,Int] = Map(apple -> 3, oranges -> 3, banana -> 1)
<div class="gist">https://gist.github.com/6968489</div>
public static <T> Map<T, Integer> getCounts(List<T> list) {
Map<T, Integer> counts = new HashMap<T, Integer>();
for(T item : list) {
if(!counts.containsKey(item)) {
counts.put(item, 0);
}
counts.put(item, counts.get(item) + 1);
}
return counts;
}
def getCounts[T](list: Seq[T]): Map[T, Int] = {
list.foldLeft(Map.empty[T, Int]) { (m: Map[T, Int], x: T) => m + ((x, m.getOrElse(x, 0) + 1)) }
}
import scala.collection.mutable.Map;
def getCounts[T](list: Seq[T]): Map[T, Int] = {
val map = Map.empty[T, Int];
list.foreach(x => {
if (!map.contains(x)) {
map.put(x, 0);
}
map.put(x, map(x) + 1);
});
return map;
def getCounts[T](list: Seq[T]): Map[T, Int] = list.foldLeft(Map.empty[T, Int]) { (m: Map[T, Int], x: T) => m + ((x, m.getOrElse(x, 0) + 1)) }
module.exports = React.createClass({
displayName: 'ImageTester',
getInitialState() {
return {
showImage: false
};
},
render() {
import React from 'react-native'
var {
Image,
Animated,
View
} = React
module.exports = React.createClass({
getInitialState() {
import React from 'react-native'
var {
Image,
Animated,
View
} = React
module.exports = React.createClass({
getInitialState() {
import React from 'react-native'
import Swiper from 'react-native-swiper'
import randomcolor from 'randomcolor'
const {
View,
Text,
StyleSheet
} = React