zoukankan      html  css  js  c++  java
  • 函数式编程

    函数式编程语言操纵代码片段就像操作数据一样容易。 虽然 Java 不是函数式语言,但 Java 8 Lambda 表达式和方法引用 (Method References) 允许你以函数式编程。

    // functional/Strategize.java
    
    interface Strategy {
      String approach(String msg);
    }
    
    class Soft implements Strategy {
      public String approach(String msg) {
        return msg.toLowerCase() + "?";
      }
    }
    
    class Unrelated {
      static String twice(String msg) {
        return msg + " " + msg;
      }
    }
    
    public class Strategize {
      Strategy strategy;
      String msg;
      Strategize(String msg) {
        strategy = new Soft(); // [1]
        this.msg = msg;
      }
    
      void communicate() {
        System.out.println(strategy.approach(msg));
      }
    
      void changeStrategy(Strategy strategy) {
        this.strategy = strategy;
      }
    
      public static void main(String[] args) {
        Strategy[] strategies = {
          new Strategy() { // [2]
            public String approach(String msg) {
              return msg.toUpperCase() + "!";
            }
          },
          msg -> msg.substring(0, 5), // [3]
          Unrelated::twice // [4]
        };
        Strategize s = new Strategize("Hello there");
        s.communicate();
        for(Strategy newStrategy : strategies) {
          s.changeStrategy(newStrategy); // [5]
          s.communicate(); // [6]
        }
      }
    }
    • [1] 在 Strategize 中,Soft 作为默认策略,在构造函数中赋值。

    • [2] 一种略显简短且更自发的方法是创建一个匿名内部类。即使这样,仍有相当数量的冗余代码。你总是要仔细观察:“哦,原来这样,这里使用了匿名内部类。”

    • [3] Java 8 的 Lambda 表达式。由箭头 -> 分隔开参数和函数体,箭头左边是参数,箭头右侧是从 Lambda 返回的表达式,即函数体。这实现了与定义类、匿名内部类相同的效果,但代码少得多。

    • [4] Java 8 的方法引用,由 :: 区分。在 :: 的左边是类或对象的名称,在 :: 的右边是方法的名称,但没有参数列表。

    • [5] 在使用默认的 Soft strategy 之后,我们逐步遍历数组中的所有 Strategy,并使用 changeStrategy() 方法将每个 Strategy 放入 变量 s 中。

    • [6] 现在,每次调用 communicate() 都会产生不同的行为,具体取决于此刻正在使用的策略代码对象。我们传递的是行为,而非仅数据。[^3]

    这段代码debug了几遍才懂得,我之前对函数式编程了解不多。现在是2019年了,不会函数式编程不算程序员。

    // functional/LambdaExpressions.java
    
    interface Description {
      String brief();
    }
    
    interface Body {
      String detailed(String head);
    }
    
    interface Multi {
      String twoArg(String head, Double d);
    }
    
    public class LambdaExpressions {
    
      static Body bod = h -> h + " No Parens!"; // [1]
    
      static Body bod2 = (h) -> h + " More details"; // [2]
    
      static Description desc = () -> "Short info"; // [3]
    
      static Multi mult = (h, n) -> h + n; // [4]
    
      static Description moreLines = () -> { // [5]
        System.out.println("moreLines()");
        return "from moreLines()";
      };
    
      public static void main(String[] args) {
        System.out.println(bod.detailed("Oh!"));
        System.out.println(bod2.detailed("Hi!"));
        System.out.println(desc.brief());
        System.out.println(mult.twoArg("Pi! ", 3.14159));
        System.out.println(moreLines.brief());
      }
    }

    输出结果

    Oh! No Parens!
    Hi! More details
    Short info
    Pi! 3.14159
    moreLines()
    from moreLines()

    Lambda的语法就是

    1.参数,也就是-> 左边的部分,可以用()也可以不用。

    2.方法体。->右边的部分,也就是这个方法的执行逻辑

    1. 参数。

    2. 接着 ->,可视为“产出”。

    3. -> 之后的内容都是方法体。

      • [1] 当只用一个参数,可以不需要括号 ()。 然而,这是一个特例。

      • [2] 正常情况使用括号 () 包裹参数。 为了保持一致性,也可以使用括号 () 包裹单个参数,虽然这种情况并不常见。

      • [3] 如果没有参数,则必须使用括号 () 表示空参数列表。

      • [4] 对于多个参数,将参数列表放在括号 () 中。

    到目前为止,所有 Lambda 表达式方法体都是单行。 该表达式的结果自动成为 Lambda 表达式的返回值,在此处使用 return 关键字是非法的。 这是 Lambda 表达式缩写用于描述功能的语法的另一种方式。

    [5] 如果在 Lambda 表达式中确实需要多行,则必须将这些行放在花括号中。 在这种情况下,就需要使用 return。

    Lambda 表达式通常比匿名内部类产生更易读的代码,因此我们将在本书中尽可能使用它们。

    一个递归阶乘的例子

    递归函数是一个自我调用的函数。可以编写递归的 Lambda 表达式,但需要注意:递归方法必须是实例变量或静态变量,否则会出现编译时错误。

    // functional/IntCall.java
    
    interface IntCall {
      int call(int arg);
    }
    // functional/RecursiveFactorial.java
    
    public class RecursiveFactorial {
      static IntCall fact;
      public static void main(String[] args) {
        fact = n -> n == 0 ? 1 : n * fact.call(n - 1);
        for(int i = 0; i <= 10; i++)
          System.out.println(fact.call(i));
      }
    }

    输出

    1
    1
    2
    6
    24
    120
    720
    5040
    40320
    362880
    3628800
    // functional/RecursiveFibonacci.java
    
    public class RecursiveFibonacci {
      IntCall fib;
    
      RecursiveFibonacci() {
        fib = n -> n == 0 ? 0 :
                   n == 1 ? 1 :
                   fib.call(n - 1) + fib.call(n - 2);
      }
    
      int fibonacci(int n) { return fib.call(n); }
    
      public static void main(String[] args) {
        RecursiveFibonacci rf = new RecursiveFibonacci();
        for(int i = 0; i <= 10; i++)
          System.out.println(rf.fibonacci(i));
      }
    }
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55

    方法引用

    // functional/MethodReferences.java
    
    import java.util.*;
    
    interface Callable { // [1]
      void call(String s);
    }
    
    class Describe {
      void show(String msg) { // [2]
        System.out.println(msg);
      }
    }
    
    public class MethodReferences {
      static void hello(String name) { // [3]
        System.out.println("Hello, " + name);
      }
      static class Description {
        String about;
        Description(String desc) { about = desc; }
        void help(String msg) { // [4]
          System.out.println(about + " " + msg);
        }
      }
      static class Helper {
        static void assist(String msg) { // [5]
          System.out.println(msg);
        }
      }
      public static void main(String[] args) {
        Describe d = new Describe();
        Callable c = d::show; // [6]
        c.call("call()"); // [7]
    
        c = MethodReferences::hello; // [8]
        c.call("Bob");
    
        c = new Description("valuable")::help; // [9]
        c.call("information");
    
        c = Helper::assist; // [10]
        c.call("Help!");
      }
    }

    符合签名的就可以调用

    Runnable接口

    // functional/RunnableMethodReference.java
    
    // 方法引用与 Runnable 接口的结合使用
    
    class Go {
      static void go() {
        System.out.println("Go::go()");
      }
    }
    
    public class RunnableMethodReference {
      public static void main(String[] args) {
    
        new Thread(new Runnable() {
          public void run() {
            System.out.println("Anonymous");
          }
        }).start();
    
        new Thread(
          () -> System.out.println("lambda")
        ).start();
    
        new Thread(Go::go).start();
      }
    }
    Anonymous
    lambda
    Go::go()

    这边不理解

    // functional/UnboundMethodReference.java
    
    // 没有方法引用的对象
    
    class X {
      String f() { return "X::f()"; }
    }
    
    interface MakeString {
      String make();
    }
    
    interface TransformX {
      String transform(X x);
    }
    
    public class UnboundMethodReference {
      public static void main(String[] args) {
        // MakeString ms = X::f; // [1]
        TransformX sp = X::f;
        X x = new X();
        System.out.println(sp.transform(x)); // [2]
        System.out.println(x.f()); // 同等效果
      }
    }
    X::f()
    X::f()
    // functional/MultiUnbound.java
    
    // 未绑定的方法与多参数的结合运用
    
    class This {
      void two(int i, double d) {}
      void three(int i, double d, String s) {}
      void four(int i, double d, String s, char c) {}
    }
    
    interface TwoArgs {
      void call2(This athis, int i, double d);
    }
    
    interface ThreeArgs {
      void call3(This athis, int i, double d, String s);
    }
    
    interface FourArgs {
      void call4(
        This athis, int i, double d, String s, char c);
    }
    
    public class MultiUnbound {
      public static void main(String[] args) {
        TwoArgs twoargs = This::two;
        ThreeArgs threeargs = This::three;
        FourArgs fourargs = This::four;
        This athis = new This();
        twoargs.call2(athis, 11, 3.14);
        threeargs.call3(athis, 11, 3.14, "Three");
        fourargs.call4(athis, 11, 3.14, "Four", 'Z');
      }
    }

    掌握了写的敲门就是这种未绑定的引用只能用于方法签名参数不同的时候,但是为什么,还不清楚。

    构造函数的引用

    // functional/CtorReference.java
    
    class Dog {
      String name;
      int age = -1; // For "unknown"
      Dog() { name = "stray"; }
      Dog(String nm) { name = nm; }
      Dog(String nm, int yrs) { name = nm; age = yrs; }
    }
    
    interface MakeNoArgs {
      Dog make();
    }
    
    interface Make1Arg {
      Dog make(String nm);
    }
    
    interface Make2Args {
      Dog make(String nm, int age);
    }
    
    public class CtorReference {
      public static void main(String[] args) {
        MakeNoArgs mna = Dog::new; // [1]
        Make1Arg m1a = Dog::new;   // [2]
        Make2Args m2a = Dog::new;  // [3]
    
        Dog dn = mna.make();
        Dog d1 = m1a.make("Comet");
        Dog d2 = m2a.make("Ralph", 4);
      }
    }

    Dog 有三个构造函数,函数接口内的 make() 方法反映了构造函数参数列表( make() 方法名称可以不同)。

    注意我们如何对 [1],[2] 和 [3] 中的每一个使用 Dog :: new。 这 3 个构造函数只有一个相同名称::: new,但在每种情况下都赋值给不同的接口。编译器可以检测并知道从哪个构造函数引用。

    编译器能识别并调用你的构造函数( 在本例中为 make())。

    在编写接口时,可以使用 @FunctionalInterface 注解强制执行此“函数式方法”模式:

    // functional/FunctionalAnnotation.java
    
    @FunctionalInterface
    interface Functional {
      String goodbye(String arg);
    }
    
    interface FunctionalNoAnn {
      String goodbye(String arg);
    }
    
    /*
    @FunctionalInterface
    interface NotFunctional {
      String goodbye(String arg);
      String hello(String arg);
    }
    产生错误信息:
    NotFunctional is not a functional interface
    multiple non-overriding abstract methods
    found in interface NotFunctional
    */
    
    public class FunctionalAnnotation {
      public String goodbye(String arg) {
        return "Goodbye, " + arg;
      }
      public static void main(String[] args) {
        FunctionalAnnotation fa =
          new FunctionalAnnotation();
        Functional f = fa::goodbye;
        FunctionalNoAnn fna = fa::goodbye;
        // Functional fac = fa; // Incompatible
        Functional fl = a -> "Goodbye, " + a;
        FunctionalNoAnn fnal = a -> "Goodbye, " + a;
      }
    }

    函数式接口,这块需要记忆吗?挺多的,举个例子

    / functional/MethodConversion.java
    
    import java.util.function.*;
    
    class In1 {}
    class In2 {}
    
    public class MethodConversion {
      static void accept(In1 i1, In2 i2) {
        System.out.println("accept()");
      }
      static void someOtherName(In1 i1, In2 i2) {
        System.out.println("someOtherName()");
      }
      public static void main(String[] args) {
        BiConsumer<In1,In2> bic;
    
        bic = MethodConversion::accept;
        bic.accept(new In1(), new In2());
    
        bic = MethodConversion::someOtherName;
        // bic.someOtherName(new In1(), new In2()); // Nope
        bic.accept(new In1(), new In2());
      }
    }
    accept()
    someOtherName()

    这里不要被bic.accept误导,方法引用,这个bic不能直接用引用方法的方法名的,为什么用accept,是因为这个BiConsumer里有accept方法。

    因此,在使用函数接口时,名称无关紧要——只要参数类型和返回类型相同。 Java 会将你的方法映射到接口方法。 要调用方法,可以调用接口的函数式方法名(在本例中为 accept()),而不是你的方法名。

    // functional/ClassFunctionals.java
    
    import java.util.*;
    import java.util.function.*;
    
    class AA {}
    class BB {}
    class CC {}
    
    public class ClassFunctionals {
      static AA f1() { return new AA(); }
      static int f2(AA aa1, AA aa2) { return 1; }
      static void f3(AA aa) {}
      static void f4(AA aa, BB bb) {}
      static CC f5(AA aa) { return new CC(); }
      static CC f6(AA aa, BB bb) { return new CC(); }
      static boolean f7(AA aa) { return true; }
      static boolean f8(AA aa, BB bb) { return true; }
      static AA f9(AA aa) { return new AA(); }
      static AA f10(AA aa1, AA aa2) { return new AA(); }
      public static void main(String[] args) {
        Supplier<AA> s = ClassFunctionals::f1;
        s.get();
        Comparator<AA> c = ClassFunctionals::f2;
        c.compare(new AA(), new AA());
        Consumer<AA> cons = ClassFunctionals::f3;
        cons.accept(new AA());
        BiConsumer<AA,BB> bicons = ClassFunctionals::f4;
        bicons.accept(new AA(), new BB());
        Function<AA,CC> f = ClassFunctionals::f5;
        CC cc = f.apply(new AA());
        BiFunction<AA,BB,CC> bif = ClassFunctionals::f6;
        cc = bif.apply(new AA(), new BB());
        Predicate<AA> p = ClassFunctionals::f7;
        boolean result = p.test(new AA());
        BiPredicate<AA,BB> bip = ClassFunctionals::f8;
        result = bip.test(new AA(), new BB());
        UnaryOperator<AA> uo = ClassFunctionals::f9;
        AA aa = uo.apply(new AA());
        BinaryOperator<AA> bo = ClassFunctionals::f10;
        aa = bo.apply(new AA(), new AA());
      }
    }

    多参数函数式接口

    java.util.functional 中的接口是有限的。比如有了 BiFunction,但它不能变化。 如果需要三参数函数的接口怎么办? 其实这些接口非常简单,很容易查看 Java 库源代码并自行创建。代码示例:

    // functional/TriFunction.java
    
    @FunctionalInterface
    public interface TriFunction<T, U, V, R> {
        R apply(T t, U u, V v);
    }
    // functional/TriFunctionTest.java
    
    public class TriFunctionTest {
      static int f(int i, long l, double d) { return 99; }
      public static void main(String[] args) {
        TriFunction<Integer, Long, Double, Integer> tf =
          TriFunctionTest::f;
        tf = (i, l, d) -> 12;
      }
    }

    缺少基本类型的函数

    // functional/BiConsumerPermutations.java
    
    import java.util.function.*;
    
    public class BiConsumerPermutations {
      static BiConsumer<Integer, Double> bicid = (i, d) ->
        System.out.format("%d, %f%n", i, d);
      static BiConsumer<Double, Integer> bicdi = (d, i) ->
        System.out.format("%d, %f%n", i, d);
      static BiConsumer<Integer, Long> bicil = (i, l) ->
        System.out.format("%d, %d%n", i, l);
      public static void main(String[] args) {
        bicid.accept(47, 11.34);
        bicdi.accept(22.45, 92);
        bicil.accept(1, 11L);
      }
    }
    47, 11.340000
    92, 22.450000
    1, 11

    闭包

    java好像没有类似js那种闭包,不过也是处处闭包。

    推荐看一下这个 java为什么匿名内部类的参数引用时final? - 胖君的回答 - 知乎 https://www.zhihu.com/question/21395848/answer/110829597

    public class Closure {
    
        IntSupplier makeFun(int x) {
            int i = 0;
            return () -> x + i;
    //        return () -> x++ + i++; 编译失败
        }
    }

    我们的lambda是capture by value所以不能需要将x和i都设置为final,为什么不设置final也可以,因为等同final效果。

    public class Closure {
    
        IntSupplier makeFun(int x) {
            int i = 0;
            i++;
            x++;
            final int iFinal = i;
            final int xFinal = x;
            return () -> xFinal + iFinal;
        }
    }

    这里的话其实就final多余了,另外我们需要设置final的地方知识lambda操作的变量,所以这里的x,i就不用考虑final与否的问题了。

    @FunctionalInterface
    public interface IntSupplier {
    
        /**
         * Gets a result.
         *
         * @return a result
         */
        int getAsInt();
    }

    我们用lambda实际上改写了getasint,其实也可以

    // functional/Closure7.java
    
    // {无法编译成功}
    import java.util.function.*;
    
    public class Closure7 {
      IntSupplier makeFun(int x) {
        Integer i = 0;
        i = i + 1;
        return () -> x + i;
      }
    }

    这里虽然是引用,但是这属于jdk智能的处理。个人觉得不太合理。下面这种就合理了。

    // functional/Closure8.java
    
    import java.util.*;
    import java.util.function.*;
    
    public class Closure8 {
      Supplier<List<Integer>> makeFun() {
        final List<Integer> ai = new ArrayList<>();
        ai.add(1);
        return () -> ai;
      }
      public static void main(String[] args) {
        Closure8 c7 = new Closure8();
        List<Integer>
          l1 = c7.makeFun().get(),
          l2 = c7.makeFun().get();
        System.out.println(l1);
        System.out.println(l2);
        l1.add(42);
        l2.add(96);
        System.out.println(l1);
        System.out.println(l2);
      }
    }
    [1]
    [1]
    [1, 42]
    [1, 96]

    我们改变的不是引用,而是引用的对象,所以没有问题。但是下面这种就不行了。

    // functional/Closure9.java
    
    // {无法编译成功}
    import java.util.*;
    import java.util.function.*;
    
    public class Closure9 {
      Supplier<List<Integer>> makeFun() {
        List<Integer> ai = new ArrayList<>();
        ai = new ArrayList<>(); // Reassignment
        return () -> ai;
      }
    }
    @FunctionalInterface
    public interface Supplier<T> {
    
        /**
         * Gets a result.
         *
         * @return a result
         */
        T get();
    }

    可以看到这个是一个函数接口,之前写过类似的,所以可以自己改写一下。

    方法引用

    class test {
        static List<Integer> get() {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            return list;
        }
    }
    public class Closure {
        Supplier<List<Integer>> test1 = test::get;
        public static void main(String[] args) {
            List<Integer> list = test.get();
            System.out.println(list);
            list.add(1);
            System.out.println(list);
        }
    }
    [1, 2]
    [1, 2, 1]

    匿名内部类方式

    // functional/AnonymousClosure.java
    
    import java.util.function.*;
    
    public class AnonymousClosure {
      IntSupplier makeFun(int x) {
        int i = 0;
        // 同样规则的应用:
        // i++; // 非等同 final 效果
        // x++; // 同上
        return new IntSupplier() {
          public int getAsInt() { return x + i; }
        };
      }
    }

    // functional/FunctionComposition.java
    
    import java.util.function.*;
    
    public class FunctionComposition {
      static Function<String, String>
        f1 = s -> {
          System.out.println(s);
          return s.replace('A', '_');
        },
        f2 = s -> s.substring(3),
        f3 = s -> s.toLowerCase(),
        f4 = f1.compose(f2).andThen(f3);
      public static void main(String[] args) {
        System.out.println(
          f4.apply("GO AFTER ALL AMBULANCES"));
      }
    }
    AFTER ALL AMBULANCES
    _fter _ll _mbul_nces
    // functional/PredicateComposition.java
    
    import java.util.function.*;
    import java.util.stream.*;
    
    public class PredicateComposition {
      static Predicate<String>
        p1 = s -> s.contains("bar"),
        p2 = s -> s.length() < 5,
        p3 = s -> s.contains("foo"),
        p4 = p1.negate().and(p2).or(p3);
      public static void main(String[] args) {
        Stream.of("bar", "foobar", "foobaz", "fongopuckey")
          .filter(p4)
          .forEach(System.out::println);
      }
    }
    foobar
    foobaz

    要清晰的意识到一点,我们只是把行为当成参数,本质上还是执行的我们想要的方法体。

    柯里化和部分求值

    柯里化就是将多参数函数转成单参数函数

    // functional/CurryingAndPartials.java
    
    import java.util.function.*;
    
    public class CurryingAndPartials {
       // 未柯里化:
       static String uncurried(String a, String b) {
          return a + b;
       }
       public static void main(String[] args) {
          // 柯里化的函数:
          Function<String, Function<String, String>> sum =
             a -> b -> a + b; // [1]
    
          System.out.println(uncurried("Hi ", "Ho"));
    
          Function<String, String>
            hi = sum.apply("Hi "); // [2]
          System.out.println(hi.apply("Ho"));
    
          // 部分应用:
          Function<String, String> sumHi =
            sum.apply("Hup ");
          System.out.println(sumHi.apply("Ho"));
          System.out.println(sumHi.apply("Hey"));
       }
    }
    Hi Ho
    Hi Ho
    Hup Ho
    Hup Hey
    // functional/Curry3Args.java
    
    import java.util.function.*;
    
    public class Curry3Args {
       public static void main(String[] args) {
          Function<String,
            Function<String,
              Function<String, String>>> sum =
                a -> b -> c -> a + b + c;
          Function<String,
            Function<String, String>> hi =
              sum.apply("Hi ");
          Function<String, String> ho =
            hi.apply("Ho ");
          System.out.println(ho.apply("Hup"));
       }
    }
    Hi Ho Hup

    处理基本类型和装箱时,请使用适当的 Function 接口:

    // functional/CurriedIntAdd.java
    
    import java.util.function.*;
    
    public class CurriedIntAdd {
      public static void main(String[] args) {
        IntFunction<IntUnaryOperator>
          curriedIntAdd = a -> b -> a + b;
        IntUnaryOperator add4 = curriedIntAdd.apply(4);
        System.out.println(add4.applyAsInt(5));
          }
    }
    9
    public static void main(String[] args) {
    IntFunction<IntUnaryOperator>
    curriedIntAdd = a -> b -> a + b;
    IntUnaryOperator add4 = curriedIntAdd.apply(4);
    System.out.println(add4.applyAsInt(44));
    add4.applyAsInt(22);
    System.out.println(add4.applyAsInt(5));
    }
    48
    9

    这里就很体验了函数式编程的本质,实际上我们用lambda只是把行为替换掉了,并且是一个final过程,所以根本不会相互影响,这种简直太适合并发了。

    虽然 Java 支持并发编程,但如果这是你项目的核心部分,你应该考虑在项目部分功能中使用 Scala 或 Clojure 之类的语言。

    函数式编程学到这里基本算是理清了,但是感觉它没有泛型操作等还是差强人意。以后多用下,这个特性讲道理还是不错的。

    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    【题解】Acwing 90 64位整数乘法
    【题解】Acwing 89 a ^ b
    【题解】POJ 1995 Raising Modulo Numbers
    python图像处理(2)图像水印和PIL模式转化
    python图像处理(1)图像的打开与保存
    请把我风干成你的回忆
    python图像处理 模式转化简单总结
    python 不以科学计数法输出
    talib指标公式及释义整理
    cuda、cudnn下载安装教程
  • 原文地址:https://www.cnblogs.com/CherryTab/p/11960613.html
Copyright © 2011-2022 走看看