scala入门初级代码训练-34List的应用函数
原创文章,转载请注明出处!
原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-34/
There I am,in the world more exciting!
by postbird
package com.ptbird.scala
/**
* Created by postbird on 2016/5/26.
*/
object ListTest5 {
def main(args: Array[String]): Unit = {
val l = List(1, 2, 3, 4, 5, 6)
println(l partition (_ % 2 == 0))
println(l find (_ % 2 == 0)) //第一个
println(l find (_ <= 0))
println(l takeWhile (_ < 4)) //所有符合条件的元素
println(l dropWhile (_ < 4))
println(l span (_ < 4))
// (List(2, 4, 6),List(1, 3, 5))
// Some(2)
// None
// List(1, 2, 3)
// List(4, 5, 6)
// (List(1, 2, 3),List(4, 5, 6))
def hastotallyZeroRow(m: List[List[Int]]) = m exists (
row => row forall (_ == 0) //需要全部满足这个函数 List(0, 0, 0)符合
)
val m = List(List(0, 1, 0, 0), List(0, 1, 0), List(0, 0, 0))
println(hastotallyZeroRow(m))
// true
}
}
文章版权:Postbird-There I am , in the world more exciting!
本文链接:http://www.ptbird.cn/scala-chuji-demo-35.html
转载请注明文章原始出处 !
扫描二维码,在手机阅读!