scala 基础
// ::链接字符串 链接字符和list
scala> val a = List(1,2,3) a: List[Int] = List(1, 2, 3) scala> val b = 0::a b: List[Int] = List(0, 1, 2, 3) scala> val c = "x"::"y"::"z" :: Nil c: List[String] = List(x, y, z)
// :::链接两个list Any公共父类
scala> val d = a ::: c d: List[Any] = List(1, 2, 3, x, y, z)
//head 返回第一个元素
scala> d.head res20: Any = 1 scala> a.head res21: Int = 1 scala> b.head res22: Int = 0
//tail 除第一个元素以外的其他元素组成的列表-〉伪列表
scala> a.tail res23: List[Int] = List(2, 3) scala> b.tail res24: List[Int] = List(1, 2, 3) scala> c.tail res25: List[String] = List(y, z)
//isEmpty
scala> a.isEmpty res26: Boolean = false scala> Nil.isEmpty res27: Boolean = true scala> def checkEmpty(l : List[Int]):String = { | if(l.isEmpty) "" | else l.head.toString} checkEmpty: (l: List[Int])String scala> checkEmpty(a) res28: String = 1
//filter应用
scala> a res29: List[Int] = List(1, 2, 3) scala> a.filter(x=>x%2==1) res30: List[Int] = List(1, 3)
//toList
scala> "acd1r3wqa".toList res31: List[Char] = List(a, c, d, 1, r, 3, w, q, a)
//isDigit
scala> "acd1r3wqa".toList.filter(x=>Character.isDigit(x)) res33: List[Char] = List(1, 3)
//takeWhile
scala> "acd1r3wqa".toList.takeWhile(x=>Character.isDigit(x)) res34: List[Char] = List() scala> "acd1r3wqa".toList.takeWhile(x=>x != '3') res38: List[Char] = List(a, c, d, 1, r)
//toUpperCase _通配匿名函数
scala> c res41: List[String] = List(x, y, z) scala> c.map(x=>x.toUpperCase) res42: List[String] = List(X, Y, Z) scala> c.map(_.toUpperCase) res43: List[String] = List(X, Y, Z)
//list-map/flatMap可以将两层list转为一层/_多层通配
scala> a res44: List[Int] = List(1, 2, 3) scala> a.filter(_%2==1).map(_+10) res45: List[Int] = List(11, 13) scala> val e =List(a,List(4,5,6)) e: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6)) scala> e.map(x=>x.filter(_%2==0)) res48: List[List[Int]] = List(List(2), List(4, 6)) scala> e.map(_.filter(_%2==0)) res49: List[List[Int]] = List(List(2), List(4, 6)) scala> e.flatMap(_.filter(_%2==0)) res51: List[Int] = List(2, 4, 6)
//Scala reduceLeft与flodLeft
scala> a res52: List[Int] = List(1, 2, 3) scala> a.reduceLeft(_+_) res53: Int = 6 scala> a.reduceLeft((x,y)=>x+y) res54: Int = 6
//flodLeft 类型与最左边参数类型一致
scala> a.foldLeft(0)(_+_) res55: Int = 6 scala> a.foldLeft(1)(_+_) res56: Int = 7 scala> a.foldLeft(4)(_+_) res57: Int = 10 scala> a.foldLeft(4.0)(_+_) res58: Double = 10.0
// Scala Rang与Stream
//Scala Rang
scala> 1 to 10 res59: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 to 10 by 2 res60: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9) scala> (1 to 10).toList res61: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res62: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
//Stream
scala> 1#::2#::3#::Stream.empty res63: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> val stream=(1 to 10000000).toStream stream: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> stream.head res64: Int = 1 scala> stream.tail res65: scala.collection.immutable.Stream[Int] = Stream(2, ?)
//Scala tuple与map
//tuple
scala> (1,2) res66: (Int, Int) = (1,2) scala> 1->2 res67: (Int, Int) = (1,2) scala> (1,"xiaoming","english",99.9) res69: (Int, String, String, Double) = (1,xiaoming,english,99.9) scala> val t=(1,"xiaoming","english",99.9) t: (Int, String, String, Double) = (1,xiaoming,english,99.9) scala> t._1 res71: Int = 1 scala> t._2 res72: String = xiaoming
=====================================================
scala> def sumSq(in:List[Int]):(Int,Int,Int)=in.foldLeft((0,0,0))((t,v)=>(t._1+1,t._2+v,t._3+v*v)) sumSq: (in: List[Int])(Int, Int, Int) scala> a res84: List[Int] = List(1, 2, 3) scala> sumSq(a) res85: (Int, Int, Int) = (3,6,14) scala> sumSq(_) res86: List[Int] => (Int, Int, Int) = <function1>
//map
scala> val f = Map(1->"xiaoming",2->"alisa") f: scala.collection.immutable.Map[Int,String] = Map(1 -> xiaoming, 2 -> alisa) scala> f(1) res88: String = xiaoming scala> f(2) res89: String = alisa scala> f.contains(1) res90: Boolean = true scala> f.contains(3) res91: Boolean = false scala> f.keys res92: Iterable[Int] = Set(1, 2) scala> f.values res93: Iterable[String] = MapLike(xiaoming, alisa) scala> f+(5->"eric") res94: scala.collection.immutable.Map[Int,String] = Map(1 -> xiaoming, 2 -> alisa, 5 -> eric) scala> f - 1 res95: scala.collection.immutable.Map[Int,String] = Map(2 -> alisa) scala> f ++ List(3->"nan",4->"frank") res96: scala.collection.immutable.Map[Int,String] = Map(1 -> xiaoming, 2 -> alisa, 3 -> nan, 4 -> frank) scala> f res97: scala.collection.immutable.Map[Int,String] = Map(1 -> xiaoming, 2 -> alisa) scala> f--List(1,2,3) res98: scala.collection.immutable.Map[Int,String] = Map() scala> f ++ List(3->"nan",4->"frank")--List(1,4) res99: scala.collection.immutable.Map[Int,String] = Map(2 -> alisa, 3 -> nan)
//快速排序
scala> def qSort(a:List[Int]):List[Int]= if (a.length<2)a else qSort(a.filter(_<a.head))++ a.filter(_==a.head)++ qSort(a.filter(_>a.head)) qSort: (a: List[Int])List[Int] scala> qSort(List(1,4,35,76,43,21,53,6)) res81: List[Int] = List(1, 4, 6, 21, 35, 43, 53, 76)