scala 编程思想 --数据
2021/4/26 1:26:29
本文主要是介绍scala 编程思想 --数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.test2 import scala.util.Try object Zipper { def main(args: Array[String]): Unit = { //zip进行数据压缩 val left = Vector("a","b","c","d") val right = Vector("q","r","s","t") println(left.zip(right)) println(left.zip(Range(0,4))) println(left.zipWithIndex) //zipMap case class Person(name:String,ID:Int) val names = Vector("Bob","Jill","Jim") val IDs = Vector(1731,9234,8378) names.zip(IDs).map{case(n,id)=>Person(n,id)} //scala中case的用法 val bools = List(true,false) for(boolprintln("heads") case false=>println("tails") case _=>println("somthing other than heads of tails yikes") } } import scala.util.Random val randomInt = new Random().nextInt(10) randomInt match { case 7 =>println("lucky seven!") case otherNumber =>println("boo,go boring ol "+otherNumber) } //根据顺序匹配 val willWork = List(1,3,24,90) val willNotWork = List(4,28,52) val empty = List() for(lprintln("four elements,with the 2nd being '3'") case List(_*) => println("any other list with 0 or more elements.") } } //case 里面用guard的数组匹配 val tupA = ("Good","Morning!") val tupB = ("Gute","Tag!") for(tup{println("A two tuple starting with good")} case (_1,_2) =>println(_1,_2) } } case class Person1(name:String,age:Int) val alice = Person1("Alice",24) val bob = Person1("Bob",32) val chalie = Person1("Chalie",32) for(personprintln("Hi Alice") case Person1("Bob",25)=>println("Hi Bob!") case Person1(name,age)=>println("who are you,"+age+"year -lold"+name +"?") } } //正则表达式匹配 def yielding4(v:Vector[Int])={ for{ n<-v if n<10 if(n%2 != 0 ) }yield { for(u<-Range(0,n)) yield u } } println(yielding4(Vector(0,1,2,3,4,5,6,7,8,9,10))) // def yielding3(v:Vector[Int]):Vector[Int]={ for{ n<-v if n < 10 if(n%2 != 0) }yield { val u = n * 10 u + 2 } } val v = Vector(1,2,3,4,5,6,7,8,9,10,11,12,17) println(yielding3(v)) Try( println("daffas".toInt) ) } }
这篇关于scala 编程思想 --数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25【机器学习(二)】分类和回归任务-决策树(Decision Tree,DT)算法-Sentosa_DSML社区版
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享