scala基本操作分解
scala> import scala.io.Source
import scala.io.Source
scala> val lines = Source.fromFile("./TestFile.txt").getLines().toList
lines: List[String] = List(Preface, “The Forsyte Saga” was the title originally destined for that part of it which is called “The Man of Property”; and to adopt it for the collected chronicles of the Forsyte family has indulged the Forsytean tenacity that is in all of us. The word Saga might be objected to on the ground that it connotes the heroic and that there is little heroism in these pages. But it is used with a suitable irony; and, after all, this long tale, though it may deal with folk in frock coats, furbelows, and a gilt-edged period, is not devoid of the essential heat of conflict. Discounting for the gigantic stature and blood-thirstiness of old days, as they have come down to us in fairy-tale and legend, the folk of the old Sagas were Forsytes, assuredly, in their possessive…
scala> lines.length
res1: Int = 2866
scala> lines.map(x=>x.split(" ")).flatten
res2: List[String] = List(Preface, “The, Forsyte, Saga”, was, the, title, originally, destined, for, that, part, of, it, which, is, called, “The, Man, of, Property”;, and, to, adopt, it, for, the, collected, chronicles, of, the, Forsyte, family, has, indulged, the, Forsytean, tenacity, that, is, in, all, of, us., The, word, Saga, might, be, objected, to, on, the, ground, that, it, connotes, the, heroic, and, that, there, is, little, heroism, in, these, pages., But, it, is, used, with, a, suitable, irony;, and, after, all, this, long, tale, though, it, may, deal, with, folk, in, frock, coats, furbelows, and, a, gilt-edged, period, is, not, devoid, of, the, essential, heat, of, conflict., Discounting, for, the, gigantic, stature, and, blood-thirstiness, of, old, days, as, they, hav…
scala> lines.map(_.split(" ")).flatten
res3: List[String] = List(Preface, “The, Forsyte, Saga”, was, the, title, originally, destined, for, that, part, of, it, which, is, called, “The, Man, of, Property”;, and, to, adopt, it, for, the, collected, chronicles, of, the, Forsyte, family, has, indulged, the, Forsytean, tenacity, that, is, in, all, of, us., The, word, Saga, might, be, objected, to, on, the, ground, that, it, connotes, the, heroic, and, that, there, is, little, heroism, in, these, pages., But, it, is, used, with, a, suitable, irony;, and, after, all, this, long, tale, though, it, may, deal, with, folk, in, frock, coats, furbelows, and, a, gilt-edged, period, is, not, devoid, of, the, essential, heat, of, conflict., Discounting, for, the, gigantic, stature, and, blood-thirstiness, of, old, days, as, they, hav…
scala> lines.flatMap(_.split(" "))
res4: List[String] = List(Preface, “The, Forsyte, Saga”, was, the, title, originally, destined, for, that, part, of, it, which, is, called, “The, Man, of, Property”;, and, to, adopt, it, for, the, collected, chronicles, of, the, Forsyte, family, has, indulged, the, Forsytean, tenacity, that, is, in, all, of, us., The, word, Saga, might, be, objected, to, on, the, ground, that, it, connotes, the, heroic, and, that, there, is, little, heroism, in, these, pages., But, it, is, used, with, a, suitable, irony;, and, after, all, this, long, tale, though, it, may, deal, with, folk, in, frock, coats, furbelows, and, a, gilt-edged, period, is, not, devoid, of, the, essential, heat, of, conflict., Discounting, for, the, gigantic, stature, and, blood-thirstiness, of, old, days, as, they, hav…
scala> val s1 = List(Range(0,9),Range(0,9),Range(0,9))
s1: List[scala.collection.immutable.Range] = List(Range(0, 1, 2, 3, 4, 5, 6, 7, 8), Range(0, 1, 2, 3, 4, 5, 6, 7, 8), Range(0, 1, 2, 3, 4, 5, 6, 7, 8))
scala> sl.map(x=>x.map(x*2))
:13: error: not found: value sl
sl.map(x=>x.map(x*2))
^
scala> s1.map(x=>x.map(_2))*
res7: List[scala.collection.immutable.IndexedSeq[Int]] = List(Vector(0, 2, 4, 6, 8, 10, 12, 14, 16), Vector(0, 2, 4, 6, 8, 10, 12, 14, 16), Vector(0, 2, 4, 6, 8, 10, 12, 14, 16))
*scala> s1.map(.map(2))
res8: List[scala.collection.immutable.IndexedSeq[Int]] = List(Vector(0, 2, 4, 6, 8, 10, 12, 14, 16), Vector(0, 2, 4, 6, 8, 10, 12, 14, 16), Vector(0, 2, 4, 6, 8, 10, 12, 14, 16))
*scala> s1.map(.map(2)).flat
flatMap flatten
*scala> s1.map(.map(2)).flatten
res9: List[Int] = List(0, 2, 4, 6, 8, 10, 12, 14, 16, 0, 2, 4, 6, 8, 10, 12, 14, 16, 0, 2, 4, 6, 8, 10, 12, 14, 16)
*scala> s1.flatMap(.map(2))
res10: List[Int] = List(0, 2, 4, 6, 8, 10, 12, 14, 16, 0, 2, 4, 6, 8, 10, 12, 14, 16, 0, 2, 4, 6, 8, 10, 12, 14, 16)
scala> lines.flatMap(.split(" ")).map((,1))
res12: List[(String, Int)] = List((Preface,1), (“The,1), (Forsyte,1), (Saga”,1), (was,1), (the,1), (title,1), (originally,1), (destined,1), (for,1), (that,1), (part,1), (of,1), (it,1), (which,1), (is,1), (called,1), (“The,1), (Man,1), (of,1), (Property”;,1), (and,1), (to,1), (adopt,1), (it,1), (for,1), (the,1), (collected,1), (chronicles,1), (of,1), (the,1), (Forsyte,1), (family,1), (has,1), (indulged,1), (the,1), (Forsytean,1), (tenacity,1), (that,1), (is,1), (in,1), (all,1), (of,1), (us.,1), (The,1), (word,1), (Saga,1), (might,1), (be,1), (objected,1), (to,1), (on,1), (the,1), (ground,1), (that,1), (it,1), (connotes,1), (the,1), (heroic,1), (and,1), (that,1), (there,1), (is,1), (little,1), (heroism,1), (in,1), (these,1), (pages.,1), (But,1), (it,1), (is,1), (used,1), (with,1), (a,1), …
scala> lines.flatMap(_.split(" ")).map(x=>(x,1))
res13: List[(String, Int)] = List((Preface,1), (“The,1), (Forsyte,1), (Saga”,1), (was,1), (the,1), (title,1), (originally,1), (destined,1), (for,1), (that,1), (part,1), (of,1), (it,1), (which,1), (is,1), (called,1), (“The,1), (Man,1), (of,1), (Property”;,1), (and,1), (to,1), (adopt,1), (it,1), (for,1), (the,1), (collected,1), (chronicles,1), (of,1), (the,1), (Forsyte,1), (family,1), (has,1), (indulged,1), (the,1), (Forsytean,1), (tenacity,1), (that,1), (is,1), (in,1), (all,1), (of,1), (us.,1), (The,1), (word,1), (Saga,1), (might,1), (be,1), (objected,1), (to,1), (on,1), (the,1), (ground,1), (that,1), (it,1), (connotes,1), (the,1), (heroic,1), (and,1), (that,1), (there,1), (is,1), (little,1), (heroism,1), (in,1), (these,1), (pages.,1), (But,1), (it,1), (is,1), (used,1), (with,1), (a,1), …
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1)
res14: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(unlikely. -> List((unlikely.,1)), come? -> List((come?,1)), unexpectedly; -> List((unexpectedly;,1)), easel. -> List((easel.,1)), herself. -> List((herself.,1), (herself.,1), (herself.,1), (herself.,1), (herself.,1)), hand’ -> List((hand’,1)), incident -> List((incident,1), (incident,1), (incident,1), (incident,1)), buns -> List((buns,1)), serious -> List((serious,1), (serious,1), (serious,1), (serious,1), (serious,1)), “Let’s -> List((“Let’s,1), (“Let’s,1)), sinister -> List((sinister,1)), breaks -> List((breaks,1)), sure,” -> List((sure,”,1)), savour, -> List((savour,1)), mutton.’ -> List((mutton.’,1)), forgotten -> List((forgotten,1), (forgotten,1), (forgotten,1), (forgotten,1), (forgotten,1), (forgotten,1), (fo…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1).map(x=>(x._1,x._2.size))
res16: scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1, easel. -> 1, herself. -> 5, hand’ -> 1, incident -> 4, buns -> 1, serious -> 5, “Let’s -> 2, sinister -> 1, breaks -> 1, sure,” -> 1, savour, -> 1, mutton.’ -> 1, forgotten -> 20, precious -> 8, leer -> 1, compliment -> 2, got, -> 1, embedded -> 2, lover -> 13, of. -> 3, “Yes” -> 1, “will -> 2, pasture -> 1, speaker -> 1, terrible -> 15, lion -> 2, cap, -> 2, stacks, -> 1, inevitable -> 1, brow; -> 2, which? -> 1, assert -> 1, Inventor. -> 1, lights -> 2, rage -> 6, submitted -> 1, Talk -> 1, crispness -> 1, beard, -> 3, policeman’s -> 1, snow -> 1, humbug.” -> 1, wall. -> 6, muttered. -> 3, remarked -> 4, Club, -> 7, “There!” -> 2, disinclined -> 1, laughing -> 1, coat, -> 12, darken…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1).map(x=>(x._1,x._2.size))
res16: scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1, easel. -> 1, herself. -> 5, hand’ -> 1, incident -> 4, buns -> 1, serious -> 5, “Let’s -> 2, sinister -> 1, breaks -> 1, sure,” -> 1, savour, -> 1, mutton.’ -> 1, forgotten -> 20, precious -> 8, leer -> 1, compliment -> 2, got, -> 1, embedded -> 2, lover -> 13, of. -> 3, “Yes” -> 1, “will -> 2, pasture -> 1, speaker -> 1, terrible -> 15, lion -> 2, cap, -> 2, stacks, -> 1, inevitable -> 1, brow; -> 2, which? -> 1, assert -> 1, Inventor. -> 1, lights -> 2, rage -> 6, submitted -> 1, Talk -> 1, crispness -> 1, beard, -> 3, policeman’s -> 1, snow -> 1, humbug.” -> 1, wall. -> 6, muttered. -> 3, remarked -> 4, Club, -> 7, “There!” -> 2, disinclined -> 1, laughing -> 1, coat, -> 12, darken…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1).map(x=>(x._1,x._2.length))
res17: scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1, easel. -> 1, herself. -> 5, hand’ -> 1, incident -> 4, buns -> 1, serious -> 5, “Let’s -> 2, sinister -> 1, breaks -> 1, sure,” -> 1, savour, -> 1, mutton.’ -> 1, forgotten -> 20, precious -> 8, leer -> 1, compliment -> 2, got, -> 1, embedded -> 2, lover -> 13, of. -> 3, “Yes” -> 1, “will -> 2, pasture -> 1, speaker -> 1, terrible -> 15, lion -> 2, cap, -> 2, stacks, -> 1, inevitable -> 1, brow; -> 2, which? -> 1, assert -> 1, Inventor. -> 1, lights -> 2, rage -> 6, submitted -> 1, Talk -> 1, crispness -> 1, beard, -> 3, policeman’s -> 1, snow -> 1, humbug.” -> 1, wall. -> 6, muttered. -> 3, remarked -> 4, Club, -> 7, “There!” -> 2, disinclined -> 1, laughing -> 1, coat, -> 12, darken…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1).map(x=>(x._1,x.2.map(._2).sum))
res19: scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1, easel. -> 1, herself. -> 5, hand’ -> 1, incident -> 4, buns -> 1, serious -> 5, “Let’s -> 2, sinister -> 1, breaks -> 1, sure,” -> 1, savour, -> 1, mutton.’ -> 1, forgotten -> 20, precious -> 8, leer -> 1, compliment -> 2, got, -> 1, embedded -> 2, lover -> 13, of. -> 3, “Yes” -> 1, “will -> 2, pasture -> 1, speaker -> 1, terrible -> 15, lion -> 2, cap, -> 2, stacks, -> 1, inevitable -> 1, brow; -> 2, which? -> 1, assert -> 1, Inventor. -> 1, lights -> 2, rage -> 6, submitted -> 1, Talk -> 1, crispness -> 1, beard, -> 3, policeman’s -> 1, snow -> 1, humbug.” -> 1, wall. -> 6, muttered. -> 3, remarked -> 4, Club, -> 7, “There!” -> 2, disinclined -> 1, laughing -> 1, coat, -> 12, darken…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x._1).map(x=>(x.1,x.2.map(.2).reduce(+)))
res20: scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1, easel. -> 1, herself. -> 5, hand’ -> 1, incident -> 4, buns -> 1, serious -> 5, “Let’s -> 2, sinister -> 1, breaks -> 1, sure,” -> 1, savour, -> 1, mutton.’ -> 1, forgotten -> 20, precious -> 8, leer -> 1, compliment -> 2, got, -> 1, embedded -> 2, lover -> 13, of. -> 3, “Yes” -> 1, “will -> 2, pasture -> 1, speaker -> 1, terrible -> 15, lion -> 2, cap, -> 2, stacks, -> 1, inevitable -> 1, brow; -> 2, which? -> 1, assert -> 1, Inventor. -> 1, lights -> 2, rage -> 6, submitted -> 1, Talk -> 1, crispness -> 1, beard, -> 3, policeman’s -> 1, snow -> 1, humbug.” -> 1, wall. -> 6, muttered. -> 3, remarked -> 4, Club, -> 7, “There!” -> 2, disinclined -> 1, laughing -> 1, coat, -> 12, darken…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x.1).map(x=>(x.1,x.2.map(.2).reduce(+))).toList.sortBy(._2).reverse
res22: List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526), (that,1273), (with,1029), (her,1020), (—,931), (at,815), (for,765), (not,723), (she,711), (He,695), (it,689), (on,668), (as,655), (him,470), (I,454), (be,452), (would,436), (all,423), (you,420), (The,412), (this,412), (but,411), (were,400), (by,391), (from,388), (they,368), (have,350), (an,344), (so,343), (no,338), (which,337), (been,337), (could,314), (Soames,303), (like,301), (.,292), (their,283), (old,277), (what,265), (into,262), (out,255), (or,253), (And,253), (Jolyon,251), (She,243), (It,242), (up,238), (said,236), (little,235), (is,232), (who,231), (there,221), (if,221), (one,218), (never,213), (about,210), (did,209), (very,188), (ove…
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x.1).map(x=>(x.1,x.2.map(.2).reduce(+))).toList.sortBy(._2).reverse.slice(0,5)
res23: List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543))
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x.1).map(x=>(x.1,x.2.map(.2).reduce(+))).toList.sortBy(._2).reverse.slice(0,10)
res24: List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526))
scala> lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(x=>x.1).map(x=>(x.1,x.2.map(.2).reduce(+))).toList.sortBy(._2).reverse.slice(0,10)(1)
res25: (String, Int) = (of,3407)
scala> lines.flatMap(.split(" ")).map(x=>(x,1)).groupBy(x=>x.1).mapValues(.size).toArray.sortWith(.2>._2)
res29: Array[(String, Int)] = Array((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526), (that,1273), (with,1029), (her,1020), (—,931), (at,815), (for,765), (not,723), (she,711), (He,695), (it,689), (on,668), (as,655), (him,470), (I,454), (be,452), (would,436), (all,423), (you,420), (this,412), (The,412), (but,411), (were,400), (by,391), (from,388), (they,368), (have,350), (an,344), (so,343), (no,338), (been,337), (which,337), (could,314), (Soames,303), (like,301), (.,292), (their,283), (old,277), (what,265), (into,262), (out,255), (And,253), (or,253), (Jolyon,251), (She,243), (It,242), (up,238), (said,236), (little,235), (is,232), (who,231), (if,221), (there,221), (one,218), (never,213), (about,210), (did,209), (over,188), (v…
scala> val a = Range(0,100)
a: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
scala> a.fold
fold foldLeft foldRight
scala> a.foldLeft(0)(+)
res31: Int = 4950
scala> val b = Array((“who”,3), (“if”,2), (“there”,1))
b: Array[(String, Int)] = Array((who,3), (if,2), (there,1))
scala> b.fold
fold foldLeft foldRight
scala> b.foldLeft(0)(+._2)
res33: Int = 6
scala> val ss = "235234sgsdfgs"
ss: String = 235234sgsdfgs
scala> p.findAllIn(ss).toArray
res36: Array[String] = Array(235234)
scala> p.findAllIn(ss).toArray.foreach(x=>print(x))
235234
scala> p.findAllIn(ss).mkString("")
res38: String = 235234
scala> p.findAllIn(ss).mkString("[","","]")
res39: String = [235234]
scala> a
res40: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
scala> a.mkString
def mkString: String def mkString(sep: String): String def mkString(start: String,sep: String,end: String): String
scala> a.mkString("_")
res41: String = 0_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_25_26_27_28_29_30_31_32_33_34_35_36_37_38_39_40_41_42_43_44_45_46_47_48_49_50_51_52_53_54_55_56_57_58_59_60_61_62_63_64_65_66_67_68_69_70_71_72_73_74_75_76_77_78_79_80_81_82_83_84_85_86_87_88_89_90_91_92_93_94_95_96_97_98_99
scala> val p = “[0-9a-zA-Z]+”.r
p: scala.util.matching.Regex = [0-9a-zA-Z]+
scala> lines.flatMap(_.split(" “)).map(x=>(p.findAllIn(x).mkString(”"),1))
res42: List[(String, Int)] = List((Preface,1), (The,1), (Forsyte,1), (Saga,1), (was,1), (the,1), (title,1), (originally,1), (destined,1), (for,1), (that,1), (part,1), (of,1), (it,1), (which,1), (is,1), (called,1), (The,1), (Man,1), (of,1), (Property,1), (and,1), (to,1), (adopt,1), (it,1), (for,1), (the,1), (collected,1), (chronicles,1), (of,1), (the,1), (Forsyte,1), (family,1), (has,1), (indulged,1), (the,1), (Forsytean,1), (tenacity,1), (that,1), (is,1), (in,1), (all,1), (of,1), (us,1), (The,1), (word,1), (Saga,1), (might,1), (be,1), (objected,1), (to,1), (on,1), (the,1), (ground,1), (that,1), (it,1), (connotes,1), (the,1), (heroic,1), (and,1), (that,1), (there,1), (is,1), (little,1), (heroism,1), (in,1), (these,1), (pages,1), (But,1), (it,1), (is,1), (used,1), (with,1), (a,1), (suitab…