Scala函数

scala常用函数列举

函数传名调用(Call-by-Name) 指定函数参数名
函数 - 可变参数
递归函数
默认参数值
高阶函数
内嵌函数
匿名函数
偏应用函数 函数柯里化(Function Currying)

/**
  * Scala 中的方法跟 Java 的类似,方法是组成类的一部分。
  *
  * Scala 中的函数则是一个完整的对象,Scala 中的函数其实就是继承了 Trait 的类的对象。
  *
  * Scala 中使用 val 语句可以定义函数,def 语句定义方法。
  */
object MethodFunctionDemo {
    // def functionName([参数列表]):[return type]
    // function body
    // return [expr]
    // 抽象
    def myMethod(x: Int) = x+3
    //
    val myFunction1 = (x: Int) => x+3

  def main(args: Array[String]): Unit = {
    println(HigherOrderFunction.apply(HigherOrderFunction.layout,17))
    println(HigherOrderFunction.applyStudentBean(HigherOrderFunction.layout,new StudentBean("3")))

    for(i <- 1 to 10 ){
      println("Recursion ("+i+")的阶乘 : "+Recursion.factorial(i))
    }
    println(Currying.strcat("hello")("scala curring"))
    println(Anonymous.multipiler(2,4))
    TestCallByName.delayed(1+2)
    TestCallByName.delayed(TestCallByName.time())
    println(this.myMethod(4))
    println(this.myFunction1(4))
    Hello.printMe()
    //Hello.printYou()
    println(add.addInt(1,3))
  }
}

object add{
  def addInt(a: Int,b:Int):Int = {
    var sum:Int = 0
    sum = a+b
    return sum
  }
}

object Hello {
  def printMe(): Unit ={
    println("hello scala unit !")
  }

//  def printYou(): Void ={ 用java的void作为返回类型不行
//    println("hello scala void !")
//    Void
//  }
}
/**
  * 传值调用(call-by-value):先计算参数表达式的值,再应用到函数内部;
  * 传名调用(call-by-name):将未计算的参数表达式直接应用到函数内部
  * 在进入函数内部前,传值调用方式就已经将参数表达式的值计算完毕,而传名调用是在函数内部进行参数表达式的值计算的。
  *
  * 这就造成了一种现象,每次使用传名调用时,解释器都会计算一次表达式的值
  */
object TestCallByName {
  def time() = {
    println("获取时间,单位为纳秒")
    System.nanoTime
  }
  // 该方法在变量名和变量类型使用 => 符号来设置传名调用
  def delayed( t: => Long ) = {
    println("在 delayed 方法内")
    println("参数: " + t*2)
    t*2
  }
}

/**
* 递归函数
**/
object Recursion{
  def factorial(n:BigInt): BigInt ={
    if(n<=1)
      1
    else
      n * factorial(n-1)
  }
}
/**
* 高阶函数
**/
object HigherOrderFunction{
  def apply(f:Int => String,v: Int)=f(v)
  // g:StudentBean => String 1、作为第一个参数,运算表达式传入到第二个g(v)中 、2参数本身也是表达式(传入StudentBean对象,输出String类型)
  // v:StudentBean   作为第二个参数
  def applyStudentBean(g:StudentBean => String,v:StudentBean) = g(v)
  def layout[A](x:A) = "["+x.toString+"]"
}

/**
* 匿名函数
**/
object Anonymous{
  val factor = 3
  def multipiler = (x:Int,y:Int) => x*y*factor
}
/**
* 柯里化函数
**/
object Currying{
  // strcat(str1: String) 传入参数str1,返回一个匿名函数,
  // 匿名函数处理(str2: String),str2传给匿名函数,返回String类型
  // 匿名函数处理的函数体是{ str1+" "+str2 }
  def strcat(str1: String)(str2: String) = {
    str1+" "+str2
  }
}

引用到的java对象

/**
 * @author 
 * @description
 * @date 2021-10-1
 */
public class StudentBean {
    private String no;
    private String name;
    private int age;

    public String getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentBean{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public StudentBean(String no) {
        this.no = no;
        this.name = "张三";
        this.age=10;
    }
}

参考 https://www.runoob.com/scala/scala-functions.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值