首頁  >  文章  >  Java  >  Java中的函數式程式設計怎麼使用

Java中的函數式程式設計怎麼使用

王林
王林轉載
2023-05-11 17:34:061654瀏覽

    概述

    背景

    函數式程式設計的理論基礎是阿隆佐·丘奇(Alonzo Church)於1930 年代提出的λ 演算(Lambda Calculus)。 λ 演算是一種形式系統,用於研究函數定義、函數應用和遞歸。它為計算理論和計算機科學的發展奠定了基礎。隨著 Haskell(1990年)和 Erlang(1986年)等新一代函數式程式語言的誕生,函數式程式設計開始在實際應用中發揮作用。

    函數式的價值

    隨著硬體越來越便宜,程式的規模和複雜度都在線性的成長。這一切都讓程式設計工作變得困難重重。我們想辦法讓程式碼更加一致和易懂。我們急需一種 文法優雅,簡潔健壯,高並發,容易測試與除錯 的程式設計方式,這一切正是 函數式程式設計(FP) 的意義所在。

    函數式語言已經產生了優雅的語法,這些語法對於非函數式語言也適用。例如:如今Python,Java 8 都在吸收FP 的思想,並且將其融入其中,你也可以這樣想:

    OO(object oriented,面向對象)是抽象數據,FP(functional programming,函數式程式設計)是抽象行為。

    新舊對比

    以傳統形式和 Java 8 的方法引用、Lambda 表達式分別示範。程式碼範例:

    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] 构建默认的 Soft
            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] Java 8 以前的匿名内部类
                        public String approach(String msg) {
                            return msg.toUpperCase() + "!";
                        }
                    },
                    msg -> msg.substring(0, 5), // [3] 基于 Ldmbda 表达式,实例化 interface
                    Unrelated::twice // [4] 基于 方法引用,实例化 interface
            };
            Strategize s = new Strategize("Hello there");
            s.communicate();
            for(Strategy newStrategy : strategies) {
                s.changeStrategy(newStrategy); // [5] 使用默认的 Soft 策略
                s.communicate(); // [6] 每次调用 communicate() 都会产生不同的行为
            }
        }
    }

    輸出結果:

    hello there?
    HELLO THERE!
    Hello
    Hello there Hello there

    #Lambda 表達式

    Lambda 表達式是使用最小可能語法編寫的函數定義:(原則)

    • Lambda 表達式產生函數,而不是類別

    • Lambda 語法盡可能少,這正是為了讓Lambda 易於編寫和使用

    Lambda 用法:

    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] 多行代码情况下使用 `{}` + `return` 关键字
            // (在单行的 Lambda 表达式中 `return` 是非法的)
            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 表達式通常比匿名內部類別產生更易讀的程式碼,因此我們將盡可能使用它們。

    方法引用

    方法引用由類別名稱或物件名稱,後面跟著 :: 然後跟著方法名稱,

    使用範例:

    interface Callable { // [1] 单一方法的接口(重要)
        void call(String s);
    }
    
    class Describe {
        void show(String msg) { // [2] 符合 Callable 接口的 call() 方法实现
            System.out.println(msg);
        }
    }
    
    public class MethodReferences {
        static void hello(String name) { // [3] 也符合 call() 方法实现
            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] 静态类的静态方法,符合 call() 方法
                System.out.println(msg);
            }
        }
    
        public static void main(String[] args) {
            Describe d = new Describe();
            Callable c = d::show; // [6] 通过方法引用创建 Callable 的接口实现
            c.call("call()"); // [7] 通过该实例 call() 方法调用 show() 方法
    
            c = MethodReferences::hello; // [8] 静态方法的方法引用
            c.call("Bob");
    
            c = new Description("valuable")::help; // [9] 实例化对象的方法引用
            c.call("information");
    
            c = Helper::assist; // [10] 静态方法的方法引用
            c.call("Help!");
        }
    }

    輸出結果:

    call()
    Hello, Bob
    valuable information
    Help!

    Runnable 介面

    使用Lambda 和方法引用改變Runnable 介面的寫法:

    // 方法引用与 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();		// 通过 方法引用创建 Runnable 实现的引用
        }
    }

    輸出結果:

    Anonymous
    lambda
    Go::go()

    未綁定的方法參考

    使用未綁定的參考時,需要先提供物件:

    // 未绑定的方法引用是指没有关联对象的普通方法
    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 sp = X::f;       // [1] 你不能在没有 X 对象参数的前提下调用 f(),因为它是 X 的方法
            TransformX sp = X::f;       // [2] 你可以首个参数是 X 对象参数的前提下调用 f(),使用未绑定的引用,函数式的方法不再与方法引用的签名完全相同
            X x = new X();
            System.out.println(sp.transform(x));      // [3] 传入 x 对象,调用 x.f() 方法
            System.out.println(x.f());      // 同等效果
        }
    }

    輸出結果:

    X ::f()
    X::f()

    我們透過更多範例來證明,透過未綁定的方法引用和interface 之間建立關聯:

    package com.github.xiao2shiqi.lambda;
    
    // 未绑定的方法与多参数的结合运用
    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');
        }
    }

    建構函式參考

    可以擷取建構函式的引用,然後透過引用建構物件

    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) {
            // 通过 ::new 关键字赋值给不同的接口,然后通过 make() 构建不同的实例
            MakeNoArgs mna = Dog::new; // [1] 将构造函数的引用交给 MakeNoArgs 接口
            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);
        }
    }

    #總結

    • 方法引用在很大程度上可以理解為創建一個函數式介面的實例

    • 方法引用實際上是一種簡化Lambda 表達式的語法糖,它提供了一種更簡潔的方式來創建一個函數式介面的實作

    • 在程式碼中使用方法引用時,實際上是在建立一個匿名實作類,引用方法實作並且覆寫了介面的抽象方法

    • 方法引用大多用於建立函數式介面的實作

    #函數式介面

    • Lambda包含型別推導

    • Java 8 引入 java.util.function 包,解決型別推導的問題

    透過函數表達式建立Interface:

    // 使用 @FunctionalInterface 注解强制执行此 “函数式方法” 模式
    @FunctionalInterface
    interface Functional {
        String goodbye(String arg);
    }
    
    interface FunctionalNoAnn {
        String goodbye(String arg);
    }
    
    public class FunctionalAnnotation {
        // goodbye
        public String goodbye(String arg) {
            return "Goodbye, " + arg + "!";
        }
    
        public static void main(String[] args) {
            FunctionalAnnotation fa = new FunctionalAnnotation();
    
            // FunctionalAnnotation 没有实现 Functional 接口,所以不能直接赋值
    //        Functional fac = fa;      // Incompatible ?
    
            // 但可以通过 Lambda 将函数赋值给接口 (类型需要匹配)
            Functional f = fa::goodbye;
            FunctionalNoAnn fna = fa::goodbye;
            Functional fl = a -> "Goodbye, " + a;
            FunctionalNoAnn fnal = a -> "Goodbye, " + a;
        }
    }

    以上是自行建立函數式介面的範例。

    但在 java.util.function 套件旨在創建一組完整的預定義接口,使得我們一般情況下不需再定義自己的接口。

    在 java.util.function# 的函數式介面的基本使用基本準測,如下

    • 只處理物件而非基本型,名稱則為Function,Consumer,Predicate 等,參數透過泛型添加

    • 如果接收的參數是基本類型,則由名稱的第一部分錶示,如LongConsumer,DoubleFunction,IntPredicate 等

    • 如果傳回值為基本型,則以To 表示,如ToLongFunction 和IntToLongFunction

    • 如果傳回值類型與參數類型一致,則是運算子

    • 如果接收兩個參數且傳回值為布林值,則是謂詞(Predicate)

    • 如果接收的兩個參數類型不同,則名稱中有一個Bi

    基本类型

    下面枚举了基于 Lambda 表达式的所有不同 Function 变体的示例:

    class Foo {}
    
    class Bar {
        Foo f;
        Bar(Foo f) { this.f = f; }
    }
    
    class IBaz {
        int i;
        IBaz(int i) { this.i = i; }
    }
    
    class LBaz {
        long l;
        LBaz(long l) { this.l = l; }
    }
    
    class DBaz {
        double d;
        DBaz(double d) { this.d = d; }
    }
    
    public class FunctionVariants {
        // 根据不同参数获得对象的函数表达式
        static Function<Foo, Bar> f1 = f -> new Bar(f);
        static IntFunction<IBaz> f2 = i -> new IBaz(i);
        static LongFunction<LBaz> f3 = l -> new LBaz(l);
        static DoubleFunction<DBaz> f4 = d -> new DBaz(d);
        // 根据对象类型参数,获得基本数据类型返回值的函数表达式
        static ToIntFunction<IBaz> f5 = ib -> ib.i;
        static ToLongFunction<LBaz> f6 = lb -> lb.l;
        static ToDoubleFunction<DBaz> f7 = db -> db.d;
        static IntToLongFunction f8 = i -> i;
        static IntToDoubleFunction f9 = i -> i;
        static LongToIntFunction f10 = l -> (int)l;
        static LongToDoubleFunction f11 = l -> l;
        static DoubleToIntFunction f12 = d -> (int)d;
        static DoubleToLongFunction f13 = d -> (long)d;
    
        public static void main(String[] args) {
            // apply usage examples
            Bar b = f1.apply(new Foo());
            IBaz ib = f2.apply(11);
            LBaz lb = f3.apply(11);
            DBaz db = f4.apply(11);
    
            // applyAs* usage examples
            int i = f5.applyAsInt(ib);
            long l = f6.applyAsLong(lb);
            double d = f7.applyAsDouble(db);
    
            // 基本类型的相互转换
            long applyAsLong = f8.applyAsLong(12);
            double applyAsDouble = f9.applyAsDouble(12);
            int applyAsInt = f10.applyAsInt(12);
            double applyAsDouble1 = f11.applyAsDouble(12);
            int applyAsInt1 = f12.applyAsInt(13.0);
            long applyAsLong1 = f13.applyAsLong(13.0);
        }
    }

    以下是用表格整理基本类型相关的函数式接口:

    函数式接口 特征 用途 方法名
    Function469df1b70b2914a3841e0404ec3e3d5d 接受一个参数,返回一个结果 将输入参数转换成输出结果,如数据转换或映射操作 R apply(T t)
    IntFunction 接受一个 int 参数,返回一个结果 将 int 值转换成输出结果 R apply(int value)
    LongFunction 接受一个 long 参数,返回一个结果 将 long 值转换成输出结果 R apply(long value)
    DoubleFunction 接受一个 double 参数,返回一个结果 将 double 值转换成输出结果 R apply(double value)
    ToIntFunction 接受一个参数,返回一个 int 结果 将输入参数转换成 int 输出结果 int applyAsInt(T value)
    ToLongFunction 接受一个参数,返回一个 long 结果 将输入参数转换成 long 输出结果 long applyAsLong(T value)
    ToDoubleFunction 接受一个参数,返回一个 double 结果 将输入参数转换成 double 输出结果 double applyAsDouble(T value)
    IntToLongFunction 接受一个 int 参数,返回一个 long 结果 将 int 值转换成 long 输出结果 long applyAsLong(int value)
    IntToDoubleFunction 接受一个 int 参数,返回一个 double 结果 将 int 值转换成 double 输出结果 double applyAsDouble(int value)
    LongToIntFunction 接受一个 long 参数,返回一个 int 结果 将 long 值转换成 int 输出结果 int applyAsInt(long value)
    LongToDoubleFunction 接受一个 long 参数,返回一个 double 结果 将 long 值转换成 double 输出结果 double applyAsDouble(long value)
    DoubleToIntFunction 接受一个 double 参数,返回一个 int 结果 将 double 值转换成 int 输出结果 int applyAsInt(double value)
    DoubleToLongFunction 接受一个 double 参数,返回一个 long 结果 将 double 值转换成 long 输出结果 long applyAsLong(double value)

    非基本类型

    在使用函数接口时,名称无关紧要——只要参数类型和返回类型相同。Java 会将你的方法映射到接口方法。示例:

    import java.util.function.BiConsumer;
    
    class In1 {}
    class In2 {}
    
    public class MethodConversion {
    
        static void accept(In1 in1, In2 in2) {
            System.out.println("accept()");
        }
    
        static void someOtherName(In1 in1, In2 in2) {
            System.out.println("someOtherName()");
        }
    
        public static void main(String[] args) {
            BiConsumer<In1, In2> bic;
    
            bic = MethodConversion::accept;
            bic.accept(new In1(), new In2());
    
            // 在使用函数接口时,名称无关紧要——只要参数类型和返回类型相同。Java 会将你的方法映射到接口方法。
            bic = MethodConversion::someOtherName;
            bic.accept(new In1(), new In2());
        }
    }

    输出结果:

    accept()
    someOtherName()

    将方法引用应用于基于类的函数式接口(即那些不包含基本类型的函数式接口)

    import java.util.Comparator;
    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 aa, AA bb) { 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());
            // 接受一个参数,返回 boolean 值: 测试参数是否满足特定条件
            Predicate<AA> p = ClassFunctionals::f7;
            boolean result = p.test(new AA());
            // 接受两个参数,返回 boolean 值,测试两个参数是否满足特定条件
            BiPredicate<AA, BB> bip = ClassFunctionals::f8;
            result = bip.test(new AA(), new BB());
            // 接受一个参数,返回一个相同类型的结果,对输入执行单一操作并返回相同类型的结果,是 Function 的特殊情况
            UnaryOperator<AA> uo = ClassFunctionals::f9;
            AA aa = uo.apply(new AA());
            // 接受两个相同类型的参数,返回一个相同类型的结果,将两个相同类型的值组合成一个新值,是 BiFunction 的特殊情况
            BinaryOperator<AA> bo = ClassFunctionals::f10;
            aa = bo.apply(new AA(), new AA());
        }
    }

    以下是用表格整理的非基本类型的函数式接口:

    函数式接口 特征 用途 方法名
    Supplier 无参数,返回一个结果 获取值或实例,工厂模式,延迟计算 T get()
    Comparator 接受两个参数,返回 int 值 比较两个对象,用于排序和比较操作 int compare(T o1, T o2)
    Consumer 接受一个参数,无返回值 执行操作,通常是副作用操作,不需要返回结果 void accept(T t)
    BiConsumerabbd655bd3f9f929be0207abcc18a2ef 接受两个参数,无返回值 执行操作,通常是副作用操作,不需要返回结果,接受两个参数 void accept(T t, U u)
    Function469df1b70b2914a3841e0404ec3e3d5d 接受一个参数,返回一个结果 将输入参数转换成输出结果,如数据转换或映射操作 R apply(T t)
    BiFunction96b8c9ba1c8b330d622a8468a6113c2e 接受两个参数,返回一个结果 将两个输入参数转换成输出结果,如数据转换或映射操作 R apply(T t, U u)
    Predicate 接受一个参数,返回 boolean 值 测试参数是否满足特定条件 boolean test(T t)
    BiPredicateabbd655bd3f9f929be0207abcc18a2ef 接受两个参数,返回 boolean 值 测试两个参数是否满足特定条件 boolean test(T t, U u)
    UnaryOperator 接受一个参数,返回一个相同类型的结果 对输入执行单一操作并返回相同类型的结果,是 Function 的特殊情况 T apply(T t)
    BinaryOperator 接受两个相同类型的参数,返回一个相同类型的结果 将两个相同类型的值组合成一个新值,是 BiFunction 的特殊情况 T apply(T t1, T t2)

    多参数函数式接口

    java.util.functional 中的接口是有限的,如果需要 3 个参数函数的接口怎么办?自己创建就可以了,如下:

    // 创建处理 3 个参数的函数式接口
    @FunctionalInterface
    public interface TriFunction<T, U, V, R> {
        
        R apply(T t, U u, V v);
    }

    验证如下:

    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> tf1 = TriFunctionTest::f;
            // Lamdba 表达式
            TriFunction<Integer, Long, Double, Integer> tf2 = (i, l, d) -> 12;
        }
    }

    高阶函数

    高阶函数(Higher-order Function)其实很好理解,并且在函数式编程中非常常见,它有以下特点:

    • 接收一个或多个函数作为参数

    • 返回一个函数作为结果

    先来看看一个函数如何返回一个函数:

    import java.util.function.Function;
    
    interface FuncSS extends Function<String, String> {}        // [1] 使用继承,轻松创建属于自己的函数式接口
    
    public class ProduceFunction {
        // produce() 是一个高阶函数:既函数的消费者,产生函数的函数
        static FuncSS produce() {
            return s -> s.toLowerCase();    // [2] 使用 Lambda 表达式,可以轻松地在方法中创建和返回一个函数
        }
    
        public static void main(String[] args) {
            FuncSS funcSS = produce();
            System.out.println(funcSS.apply("YELLING"));
        }
    }

    然后再看看,如何接收一个函数作为函数的参数:

    class One {}
    class Two {}
    
    public class ConsumeFunction {
        static Two consume(Function<One, Two> onetwo) {
            return onetwo.apply(new One());
        }
    
        public static void main(String[] args) {
            Two two = consume(one -> new Two());
        }
    }

    总之,高阶函数使代码更加简洁、灵活和可重用,常见于 Stream 流式编程中

    闭包

    在 Java 中,闭包通常与 lambda 表达式和匿名内部类相关。简单来说,闭包允许在一个函数内部访问和操作其外部作用域中的变量。在 Java 中的闭包实际上是一个特殊的对象,它封装了一个函数及其相关的环境。这意味着闭包不仅仅是一个函数,它还携带了一个执行上下文,其中包括外部作用域中的变量。这使得闭包在访问这些变量时可以在不同的执行上下文中保持它们的值。

    让我们通过一个例子来理解 Java 中的闭包:

    public class ClosureExample {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
    
            // 这是一个闭包,因为它捕获了外部作用域中的变量 a 和 b
            IntBinaryOperator closure = (x, y) -> x * a + y * b;
    
            int result = closure.applyAsInt(3, 4);
            System.out.println("Result: " + result); // 输出 "Result: 110"
        }
    }

    需要注意的是,在 Java 中,闭包捕获的外部变量必须是 final 或者是有效的 final(即在实际使用过程中保持不变)。这是为了防止在多线程环境中引起不可预测的行为和数据不一致。

    函数组合

    函数组合(Function Composition)意为 “多个函数组合成新函数”。它通常是函数式 编程的基本组成部分。

    先看 Function 函数组合示例代码:

    import java.util.function.Function;
    
    public class FunctionComposition {
        static Function<String, String> f1 = s -> {
            System.out.println(s);
            return s.replace(&#39;A&#39;, &#39;_&#39;);
        },
        f2 = s -> s.substring(3),
        f3 = s -> s.toLowerCase(),
        // 重点:使用函数组合将多个函数组合在一起
        // compose 是先执行参数中的函数,再执行调用者
        // andThen 是先执行调用者,再执行参数中的函数
        f4 = f1.compose(f2).andThen(f3);        
    
        public static void main(String[] args) {
            String s = f4.apply("GO AFTER ALL AMBULANCES");
            System.out.println(s);
        }
    }

    代码示例使用了 Function 里的 compose() 和 andThen(),它们的区别如下:

    • compose 是先执行参数中的函数,再执行调用者

    • andThen 是先执行调用者,再执行参数中的函数

    输出结果:

    AFTER ALL AMBULANCES
    _fter _ll _mbul_nces

    然后,再看一段 Predicate 的逻辑运算演示代码:

    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);    // 使用谓词组合将多个谓词组合在一起,negate 是取反,and 是与,or 是或
    
        public static void main(String[] args) {
            Stream.of("bar", "foobar", "foobaz", "fongopuckey")
                    .filter(p4)
                    .forEach(System.out::println);
        }
    }

    p4 通过函数组合生成一个复杂的谓词,最后应用在 filter() 中:

    • negate():取反值,内容不包含 bar

    • and(p2):长度小于 5

    • or(p3):或者包含 f3

    输出结果:

    foobar
    foobaz

    在 java.util.function 中常用的支持函数组合的方法,大致如下:

    函数式接口 方法名 描述
    Function469df1b70b2914a3841e0404ec3e3d5d andThen 用于从左到右组合两个函数,即:h(x) = g(f(x))
    Function469df1b70b2914a3841e0404ec3e3d5d compose 用于从右到左组合两个函数,即:h(x) = f(g(x))
    Consumer andThen 用于从左到右组合两个消费者,按顺序执行两个消费者操作
    Predicate and 用于组合两个谓词函数,返回一个新的谓词函数,满足两个谓词函数的条件
    Predicate or 用于组合两个谓词函数,返回一个新的谓词函数,满足其中一个谓词函数的条件
    Predicate negate 用于对谓词函数取反,返回一个新的谓词函数,满足相反的条件
    UnaryOperator andThen 用于从左到右组合两个一元操作符,即:h(x) = g(f(x))
    UnaryOperator compose 用于从右到左组合两个一元操作符,即:h(x) = f(g(x))
    BinaryOperator andThen 用于从左到右组合两个二元操作符,即:h(x, y) = g(f(x, y))

    柯里化

    柯里化(Currying)是函数式编程中的一种技术,它将一个接受多个参数的函数转换为一系列单参数函数。

    让我们通过一个简单的 Java 示例来理解柯里化:

    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;
            System.out.println(uncurried("Hi ", "Ho"));
    
            // 通过链式调用逐个传递参数
            Function<String, String> hi = sum.apply("Hi ");
            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

    接下来我们添加层级来柯里化一个三参数函数:

    import java.util.function.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

    在处理基本类型的时候,注意选择合适的函数式接口:

    import java.util.function.IntFunction;
    import java.util.function.IntUnaryOperator;
    
    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

    以上是Java中的函數式程式設計怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除