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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | object A { //统一java数值型和引用型在scala是 Any var echo = (arg:Any)=> println (arg); //匿名方法赋值给变量的方法实际上产生了一个FunctionN对象,并产生了一个这个对象的apply()方法,方法的参数个数就是匿名函数的参数个数 val x = (arg:String)=>arg; //val a = Array.apply(1,2,3); val j = List(a:_*); //val k = Array.apply(a:_*);//no `: _*' annotation allowed here (such annotations are only allowed in arguments to *-parameters) val a = 1 to 100 ; //函数声明 在输入中指定类型,输出类型在函数定义中 val even = (x:Int)=> x % 2 == 0 ; //函数声明时将输入输出类型指定,然后后面直接定义函数体 val odd:Int=>Boolean = x => x % 2 == 1 ; val list =(a:Range,m:Int=>Boolean) => a.filter(m); val aa = list(a,even).mkString( "," ); val hasChar = (s:String,c:Char)=>s.exists((p:Char)=>p==c); def fab(x:Int,y:List[Int]):List[Int]= { if (x== 0 ) return y. reverse ; val a::b::rest = y; fab(x- 1 ,a+b::y); } val u = fab( 15 ,List( 1 , 1 )); val f = (p:Int)=> print (p+ "\t" ); //变长参数 def sum(nums:Int*):Int= { var x = 0 ; nums.foreach(x+=_); return x; } //伴生对象的apply方法可以直接用类名调用 H(a,b,c); def apply(nums:Int*):Int= { var x = 0 ; nums.foreach(x+=_); return x; } def main(args:Array[String]):Unit= { println (sum( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 )); val j = Array( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ); //将数组传入变长参数函数中的方法 println (sum(j:_*)); print (A( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 )); // val echo = (args:String*) => args.foreach(println);//error: ')' expected but identifier found. // def echo(arg:String) = println(arg); ok // val x = echo _; ok // var x = (arg:String)=>println(arg); ok var s = println (_:String); //val exex = (a:String=>(),b:String)=>a(b); String=>() is error val exex = (a:String=>Unit,b:String)=>a(b); exex(s, "hello" ); /* val f = (p:Int)=> { val x = p+1; x; //这种写法只能不要 return 否则 执行 f 返回 () } */ def f(p:Int):Int = { val x = p+ 1 ; return x; //这种写法可以要 return 也可以不要 return } print (f( 4 )); } } object AB { type moudle = () => Unit; def x(p:moudle):Unit = { p(); println (); p(); } def main(args:Array[String]):Unit = { x(()=> print ( "hello" )); } } object D { def sum(x:Int,y:Int,z:Int):Int = x+y+z; def main(args:Array[String]):Unit = { val t = sum _; // 将def定义的函数变成变量传递 val s = t( 1 ,_:Int,_:Int); print (s( 3 , 4 )); } } object E { //这样效率高一些,因为反编译后发现直接转化为循环 def fac(x:Int,y:Int):Int = { if (y== 1 ) return x; return fac(x*y,y- 1 ) } def fac2(x:Int):Int = { if (x== 1 ) return 1 ; return x*fac2(x- 1 ); } def main(args:Array[String]):Unit = { println (fac( 1 , 10 )); println (fac2( 10 )); } } object W //几种定义函数的方法 { def main(args:Array[String]):Unit = { val add:(Int,Int)=>Int = (x,y) => x+y; //val 常量名称:(输出参数类型列表)=>返回类型 = (输入参数列表) => 函数定义 val add2 = (x:Int,y:Int) => x + y; //val 常量名称 = (输入参数和类型列表) => 函数定义; def add3(x:Int,y:Int):Int = x + y; // def 函数名称(输入参数和类型列表):返回类型 = 函数定义; // val x:Int=>Int = x => x+1; //直接写 x+1 是不对的 val x:Int=>Int = { case x => x+ 1 ;} } } object X { def main(args:Array[String]):Unit = { //这个地方如果没有 lazy 关键字 repeat 无法实现递归定义,或者放到函数外面变成一个 object 属性变量,也不会立即求值 lazy val repeat : Stream[Int] = 12 #:: repeat; //Stream 是懒加载的列表,换成 List 将出现死循环 val z = repeat take ( 3 ); z.foreach( println ); } } object Y { def main(args:Array[String]):Unit = { // 这个地方如果没有 lazy 关键字 的话定义常量的同时就会求值,所以里面的递归定义将导致对一个未定义完的变量求值 // 或者将这个变量定义到 main 函数外面 变成一个 object 属性变量,也不会立即求值 // 还有就是这种递归函数用 def 直接定义函数会更简单明确 lazy val fact:Int=>Int= { case 0 => 1 ; case n => n*fact(n- 1 ); //或者没有递归的时候也可以不要 lazy 关键字 } print (fact( 10 )); } } object V { import scala.actors.Actor; import scala.actors.Actor._; def main(args:Array[String]):Unit = { //两个 actor 对话 val a = actor { react { case (msg:String,sender:Actor) => println (msg);sender ! msg + " too" ; } } val b = actor { react { //给另外一个 actor 发送消息 同时发送自己的引用, self.act 实现自身的再次调用 case "start" => a ! ( "hello" ,self);self.act(); case msg:String => println (msg); } } b ! "start" ; } } object U { def main(args:Array[String]):Unit = { // 隐式类型转换 自动将 String 转换为一个包含了你想的给String新加了一个方法的匿名类 implicit def arrayOpt(a: String) = new { def -> (b:String) = (a,b); } // 这里如果将左括号写到下一行 scala 会理解为声明了一个空数组,然后又声明了一个元祖 val z = Array( "name" -> "wengmj" , "age" -> "33" ); for(xx <- z) println (xx); } } object T { def sum(x:List[Int]):Int= x match { case List()=> 0 ; case a::b =>a+sum(b); } //这里可以写成 x => x match 或_ match 或完全省略,但是直接写 x match 是错误的 // 变量直接声明称 函数类型 类型为:x => y 的定义方法 如果又用到了 case 语法,case 前的 变量可以理解为参数声明 val s:List[Int]=>Int = { case List()=> 0 ; case a::b =>a+sum(b); } val t=(x:List[Int])=>x match { case List()=> 0 ; case a::b =>a+sum(b); } def main(args:Array[String]):Unit = { // _@_* 匹配数组的多个元素 val Array(a,_,b,_@_*) = Array( 1 , 2 , 8 , 4 , 5 ); println (b); println (s(List( 1 , 2 , 3 ))); } } object Q { def expr(x:Any) = x match { case 5 => "ok" ; case true => "truth" ; case s : String => "string" ; case _ => "other" ; } val exp:Any=>String = { case 5 => "ok" ; case true => "truth" ; case s : String => "string" ; case _ => "other" ; } def main(args:Array[String]):Unit = { println (expr( 4 )); } } object N { def main(args:Array[String]):Unit = { //这个地方是个模式匹配 val State(res) = State( "abcdefg" ) >> (_ * 3 ) >> (_ drop 3 ) println (res); } case class State(var s: String) { type Func = String => String def >> (f: Func) = { s = f(s) this } } } object J { //用折叠操作实现列表的反转 def reverse (x:List[Int]):List[Int] = { val f:(List[Int],Int)=>List[Int] = (x,y)=>y::x; val l = List[Int](); (l/:x)(f); } def main(args:Array[String]):Unit = { val x = List( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ); print ( reverse (x)); } } //插入排序 object I { val sum:Int=>Int = { case 0 => 0 ; case x => x+sum(x- 1 ); } val isort:List[Int]=>List[Int] = { case Nil => Nil; case x::xs1 => insert(x,isort(xs1)); } val insert:(Int,List[Int])=>List[Int] = //这里省略 xx match 的意思是整个输入参数的match { case (x,Nil) => List(x); case (x,y::ys) => if (x<=y) x::y::ys; else y::insert(x,ys); } def main(args:Array[String]):Unit = { val x = isort(List( 1 , 4 , 6 , 2 , 3 , 7 , 9 , 5 , 8 )); print (x.mkString( "," )); } } object H { def main(args:Array[String]):Unit = { //简明易懂,柯里化函数定义最好用 def x()() 方法定义 def loop1(r: Range)(o: Int=> Unit) {r.foreach(o);} //不容易理解 val loop2 = (r:Range) => (o:Int=>Unit) => r.foreach(o); } } case class BinOp(x:Double,op:String,y:Double); object G { def eval(exp:BinOp):Double = exp match { case BinOp(x, "+" ,y) => x+y; case BinOp(x, "-" ,y) => x-y; case BinOp(x, "*" ,y) => x*y; case BinOp(x, "/" ,y) => x/y; } def main(args:Array[String]):Unit = { print (eval(BinOp( 3 , "/" , 5 ))); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee