• Scala-高阶函数


    这里写图片描述

    map的方法filter
    这里写图片描述

    3、

    object gaojie {
    
      def main(args: Array[String]): Unit = {
        (1 to 10).map("*" * _).foreach(println _);
        //数组110 调用高阶函数map()映射 将每一行乘以对应的*
        //然后在依次打印这个数组
      }
    }  

    4、reduce化简

    object gaojie {
    
      def main(args: Array[String]): Unit = {
        val arr = Array(1,3,4,5);
       // val result =  arr.reduceLeft(_ + _);
       // val result = arr.reduceRight(_ * _);
       //val result = arr.reduceRight(_ - _);
       //1-(3-(4-5))
        val result = arr.reduceLeft(_ - _);
       //(((1-3)-4)-5)
        println(result);
    
      }
    }
    

    5、柯里化的过程
    两参变一参

    object gaojie {
    
      def main(args: Array[String]): Unit = {
    
        def fun(a:Int) = {(x : Int)=> a * x;}
        println(fun(1)(2));
    
      }
    }

    6、控制抽象

    
    object controllAbstract {
    
      def newThread(block: Unit){
        new Thread(){
          override def run() ={
            block;
          }
        }.start();
      }
    
      def main(args: Array[String]): Unit = {
        newThread{
          ( 1 to 10).foreach(e => {
            val tname = Thread.currentThread().getName();
            println(tname + ":"+e);
          })
        }
      }
    }
    打印结果:
    main:1
    main:2
    main:3
    main:4
    main:5
    main:6
    main:7
    main:8
    main:9
    main:10
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    C
    B
    A
    G
    BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)
    Codevs_1230_元素查找_(set/Hash)
    POJ_2503_Babelfish_(Trie/map)
    POJ_2001_Shortest_Prefixes_(Trie)
    BZOJ_3670_[NOI2014]_动物园_(kmp)
    BZOJ_3196_二逼平衡树_(树套树,线段树+Treap)
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10327095.html
Copyright © 2020-2023  润新知