Java中的函数组成

One can argue that composing functions is the most important idea in programming and functional programming in particular. The idea of compositionality is of monumental importance in human thinking. Creating systems by composing smaller systems that are easier to implement and design can be found in all levels of programming. Staring from the Syntax of the language, to Design patterns, architectural patterns, larger System design etc.

可以说,在编程中,尤其是在函数编程中,组成函数是最重要的想法。 组合性思想在人类思维中具有极其重要的意义。 在各个级别的编程中都可以找到通过组成更易于实现和设计的较小系统来创建系统的信息。 从语言的语法开始,一直到设计模式,建筑模式,更大的系统设计等。

In this section we are going to take a brief look at one of the most basic ones; Function composition.

在本节中,我们将简要介绍最基本的方法之一。 功能组成。

Image for post

In mathematics, function composition is the application of one function to the result of another to produce a third function. For instance, the functions f : T→ R and g : R→ U can be composed to yield a function h: g(f(x)) which maps T→ U we usually represent that as g ○ f and we read that as “g after f ” the symbol usually represents “after”.

在数学中,函数合成是将一个函数应用于另一个函数的结果以产生第三个函数。 例如,可以将函数f:T→Rg:R→U组合成函数h:g(f(x)) ,它映射T→U,我们通常将其表示为g○f,而我们将其读为“ g f 之后 ”,符号通常表示“ after ”。

Programmatically for the functional interfaces there are two ways called .andThen() and .compose() to make this composition. If you have a Function<T, R> you can:

对于功能接口,以编程方式有两种方法可以使.andThen().compose()进行组合。 如果您具有Function <T,R>,则可以:

  1. Either post-compose .andThen(Function<R, U>) another function

    .andThen(Function<R, U>)另一个函数

  2. Or you can start with Function<R, U> and pre-compose compose.compose(Function<T, R>)

    或者您可以从Function <R,U>开始并预撰写compose .compose(Function<T, R>)

Now we are now going to take a brief look at the most common java util Functional interface

现在,我们将简要介绍最常见的Java util Functional接口

功能<,>组成 (Function<,> composition)

Since the most usual functional interface is Function<T, R> it provides a default method .andThen (docs.oracle) that takes another Function<R, U> and returns a Function<T, U>

由于最常用的功能接口是Function <T,R>,因此它提供了默认方法.andThen( docs.oracle ),该方法采用另一个Function <R,U>并返回Function <T,U>

run this : Fiddle

运行此: 小提琴

Image for post

we can write the composition in the opposite direction using the compose method

我们可以使用compose方法以相反的方向编写构图

Image for post

BiFunction <>组成 (BiFunction<> Composition)

BiFunction<U, V, R> also provides an andThen method

BiFunction <U,V,R>还提供了andThen方法

Image for post

Take a look at the following function composition :

看一下下面的函数组成:

Run This: Fiddle

运行此: 小提琴

Image for post

Excerpt from the book:

摘录:

Image for post
https://leanpub.com/functional-java-with-vavr https://leanpub.com/functional-java-with-vavr

翻译自: https://medium.com/@dimpapadim3/function-composition-in-java-beaf39426f52

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java函数用于定义一组功能,可以在程序多次调用。函数由方法名、返回类型、参数组成。常用的函数有main()方法、构造函数、普通方法。main()方法是程序的入口,构造函数用于创建对象,普通方法用于实现特定功能。使用函数时需要通过方法名和参数来调用。 ### 回答2: Java的function函数也称为函数接口或Lambda表达式,是一种将函数作为一等公民的编程模式。Java 8之后引入了函数接口的概念,为开发者提供了更多的编程方式和灵活性。函数接口包括以下几个部分: 1. 函数接口类型 函数接口类型是一种抽象类型,用于定义函数的输入和输出。它由函数类型和参数列表组成Java内置了一些常用的函数接口类型,例如:Function, Consumer, Supplier等。 2. Lambda表达式 Lambda表达式是Java的一种语法,用于将函数当做一等公民进行编程。Lambda表达式可以简化编程语言,避免代码冗余。Lambda表达式不需要在它们自己的类定义,也不需要实现接口。 3. 方法引用 方法引用是Lambda表达式的一种自然语言形式,它提供了一种简洁的方式来引用现有的Java方法。例如,下面的代码展示了一个方法引用的例子: ``` list.stream().forEach(System.out::println); ``` 这个例子,forEach方法引用了System.out.println方法。 函数接口的使用方式: 1. 使用Lambda表达式 使用Lambda表达式可以简化编程语言,避免代码冗余。Lambda表达式不需要在它们自己的类定义,也不需要实现接口。例如,下面的代码展示一个由Lambda表达式实现的Function函数: ``` Function<Integer, Integer> addOne = (x) -> x + 1; ``` 2. 方法引用 方法引用可以轻松地将现有的Java方法用作Lambda表达式的参数。例如,下面的代码展示了一个方法引用的例子: ``` Function<Integer, Integer> addOne = Integer::valueOf; ``` 这个例子,valueOf方法被引用到了一个Function接口。 总之,Java的function函数提供了一种新的、灵活的编程方式,使得编程变得更加容易和高效。通过Lambda表达式和方法引用,我们可以避免代码冗余,提高代码可读性和可维护性。 ### 回答3: Java的Function函数是一个函数式接口,其定义如下: ``` @FunctionalInterface public interface Function<T, R> { R apply(T t); } ``` 其,T表示传入参数的类型,R表示返回值的类型,apply方法表示函数的执行体。 Function函数的主要作用是根据一个输入值生成一个输出值,在Java经常用于集合的转换和映射。例如,我们可以使用Function函数将一个字符串转换为其长度,代码实现如下: ``` List<String> list = Arrays.asList("Hello", "World", "Java"); Function<String, Integer> toLength = s -> s.length(); // 定义一个Function函数,将字符串转换为字符串长度 List<Integer> lengthList = list.stream().map(toLength).collect(Collectors.toList()); // 将集合的每个元素应用toLength函数,转换为字符串长度 System.out.println(lengthList); // 输出 [5, 5, 4] ``` 另一个常见的用途是在条件判断时使用Function函数,例如我们可以使用Function函数根据条件返回不同的结果,代码实现如下: ``` Function<Integer, String> isEven = i -> i % 2 == 0 ? "偶数" : "奇数"; // 定义一个Function函数,判断一个整数是偶数还是奇数 System.out.println(isEven.apply(3)); // 输出 "奇数" System.out.println(isEven.apply(4)); // 输出 "偶数" ``` 总的来说,Function函数Java 8引入的函数式编程元素,其用法涉及到lambda表达式、Stream API等,可以大大简化代码并使其更具可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值