2020-11-21-Scala-复习-8(函数式)

2020-11-23  本文已影响0人  冰菓_

1.Unit () => 的区别

(未解决)

1.() => Unit
2. => Unit
3.Unit => Unit
4.Unit


object Test5 {
  def main(args: Array[String]): Unit = {
       test1((x1)=>println("--"))
       test2(()=>println("-----"))
       test3(println("--------"))
       test4(println("-------------"))
  }

  def test1(f: Unit => Unit) = { }
  def test2(f: () => Unit) = { }
  def test3(f: => Unit) = { }
  def test4(f: Unit) = { }

}

其实code :=> Unit就是code : () => Unit,只是第一种是按名传参,用花括号可以把花括号内的所有东西作为一个函数传给code;第二种就是普通传参,但是它刚好只有一个参数。这时候用了花括号,就是花括号内表达式的值传进去了。

def 和 val的关系其实就是call-by-name和call-by-value的关系,def对应的是by-name,val对应的是by-value
传值调用(call-by-value):先计算参数表达式的值,再应用到函数内部;
传名调用(call-by-name):将未计算的参数表达式直接应用到函数内部

call by name和call by value的区别

object Test4 {
  def main(args: Array[String]): Unit = {
    show(fun())
    println("====")
    show1(fun())
  }

  def show(f: Int): Unit = {
    println(s"1 first : $f ")
    println(s"2 second : $f ")
  }

  def show1(f: => Int): Unit = {
    println(s"1 first : $f ")
    println(s"2 second : $f ")
  }

  def fun(): Int = {
    println(".......")
    1
  }
}

2.偏函数实现过滤功能

object Test3 {
  def main(args: Array[String]): Unit = {
    var array = Array[Int](1, 2, 3, 4, 5, 6, 7)
    array.collect(divide2).foreach(println)
    array.collect({ case d: Int if (d % 2 == 0) => d }).foreach(println)
  }

  val divide2: PartialFunction[Int, Int] = {
    case d: Int if (d % 2 == 0) => d
  }
}

3.Fuction的多种写法(函数的类型与类型推断)

object Test6 {
  def main(args: Array[String]): Unit = {

  }
   //1.反思为什么可以这样写
   val f : (Int,Double)=>Double =(x:Int,y:Double)=>x+y
   //2.实际上发生了什么:省略了Function2,一共有22个
   val f1 :Function2[Int,Double,Double]=(x:Int,y:Double)=>x+y
   //3.进一步思考为什么能这样写:实际上(x:Int,y:Double)=>x+y 是调用了apply方法 new了一个特质,调用函数实际上是调用了apply方法
   val f2 :Function2[Int,Double,Double]=new Function2[Int,Double,Double] {
     override def apply(v1: Int, v2: Double): Double =  v1+v2
   }
   //4.我们可以进一步的简化
   val f4 =(x:Int,y:Double)=>x+y
   //5.怎么表示元组
   val tuple :Function2[Int,Double,Tuple2[Double,Int]]=new Function2[Int,Double,Tuple2[Double,Int]] {
     override def apply(v1: Int, v2: Double): (Double, Int) = (v2,v1)
   }
}

4.java实现scala中的map reduce filter功能(泛型的使用)

public interface MyApply<A,B> {
    //接口作用是什么,重写其中的方法实现特定的功能
    //接收一种类型返回B类型
    B  apply(A a);
}
public interface MyReduce<A,B,C> {
     C apply(A a,B b);
}
import java.util.ArrayList;
import java.util.List;

//要实现map功能,要继承list集合
//使用泛型,提高复用性
public class MyList<T> extends ArrayList<T> {
    //空参构造方法
    public MyList() {
        super();
    }

    //实现map方法
    //为什么不能使用static,因为泛型要实例化,而static是先于实例化的
    public <S> MyList<S> map(MyApply<T, S> myApply) {
        //我需要new 一个集合装载我map过的数据
        MyList<S> s = new MyList<>();
        //遍历
        for (T t : this) {
            S s1 = myApply.apply(t);
            s.add(s1);
        }
        return s;
    }

    //实现过滤
    public <S> MyList<T> filter(MyApply<T, Boolean> myApply) {
        //创建一个集合接收过滤后的数据
        MyList<T> ts = new MyList<>();
        //满足条件就添加到集合中
        for (T t : this) {
            if (myApply.apply(t)) {
                ts.add(t);
            }
        }
        return  ts;
    }
    //实现reduce功能,前面的所有数据作为一个值与后面的值较小操作,返回值是一个数值
    public  T  reduce(MyReduce<T,T,T> myReduce){
           //使用一个flog来获取第一个值
           T t = null;
           Boolean flog = true;
               for (T t1 : this) {
                   if (flog){
                     t = t1;
                     flog = false;
               }
                   else{
                        t = myReduce.apply(t,t1);
                   }
           }
           return  t;
    }
}
import java.util.List;

public class MylistDemo {
    public static void main(String[] args) {
        //实现map功能,映射功能
        MyList<Integer> list = new MyList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        MyList<Integer> map = list.map(new MyApply<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                return integer + 1;
            }
        });
        System.out.println(map);
        //使用lambda表达式
        list.map(x -> x + 1).forEach(System.out::println);

        //实现过滤功能 filter
        MyList<Integer> filter = list.filter(new MyApply<Integer, Boolean>() {
            @Override
            public Boolean apply(Integer integer) {
                return integer % 2 == 1;
            }
        });
        System.out.println(filter);
        //使用lambda表达式
        list.filter(x -> x % 2 == 1).forEach(System.out::println);

        Integer reduce = list.reduce(new MyReduce<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        });
        System.out.println(reduce);
        System.out.println(list.reduce(Integer::sum));
    }
}
上一篇 下一篇

猜你喜欢

热点阅读