Spark Scala 26035582 Re-visited -
whilst understand ouctome here, cannot see how highlighted aspects work.
please, enlighten me
def isheavy(inp: string) = inp.split(",")**.map(weights(_))**.sum > 12
val input = list("a,b,c,d", "b,c,e", "a,c,d", "e,g") val splitsize = 10000 // specify number of elements fit in memory. val numsplits = (input.size / splitsize) + 1 // has > 0. val groups = sc.parallelize(input, numsplits) // specify # of splits. val weights = array(("a", 3), ("b", 2), ("c", 5), ("d", 1), ("e", 9), ("f", 4), ("g", 6)).tomap def isheavy(inp: string) = inp.split(",").map(weights(_)).sum > 12 val result = groups.filter(isheavy)
weights
map keyed strings
scala> weights res13: scala.collection.immutable.map[string,int] = map(e -> 9, f -> 4, -> 3, b -> 2, g -> 6, c -> 5, d -> 1)
inp.split(",")
split string, , map
function iterates on keys, converting each value of weights
map respective key.
the underscore scala shortcut , can written such
inp.split(",").map(x => weights(x))
in other words, val input = list("a,b,c,d")
becomes list of numbers (3,2,5,1), summed, , filtered out more 12
for example,
scala> input.foreach(x => println(x.split(",").mkstring)) abcd bce acd eg scala> input.foreach(x => println(x.split(",").map(weights(_)).mkstring(","))) 3,2,5,1 2,5,9 3,5,1 9,6 scala> input.foreach(x => println(x.split(",").map(weights(_)).sum)) 11 16 9 15 scala> input.foreach(x => { | val sum = x.split(",").map(weights(_)).sum | if (sum > 12) println(sum) | }) 16 15
Comments
Post a Comment